A common question, but I still need help. I try to get and save the coordinates of the mouse when someone clicks on the canvas.
my html
<canvas id="puppyCanvas" width="500" height="500" style="border:2px solid black" onclick = "storeGuess(event)"></canvas>
and my javascript
var canvasSetup = document.getElementById("puppyCanvas");
var ctx = canvasSetup.getContext("2d");
guessX = 0;
guessY = 0;
function storeGuess(event){
var x = event.clientX - ctx.canvas.offsetLeft;
var y = event.clientY - ctx.canvas.offsetTop;
console.log("x coords:" + x + ", y coords" + y);
I read a lot of forums, w3schools and videos on this. I am close to understanding this, but I have missed something. I can get the page coordinates if I delete ctx.canvas.offsetLeft and ctx.canvas.offsetTop. But I need to somehow enable them in order to get the coordinates of the canvas, and then save them into guessX variables and guess.
source
share