Reputation: 1112
I have a shape, in HTML5 canvas, drawn with JavaScript:
ctx.beginPath();
ctx.moveTo(25,0);
ctx.quadraticCurveTo(50,50,40,100);
ctx.lineTo(33,100);
ctx.quadraticCurveTo(50,50,20,0);
ctx.fill();
ctx.closePath();
If a click my canvas, how can I detect that I clicked this shape?
canvas.addEventListener("click",function(e){
alert(isItPartOfTheShape(e.clientX,e.clientY));
})
function isItPartOfTheShape(x,y){
/* Here comes the code which detects is it part of the shape */
return isIt;
}
Upvotes: 2
Views: 399
Reputation: 114461
You have two choices
context.isPointInPath
that however requires you to rebuild the shape when you do the test (that function checks the specified point only against the current path, i.e. the path that would be filled calling fill
)Upvotes: 1