javascript - Click event position (Y) is too high -
i have canvas element , when click on click position e.clientx(y) or e.screenx(y). strange happening. y value high. please @ image: http://img840.imageshack.us/img840/268/eventq.jpg. ideia why high?
you need take clientx , y , subtract position of canvas them.
this example unnecessarily verbose, show steps:
var canvas = document.getelementbyid('game'); var canvasx, canvasy; canvas.addeventlistener('click', function(event) { canvasx = canvas.offsetleft; canvasy = canvas.offsettop; var eventx = event.clientx; var eventy = event.clienty; var relx = eventx - canvasx; var rely = eventy - canvasy; alert('x = ' + relx + ', y = ' + rely); });
working sample: http://jsfiddle.net/jfhjf/
Comments
Post a Comment