robinrodricks / vue3-touch-events

Simple touch events support for vue.js 3
MIT License
216 stars 27 forks source link

Issue with the swipe detection logic #31

Closed mohsenasm closed 7 months ago

mohsenasm commented 7 months ago

I was reviewing the swipe detection code to better understand its logic.

In this line we have:

if (distanceY > swipeOutBounded) {
    direction = $this.startY > $this.currentY ? 'top' : 'bottom';
} else {
    direction = $this.startX > $this.currentX ? 'left' : 'right';
}

In which the swipeOutBounded variable is $this.options.swipeTolerance. Shouldn't it be:

if (distanceY > distanceX) {
    direction = $this.startY > $this.currentY ? 'top' : 'bottom';
} else {
    direction = $this.startX > $this.currentX ? 'left' : 'right';
}

In the current code, we are prioritizing top/bottom swipes over left/right swipes. There is no need for prioritization. We can check if the swipe was a more vertical one or a more horizontal one, and make the decision based on that.

For example, if the start point, current point, and swipeTolerance are like this:

startX = 0
startY = 0
currentX = 300
currentY = 100
swipeTolerance = 50

The current code will set direction = 'bottom' instead of direction = 'right'.

robinrodricks commented 7 months ago

If you feel there is an issue or bug here can you submit a PR I'll check it.

mohsenasm commented 7 months ago

Hi @robinrodricks I submitted a PR. I also reported the same issue at https://github.com/jerrybendy/vue-touch-events/issues/100 and it just merged in that repo.

robinrodricks commented 7 months ago

Thanks, published.

vue3-touch-events@4.1.6