简体   繁体   中英

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.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.

evt.which will be 1 if left mouse button is held down during the fired mousemove event, 0 if it's not.

Test with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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