Is there any algorithm to determine whether the clicked location is inside the unshaped object or not?
I am new at canvas. but, it would be easy to know the mouse coordinates are in the shaped object especially in rectangle. But, what about an unshaped object or sphere/hexagon/octagon etc.?
For example I have a following code. Then how do I implement the logic to determine whether the coordinates of my mouse click is inside the object area or not?
function drawShape(){
var canvas = document.getElementById("cnvs");
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.moveTo(20,0);
ctx.lineTo(30,10);
ctx.lineTo(30,50);
ctx.lineTo(45,100);
ctx.lineTo(60,45);
ctx.lineTo(55,30);
ctx.lineTo(90,45);
ctx.lineTo(100,100);
ctx.lineTo(50,150);
ctx.lineTo(45,160);
ctx.lineTo(20,100);
ctx.lineTo(10,50);
ctx.closePath();
ctx.fill();
$(canvas).click(function(){
var canvas = this;
var event = window.event;
alert(event.pageX - canvas.offsetLeft);
});
}
drawShape();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<canvas id="cnvs" width="200" height="200">
</canvas>