[whatwg] '' <canvas> element "selection buffer"

On 07/06/2008, Ond?ej ?i?ka <ondra at dynawest.cz> wrote:
> Hi,
>
>  I've been looking for something similar to OpenGL's selection buffer - that is, you can get some object ID for the given coordinates.
>
>  E.g.,  Jacob Seidelin's chess game http://blog.nihilogic.dk/search/label/chess could use it, but instead, keyboard control had to be used.
>
>  isPointInPath() does not solve the problem effectively if the path would be too complex - e.g. pixel-based sprites in several layers.
>
>  For an example of what I want to implement, see e.g.
>  http://www.openttd.org/screens.php?image=images/screens/0.5.0/japan_national_railway_3_aug_1984 .
>  Mathematical computation of the object is principially impossible (or too complex in best case).

To hande sprites, you could draw each sprite onto its own canvas
(during the initial loading process) and test
spriteCtx.getImageData(x, y, 1, 1).data[3] > 127 to see if it's
sufficiently non-transparent. Quickly skip any sprites whose bounding
box does not contain (x,y), then test the remaining ones from front to
back to find the first that's solid under the given point, and that
should give the answer.

What problems does that approach have, that could be better solved by
a different approach (like selection buffers)?

The main issue I can think of is performance: using getImageData()
means you have to test every sprite that's possibly under the given
point, which could be expensive if you have a very large number of
them, whereas selectionBuffer.getIdAt() takes constant time but
introduces a (probably quite large) constant overhead to all drawing
operations. The selection buffer would help if you're doing a lot of
lookups compared to the amount of drawing and have a lot of
overlapping sprites, but I would expect it to be worse (since the
additional drawing cost would exceed the cost of the object-selection
code) in the much more common cases where you're only doing a single
lookup per frame or only have a few sprites overlapping any given
point. Are there other issues I'm missing?

-- 
Philip Taylor
excors at gmail.com

Received on Friday, 20 June 2008 17:16:58 UTC