canvas hit detection

Started by
7 comments, last by Sirisian 11 years, 6 months ago
i want to detect clicks on canvas elements which are drawn using paths. so far i have think of to store elements path in javascript data structure and then check the cordinates of hits which matches the elements cordinates. i belive there is algorithm already for thins kind o cordinate search. rendering each of element path and checking the hits would be inefficient when elements number is larger. can anyone point on me that?
also any other pointers and feedbacks are welcome.
Advertisement
1st off, that's the kinda thing that would be a piece of cake in flash, lolz

2ndly, as the above will not help you in the slightest here is an idea:

For everything you draw in your canvas. draw the same thing again in a second canvas (offscreen/invisible) but draw it with a solid colour, this colour must be unique and will be used ad an identifier, so keep track of it - make a link somehow between the element you are drawing and that colour.

then when the user clicks or moves their mouse over the canvas, just locate the x,y coordinates and use that as a lookup on the hidden canvas, you can then use the colour to identify the element they are over in the visible canvas, make sense?

cons:
you will have to draw everything twice (so bad idea for fullscreen canvas with tonnes of elements)
it will only give the id 1 element (the top most one) as you cannot hold references to muliple elements easily in one colour... would be rather tricky to implement)

pros:
it will be lightning fast to work out what the users mouse is over

I think this (for a lot of cases - but not all) will be a simpler and faster approach, detecting if a point is within a path can be quite tricky, especially if the path is complex and has curves/overlaps etc...
If you can't do it that way let me know and I will try to dig out some point in path algorithms.

b
color maping approach sounds nice and different. have not think it that way. i was thinking about doing it using paths. if it is possible for you to give any idea using paths, will be great. It is for edcuational purpose so there is alot of room for me for experiments and learn.

thanks for your comments.
bwhitling is right on the money.

I've implemented this technique in OpenGL. In the sources that I've read, this technique is called "color picking" if that helps your google searches. What's great about this technique is that you can assign a picking index to each element you wish to be pickable (clickable). This index can be an unsigned 32bit integer that you generate by incrementing a counter every time you create a pickable object. When you go to draw the pickable object as a solid color, all you need to do is convert the 32bit integer into four 8bit color components (RGBA) to get the color you should draw. Then when you read back the color, just convert the four 8bit color components back into a 32bit integer and you have the picking index of the object that got picked (clicked).
how will "color picking" work for layered element?
You want to use the Context2D's "isPointInPath" method. Here's a tutorial.

You want to use the Context2D's "isPointInPath" method. Here's a tutorial.

yes, i know that function. if i have 10 elements, i have to draw 10 paths and check the cordinates comes between that or not using that function. what i am wanting is some way (algorithm) to get selective elements which is closer or tend to be closer of the hit cordinate without looping through all elements.
how will "color picking" work for layered element?[/quote]

There are two ways to handle layered elements, depending on whether you wish to pick only one element at a time or multiple:

  1. The easier case is picking a single element. When you go to render out the elements as colors, only render out the elements you care to to pick. For instance, if you have a button element with a label element inside, chances are you don't care to pick the label element, only the button. So just render the button as a solid color based on its picking index and skip the label element. If you render out the colors using depth testing, then the topmost elements will take precedence when picking any point that has elements overlapping.
  2. The harder case is picking multiple elements, like if you want to implement rubberband selection. In that case, I've implemented a technique where I cycle thru the elements separately performing three operations for each of them.

  • Render out the element as a solid color based on its picking index.
  • Query the pixel(s) I care to pick at for color (picking index) and depth.
  • Clear the color and depth buffers before rendering the next element.

[indent=1]The query will tell me if I picked anything. Usually, I choose the color white (pickingIndex == int32.max) to represent nothing was picked, but black (pickingIndex == 0) could work too if you want to start your picking indices at 1 instead of 0. If I get a hit, I can then use the queried depth value to order the elements if that matters at all.

In both scenarios, you can limit your rendering to the a viewport that only encompasses the area you care to pick to lower the overhead of rendering (typically a 1x1 pixel area unless you're rubberband selecting or you want to buffer your picking precision). I do recommend however, to save the viewport optimization portion for after you've perfected your picking code, I've historically had to tweak things several times before picking was working properly, and it's hard to debug my picking logic when I'm only rendering out a single pixel =P.

[quote name='Sirisian' timestamp='1348859929' post='4984831']
You want to use the Context2D's "isPointInPath" method. Here's a tutorial.

yes, i know that function. if i have 10 elements, i have to draw 10 paths and check the cordinates comes between that or not using that function. what i am wanting is some way (algorithm) to get selective elements which is closer or tend to be closer of the hit cordinate without looping through all elements.
[/quote]
You don't have to draw 10 paths. You can store the path objects for instance and then perform the test without ever drawing them. (One of the overload is (path, x, y)). So you'll want to use a path object to make this more efficient. Color picking is going to be slower and will still require you to loop through all objects. If you want to test less then you probably want to use a spatial partitioning system for the path objects. That is insert the paths into a grid and only check the mouse against objects in the cell the coordinate is inside of.

(Also, not sure if it's supported yet but you'll have addHitRegion for treating drawn paths like DOM elements).

This topic is closed to new replies.

Advertisement