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;