I am having the following function to get the mouse click positions(coordinates).
$('#myCanvas').on('click', function(e) {
event = e;
event = event || window.event;
var canvas = document.getElementById('myCanvas'),
x = event.pageX - canvas.offsetLeft,
y = event.pageY - canvas.offsetTop;
alert(x + ' ' + y);
});
I need to get the mouse point on clicking a position and also secound mouse point position after draging the same.
ie., mousedown point and mouseup points.
Try a little different setup:
So why do we listen to mouse up on the
window
? If you mouse outside thecanvas
and then release the mouse button, the event won't be registered with thecanvas
. So we need to listen to a global event such as thewindow
.Since we already have marked our
isDown
at the mouse down event we know that that the following mouse up "belongs" to the canvas (as we check theisDown
flag).So, you need drag'n'drop? Well, it's very easy: first you detect 'onclick' if point inside of your target (rect, circle, whatever) save point to variable , 'onmousemove' you're moving the object and then 'onmousedown' you're getting last point.
Hope it will help you!
use receivers like $('#myCanvas').mousedown and $('#myCanvas').mouseup