I was aware of the usual method for finding the click position relative to the canvas, as has been documented before, which in brief is:
- Grab
event.pageXandevent.pageYvalues to get the position of the click relative to the document - subtract the x and y coords of the canvas on the page, either with
canvasEl.offsetLeft/canvas.El.offsetTopor using jQuery's$(canvasEl).offset(), recursing up through parents if necessary - Subtract the scroll position of the page and any parent elements, if any.
Sadly, this doesn't work when both the following are true:
- The canvas is part of a Polymer (and I assume any other WebComponent solution that polyfills Shadow DOM) element
- The canvas is positioned within a scrolling parent. I did some investigating into this and it looked like Polymer was skipping an ancestor when crossing the shadow boundary, meaning the scroll offset isn't calculated correctly.