I am a new user of html5 and using canvas to draw circle for one of my application. I have drawn several arcs to form a circle. it works fine. However my problem is that i want to associate a separate mouse event for each of the arc.
I goggled and found KinteticJS may be useful. I wish to know is there any other way that could be used to attached mouse event for every arc i created using the canvas. Please note i have used just one canvas there is no ghost canvas and I do not wish to use SVG. Below is the line of the code i used:
context.arc(x, y, radius, startAngle, endAngle, counterClockwise);
canvas.addEventListener("mousedown", doMouseDown(evt), false);
regards nad
The quick answer is: No, but...
No:
Canvas does not "remember" anything drawn on itself--drawings are just color pixels added to the canvas. Canvas doesn't know the position of your arc and can't hit-test to see if the mousdown is inside that arc.
but...you can use math
You can use math to test if any point is inside the circle.
If any x/y is inside the outer radius of a circle and not inside the inner radius, then x/y is in the arc.
Given centerX,centerY,outerRadius,innerRadius:
Test if x,y is inside the outer radius:
Test if x,y is not inside the inner radius:
So if( isInsideOuterRadius && !isInsideInnerRadius){ alert("x/y is in your arc"); }
If you want to get fancy:
If you "remember" the arc for canvas, then canvas has a hit-test for that arc.
Context.isPointInStroke will test if an X/Y is located inside the most recently drawn path. If that most recent path is your arc, you can hit-test your arc.
For example, if you draw this arc:
You can hit-test that arc by supplying isPointInStroke with your mouse coordinates:
To test multiple arcs,
Except IE :(
isPointInStroke will work in Chrome and Firefox but won't yet work in Internet Explorer.
Alternatively for IE:
You can define a path around the outside of your arc and then use isPointInPath to test if your mouse is inside that path.
[ Update: and example ]
Here's a Fiddle (must view with Chrome or FF -- IE won't work):
http://jsfiddle.net/m1erickson/DsPL7/
Here's example code: