简体   繁体   English

HTML 5 canvas:检测,如果在将鼠标移到 canvas 上时按下鼠标按钮

[英]HTML 5 canvas: detect, if mouse button is pressed while moving mouse over canvas

I was trying to detect, if mouse is pressed while moving over CANVAS:我试图检测在 CANVAS 上移动时是否按下了鼠标:

canvas.addEventListener('mousemove', function (evt) {
           ...
        });

Setting a flag inside mousedown and skipping it in mouseup is a bad idea, because user can release mouse button outside the canvas.在 mousedown 中设置一个标志并在 mouseup 中跳过它是一个坏主意,因为用户可以在 canvas 之外释放鼠标按钮。

evt.which will be 1 if left mouse button is held down during the fired mousemove event, 0 if it's not. evt.which将为1如果在触发的 mousemove 事件期间按住鼠标左键,否则为0

Test with:测试:

document.addEventListener("mousemove", function (e) {
    console.log(e.which);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM