[英]How to detect mouse left button is being held down in javascript
I am trying to create code that gets the mouse pointers position when the mouse is left clicked.我正在尝试创建在单击鼠标左键时获取鼠标指针 position 的代码。 But when using i use the code below, it only returns the mouse pointer position ONCE when the mouse is left clicked and for it to get the mouse position again the button has to be released and clicked again.
但是当我使用下面的代码时,它只在鼠标左键单击时返回鼠标指针 position 一次,为了再次获得鼠标 position,必须释放并再次单击按钮。 How could I modify this code so that if the mouse is left clicked, coordinates of the mouse pointer are returned every 0.1 seconds.
我如何修改此代码,以便如果鼠标左键单击,鼠标指针的坐标每 0.1 秒返回一次。 Thus the code will be detecting if the left mouse button is being held
因此,代码将检测是否按住鼠标左键
var canvas = document.getElementById('canvas');
function getCursorPosition(canvas, event) {
const rect = canvas.getBoundingClientRect()
const x = event.clientX - rect.left
const y = event.clientY - rect.top
console.log("x: " + x + " y: " + y)
}
canvas.addEventListener('mousedown', function(e) {
getCursorPosition(canvas, e)
})
you should probably use mousemove event here:您可能应该在这里使用 mousemove 事件:
var holding = false;
var canvas = document.getElementById('canvas');
function getCursorPosition(canvas, event) {
const rect = canvas.getBoundingClientRect()
const x = event.clientX - rect.left
const y = event.clientY - rect.top
console.log("x: " + x + " y: " + y)
}
canvas.addEventListener('mousedown', function(e) {
holding = true;
})
canvas.addEventListener('mouseup', function(e) {
holding = false;
})
canvas.addEventListener('mousemove', function(e) {
if(holding == true){
getCursorPosition(canvas, e)
}
})
EDIT: You also need to set holding to false when you leave the button编辑:离开按钮时,您还需要将 hold 设置为 false
canvas.addEventListener('mouseleave', function(e) {
holding = false;
})
You can use "setInterval" and can specify your own wait time between every console in milliseconds您可以使用“setInterval”并可以指定您自己的每个控制台之间的等待时间(以毫秒为单位)
var canvas = document.getElementById('canvas');
function getCursorPosition(canvas, event) {
const rect = canvas.getBoundingClientRect()
const x = event.clientX - rect.left
const y = event.clientY - rect.top
console.log("x: " + x + " y: " + y)
}
var mousePosition, holding;
function myInterval() {
var setIntervalId = setInterval(function() {
if (!holding) clearInterval(setIntervalId);
getCursorPosition(canvas, mousePosition);
}, 100); //set your wait time between consoles in milliseconds here
}
canvas.addEventListener('mousedown', function() {
holding = true;
myInterval();
})
canvas.addEventListener('mouseup', function() {
holding = false;
myInterval();
})
canvas.addEventListener('mouseleave', function() {
holding = false;
myInterval();
})
canvas.addEventListener('mousemove', function(e) {
mousePosition = e;
})
Try this...尝试这个...
var canvas = document.getElementById('canvas'); var mouseIsDown = false; var mouseInterval = null; var mouseEvent = null; function getCursorPosition(canvas, event) { const rect = canvas.getBoundingClientRect() const x = event.clientX - rect.left const y = event.clientY - rect.top console.log("x: " + x + " y: " + y) } canvas.addEventListener('mousedown', function (e) { mouseEvent = e; mouseIsDown =;mouseIsDown, getCursorPosition(canvas, e) mouseInterval = setInterval(() => { getCursorPosition(canvas, mouseEvent) }; 100). }) canvas,addEventListener('mousemove'; function (e) { mouseEvent = e. }) canvas,addEventListener('mouseup'; function (e) { mouseIsDown =;mouseIsDown; clearInterval(mouseInterval); })
Edit: Depending on what you're trying to accomplish you might want to put the mouseup listener on the window object.编辑:根据您要完成的工作,您可能希望将 mouseup 侦听器放在 window object 上。 Just a thought.
只是一个想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.