Skip to content

Instantly share code, notes, and snippets.

@McFunkypants
Last active December 11, 2015 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save McFunkypants/4516039 to your computer and use it in GitHub Desktop.
Save McFunkypants/4516039 to your computer and use it in GitHub Desktop.
// handle click events on the canvas
function canvasClick(e)
{
var x;
var y;
// grab html page coords
if (e.pageX != undefined && e.pageY != undefined)
{
x = e.pageX;
y = e.pageY;
}
else
{
x = e.clientX + document.body.scrollLeft +
document.documentElement.scrollLeft;
y = e.clientY + document.body.scrollTop +
document.documentElement.scrollTop;
}
// make them relative to the canvas only
x -= canvas.offsetLeft;
y -= canvas.offsetTop;
// return tile x,y that we clicked
var cell =
[
Math.floor(x/tileWidth),
Math.floor(y/tileHeight)
];
// now we know while tile we clicked
console.log('we clicked tile '+cell[0]+','+cell[1]);
pathStart = pathEnd;
pathEnd = cell;
// calculate path
currentPath = findPath(world,pathStart,pathEnd);
redraw();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment