[whatwg] Canvas isPointInPath() coordinate space

David Flanagan david at davidflanagan.com
Wed Jul 14 22:58:07 PDT 2010


Here's another coordinate-space related question.

I assume that the intended purpose of isPointInPath() is hit testing. 
You get a click event on a canvas element, extract the mouse coordinates 
from the event object, subtract the canvas position from them, and pass 
them to isPointInPath() to figure out what part of your drawing the user 
has clicked on.

My question has to do with this paragraph from the spec:

> The intrinsic dimensions of the canvas element equal the size of the coordinate space, with the numbers interpreted in CSS pixels. However, the element can be sized arbitrarily by a style sheet. During rendering, the image is scaled to fit this layout size.

and this one:

> The isPointInPath(x, y) method must return true if the point given by the x and y coordinates passed to the method, when treated as coordinates in the canvas coordinate space unaffected by the current transformation...

So suppose I'm using this canvas tag:

<canvas width=100 height=100 style="width:200px; height:200px"></canvas>

If I understand the first quoted paragraph above correctly, this canvas 
will have 2 on-screen CSS pixels per coordinate space unit.

So here's my question: if I want to do hit-testing as described above, 
do I need to take the mouse coordinates from the event, subtract the 
offset of the canvas, and then divide by 2?  As the spec is written, I 
think I do have to do that division manually.  Is that what is intended? 
  What if the user has zoomed in?  Is it even possible to use 
isPointInPath() correctly in that case?

	David



More information about the whatwg mailing list