LuisF
LuisF

Reputation: 21

how to prevent the mousedown event without preventing scroll?

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)
});

Upvotes: 0

Views: 36

Answers (1)

LuisF
LuisF

Reputation: 21

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)
});

Upvotes: 0

Related Questions