hyperandroid / CAAT

Canvas Advanced Animation Toolkit
hyperandroid.github.com/CAAT
MIT License
727 stars 117 forks source link

Exact pixel hit detection #142

Open ryanjarvis opened 11 years ago

ryanjarvis commented 11 years ago

It appears that an Actor is notified if a mouse event affects it by checking to see if the mouse event coordinates are contained within the bounding box of the Actor only.

I would like to see support for determining if you clicked on the actual pixels that the Actor paints on the canvas.

I am wondering how difficult it would be to implement this. I researched two approaches. First is using another canvas as a hit canvas described here: http://tschaub.net/blog/2011/03/31/canvas-hit-detection.html Not sure if that approach makes sense in CAAT's overall architecture. Second, is checking to see if a point is in the path of an Actor when it is drawn using isPointinPath described here: http://digitalarts.bgsu.edu/faculty/bonniem/Spring11/artc4330_1/notes/notes26.html If Actors are redrawn everytime the system checks to see if a mouse interaction occurred, I am thinking this might be doable.

sntran commented 10 years ago

I would second this. It may be easier with Box2D since you can loop through the fixture and check for mouse location is within any.

hyperandroid commented 10 years ago

Are you referring to actors that contain images, or to a general shape ? The first thing, is definitely straighforward. Just extract the image's alpha channel, and compare the alpha value at x,y coordinate values supplied to every mouse/click functions.

The other one, is certainly trickier. CAAT has two different stages per frame:

transformation and paint.

what you pretend is to know at input bubling time, whether the mouse is inside what an actor painted. Definitely isPointInPath exists in the canvas rendering context (ctx). But CAAT uses its own path implementation. If you anyway use the ctx to draw your actor, you could definitely call ctx.isPointInPath to know whether it lies inside/outside.

What is your exact use case ?

2013/5/16 Son Tran-Nguyen notifications@github.com

I would second this. It may be easier with Box2D since you can loop through the fixture and check for mouse location is within any.

— Reply to this email directly or view it on GitHubhttps://github.com/hyperandroid/CAAT/issues/142#issuecomment-18034107 .