Skip to main content
Improving the answer
Source Link

YouWhen you rely on mouse coordinates on the web browser it takes in count the whole screen. So you must not rely on absolute mouse coordinates, you have to subtract the canvas offset on the page from it to use it on the 2D Tile Map.

An example of code

var x = e.pageX - canvas.offsetLeft;
var y = e.pageY - canvas.offsetTop;

You must not rely on absolute mouse coordinates, you have to subtract the canvas offset from it.

var x = e.pageX - canvas.offsetLeft;
var y = e.pageY - canvas.offsetTop;

When you rely on mouse coordinates on the web browser it takes in count the whole screen. So you must not rely on absolute mouse coordinates, you have to subtract the canvas offset on the page from it to use it on the 2D Tile Map.

An example of code

var x = e.pageX - canvas.offsetLeft;
var y = e.pageY - canvas.offsetTop;
Source Link

You must not rely on absolute mouse coordinates, you have to subtract the canvas offset from it.

var x = e.pageX - canvas.offsetLeft;
var y = e.pageY - canvas.offsetTop;