Skip to main content
function that takes in canvas position and returns tile from 2d array of tiles called map
Source Link

Use the following for mouse movement:

window.addEventListener("mousemove", function(e)
{
    {
// offsetX and layerX are actual positons of the canvas, while clientX is the position 
 in the page
 // in the page
    mouse.x = e.offsetX || e.layerX;
        mouse.y = e.offsetY || e.layerY;
    // detect a mouse click here
    updateCanvasForClick();
});

You can also round the position to get the tile:

function getTile(canvasX, canvasY)
{
    var x = updateCanvasForClickMath.floor(canvasX / tileWidth);
    }var y = Math.floor(canvasY / tileHeight);
    return map[x][y];
}

Use the following for mouse movement:

window.addEventListener("mousemove", function(e)
    {
// offsetX and layerX are actual positons of the canvas, while clientX is the position in the page
        mouse.x = e.offsetX || e.layerX;
        mouse.y = e.offsetY || e.layerY;
// detect a mouse click here
        updateCanvasForClick();
    });

Use the following for mouse movement:

window.addEventListener("mousemove", function(e)
{
    // offsetX and layerX are actual positons of the canvas, while clientX is the position 
    // in the page
    mouse.x = e.offsetX || e.layerX;
    mouse.y = e.offsetY || e.layerY;
    // detect a mouse click here
    updateCanvasForClick();
});

You can also round the position to get the tile:

function getTile(canvasX, canvasY)
{
    var x = Math.floor(canvasX / tileWidth);
    var y = Math.floor(canvasY / tileHeight);
    return map[x][y];
}
Source Link

Use the following for mouse movement:

window.addEventListener("mousemove", function(e)
    {
// offsetX and layerX are actual positons of the canvas, while clientX is the position in the page
        mouse.x = e.offsetX || e.layerX;
        mouse.y = e.offsetY || e.layerY;
// detect a mouse click here
        updateCanvasForClick();
    });