There are plenty of discussions on Stackoverflow, on how to get the mouse coordinates in a Canvas. I finally found, that the solution outlined in https://stackoverflow.com/a/5417934 works well for me, WITH ONE EXCEPTION:
If I put a style definition for the canvas border into my document, i.e.
<style>
canvas { border: 42px solid red; }
</style>
The mouse position I get is off by exactly the border size (I can easily verify this by playing around with the border size). As a temporary solution, I manually adjusted the mouse coordinates by the border size, ending up with the following function for getting the mouse coordinates:
function getCursorPosition1(event) {
var canoffset=$('#pinaka').offset(); // Id of canvas is 'pinaka'
var x=event.clientX+document.body.scrollLeft+document.documentElement.scrollLeft-Math.floor(canoffset.left) - 42;
var y=event.clientY + document.body.scrollTop + document.documentElement.scrollTop - Math.floor(canoffset.top) + 1 - 42;
return [x,y]
}
Of course this is ugly. Is there a way to automatically derive the border size in my code?