Get Relative Position of Mouse-Click in Canvas

31 08 2009

function getRelativePosition(e) {
var canvas = document.getElementById("canvas-diagram");
var scroll = document.body.scrollTop;
if( typeof( canvas.offsetParent ) != 'undefined' ) {
for( var posX = 0, posY = 0; canvas; canvas = canvas.offsetParent ) {
posX += canvas.offsetLeft;
posY += canvas.offsetTop;
}
return {x: e.clientX - posX, y: e.clientY - (posY-scroll)};
} else {
return {x: e.clientX - canvas.offsetLeft, y: e.clientY - (canvas.offsetTop-scroll)};
}
}

Advertisements




Change Mouse Cursor

20 08 2007

<span style="cursor: default">default</span>

default crosshair
pointer wait
move e-resize
n-resize ne-resize
w-resize s-resize
nw-resize se-resize
sw-resize