Last active
August 29, 2015 14:07
-
-
Save BeauBouchard/f49423c9a6192ef1242d to your computer and use it in GitHub Desktop.
canvas on click handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function canvasOnClickHandler(event) { | |
var hex = getCursorPosition(event); | |
//action | |
map.tilelist[hex.row][hex.column].whateverfunctionisinTILE(); | |
}; | |
function getCursorPosition(event) { | |
var x; | |
var y; | |
if (event.pageX || event.pageY) { | |
x = event.pageX; | |
y = event.pageY; | |
} else { | |
x = event.clientX | |
+ document.body.scrollLeft | |
+ document.documentElement.scrollLeft; | |
y = event.clientY | |
+ document.body.scrollTop | |
+ document.documentElement.scrollTop; | |
} | |
x -= gridCanvas.offsetLeft; | |
y -= gridCanvas.offsetTop; | |
//map.MAPSIZE is the diagonal size of map, it should be computed in map, instead of here | |
mapsize = Math.sqrt((map.getWidth()^2)+(map.getHeight()^2)); | |
var tile = new TILE(Math.floor((y - 4) / mapsize), Math.floor((x - 2) / mapsize)); | |
return tile; | |
}; | |
gridCanvas.addEventListener("click", canvasOnClickHandler, false); | |
} else { | |
alert("Canvas is unsupported in your browser."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment