in reply to Re^2: [OT] QML Canvas Context2D and mouse input
in thread [OT] QML Canvas Context2D and mouse input
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
|
|---|