Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: [OT] QML Canvas Context2D and mouse input

by bliako (Monsignor)
on Jan 24, 2023 at 09:51 UTC ( [id://11149809]=note: print w/replies, xml ) Need Help??


in reply to [OT] QML Canvas Context2D and mouse input

Are you sure beginPath() returns something persistent? For example, if you are using this beginPath(), it returns nothing:

void QQuickContext2D::beginPath()

see: https://codebrowser.dev/qt5/qtdeclarative/src/quick/items/context2d/qquickcontext2d.cpp.html#_ZN15QQuickContext2D9beginPathEv

But you do get some value in p1, p2. This may be the current path, previous paths not persisting.

If there is a Path constructor then you could create your paths in the Canvas (not via context). Then paint them as you do in onPaint and check inclusion in the MouseArea. But diagonally looking, I did not see any! If none is available, just find+reuse a Shape class with its own PointInShape() method. Then all you have to do in onPaint is to extract the path and supply it to context or supply context to Shape to draw the path.

bw, bliako

Replies are listed 'Best First'.
Re^2: [OT] QML Canvas Context2D and mouse input
by afoken (Chancellor) on Jan 25, 2023 at 12:09 UTC

      Looking again at https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-ispointinpath, the example shows a click handler that (partially) draws each clickable object, then invokes isPointInPath() to check if that clickable object contains the mouse coordinates, and finally issues a redraw command, probably to clean up the mess that those checks made.

      That does not look very sane and efficient to me. I'll try that.

      It may be inefficient, but it works.

      Following the example from whatwg.org, I've moved the painting to javascript functions (generated at compile time from the SVG file using Perl and XML::LibXML). This is how it looks in QML:

      import QtQuick 2.0 ... import "mymap.js" as MyMap Rectangle { ... Canvas { id: canvas ... onPaint: { var ctx = getContext("2d"); MyMap.drawBackground(ctx); MyMap.drawAreas(ctx); } MouseArea { anchors.fill: parent onClicked: function(mouse) { var ctx = canvas.getContext("2d"); for (var i = 0; i < MyMap.areaIDs.length; i++) { MyMap.drawArea(ctx, ..., MyMap.areaIDs[i]); if (ctx.isPointInPath(mouse.x, mouse.y)) { console.log("Mouse click at ("+mouse.x+", "+mo +use.y+") hit area "+MyMap.areaIDs[i]); return; } } console.log("Mouse click at ("+mouse.x+", "+mouse.y+") + hit no area"); mouse.accepted = false; } } } }

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11149809]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2024-04-19 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found