omega
omega

Reputation: 43833

How to get current mouse location while mouse is being dragged in javascript?

In this solution in javascript

JavaScript while mousedown

I can get the mouse location when it's first pressed and first clicked, but how can I get the location while its being pressed down?

Thanks

Upvotes: 1

Views: 74

Answers (2)

Seemax
Seemax

Reputation: 121

You can get any instance of mouse coordinate inside <canvas>. In this case, planting other tags inside <canvas> limited and could be done only via {position: absolute}. Code example:

canvas.addEventListener('mousemove', getPoint);
function getPoint(e){
const [x, y] = [e.offsetX, e.offsetY]; 
const offsetX = document.getElementById('offsetX');
offsetX.textContent = `Offset X: ${x}`;
const offsetY = document.getElementById('offsetY');
offsetY.textContent = `Offset Y: ${y}`;
};

Working example: http://codepen.io/462960/pen/RpVJwa

Upvotes: 0

Matej Marconak
Matej Marconak

Reputation: 1413

You can track every movement mouse, like this Track mouse movement and add logic for tracking only if before was mouse click down a stop when mouse was click up.

Upvotes: 1

Related Questions