I'm trying to find out where the user clicked on the canvas then display some stuff based on that. As you can see in my code I tried using xy coordinates but whenever I click somewhere else sharing the same x or y coordinate it alerts. Is it possible to have multiple canvases overlap each other or use different shapes that will trigger an event if mouse down on them?
function doMouseDown(event) {
var canvas_x = event.pageX;
var canvas_y = event.pageY;
if ((canvas_x <= 351 || canvas_x >= 346) && (canvas_y <= 155 || canvas_y >= 120)) {
alert("You are at *insert place on map*");
}
function startCanvas() {
var c = document.getElementById("mycanvas");
c.addEventListener("mousedown", doMouseDown, false);
var ctx = c.getContext('2d');
ctx.strokeStyle = "rgba(0,0,0,0.4)";
ctx.fillStyle = "rgba(0,0,0,0.4)";
var x = 340;
var y = 120;
var width = 100;
var height = 30;
var image = new Image();
image.src = "image.gif";
image.onload = function() {
ctx.drawImage(image, 0, 0);
};
}
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript">
</script>
</head>
<body onload="startCanvas()">
<canvas id="mycanvas" height="1000" width="1500">Not supported </canvas>
</body>
</html>