It seems you only need a more reliable method of obtaining relative coordinates when the page is full.
@RyanArtecona wrote the following function in response to this question about mouse coordinates relative to the canvas :
function relMouseCoords(event){ var totalOffsetX = 0; var totalOffsetY = 0; var canvasX = 0; var canvasY = 0; var currentElement = this; do{ totalOffsetX += currentElement.offsetLeft - currentElement.scrollLeft; totalOffsetY += currentElement.offsetTop - currentElement.scrollTop; } while(currentElement = currentElement.offsetParent) canvasX = event.pageX - totalOffsetX; canvasY = event.pageY - totalOffsetY; return {x:canvasX, y:canvasY} } HTMLCanvasElement.prototype.relMouseCoords = relMouseCoords;
This is convenient because it adds a function for obtaining relative coordinates directly on the prototype of the HTMLCanvasElement function, which means that you can just pass the link to the canvas you want to use and get the coordinates relative to it.
Using this, you can rewrite the mousedown function (and you also want to do the rest, but just for example) as follows:
function onMouseDown(event) { 'use strict'; mousePressed = true;
jshanley
source share