how to prevent the mousedown event without preventing scroll?

41 Views Asked by At

I need to prevent the mousedown event that happen after the touchstart event but don't know how to do it without preventing the user from scroll when swipes the screen. The touchstart event listener cannot be removed cause if the user(s) try to touch at two points barely at the same time none of the events are triggered.

canvas.addEventListener('touchstart', function (event) {
  touchPressed = true
  if (currentScreen.name == "end") event.preventDefault()//prevents mousedown event
  userInput(
    event.targetTouches[event.targetTouches.length - 1].clientX, 
    event.targetTouches[event.targetTouches.length - 1].clientY
  )
});
canvas.addEventListener('touchend', function () {
  touchPressed = false
});
canvas.addEventListener('mousedown', function (event) {
  userInput(event.clientX, event.clientY)
});
1

There are 1 best solutions below

1
On
canvas.addEventListener('touchstart', function (event) {
  touchPressed = true
  userInput(
    event.targetTouches[event.targetTouches.length - 1].clientX, 
    event.targetTouches[event.targetTouches.length - 1].clientY
  )
});
canvas.addEventListener('touchend', function () {
  touchPressed = false
  touchEventHasBeenTriggered = true
});
canvas.addEventListener('mousedown', function (event) {
  if(touchEventHasBeenTriggered){
    touchEventHasBeenTriggered = false
    return
  }
  userInput(event.clientX, event.clientY)
});