VVolfe21
VVolfe21

Reputation: 3

How to only execute onmousemove function if onmousedown is active

I don't usually use vanilla js, and I simply can't make this happen. handleTilePressed is the function I want to execute, but I don't know how to make it so that onmousemove only works if onmousedown is active.

    tile.onmousemove = function() { handleTilePressed(i) }

Any help is greatly appreciated.

Upvotes: 0

Views: 32

Answers (1)

JaivBhup
JaivBhup

Reputation: 855

You have to create a variable that could determine it the mouse was click or not:

var isMouseDown = false;
tile.addEventListener('mousedown', e => {
  isMouseDown = true
});

window.addEventListener('mouseup', e => {
isMouseDown = false
});

Now you can use this variable to determine if you are dragging or not

tile.onmousemove = function() { 
 if(isMouseDown)
 handleTilePressed(i) 

}

Upvotes: 2

Related Questions