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". ;-)

In reply to Re^3: [OT] QML Canvas Context2D and mouse input by afoken
in thread [OT] QML Canvas Context2D and mouse input by afoken

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.