Skip to content

Commit e628f8f

Browse files
committed
Allow very small twodrag and pinch gestures
Before this commit, we strictly required the user to make a ~50 pixel move in order to get these two gestures. We can get a more precise experience if we allow very small gestures as well. This can be achieved by making better use of our "_twoTouchTimeout". Our two touch timeout handling was written to handle this well anyway. We can distinguish between drag and pinch using the angle.
1 parent 047531e commit e628f8f

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

core/input/gesturehandler.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,14 @@ export default class GestureHandler {
186186
}
187187

188188
if (!this._hasDetectedGesture()) {
189-
// Ignore moves smaller than the minimum threshold
190-
if (Math.hypot(deltaX, deltaY) < GH_MOVE_THRESHOLD) {
191-
return;
192-
}
193189

194-
// Can't be a tap or long press as we've seen movement
195-
this._state &= ~(GH_ONETAP | GH_TWOTAP | GH_THREETAP | GH_LONGPRESS);
196-
this._stopLongpressTimeout();
190+
let deltaMove = Math.hypot(deltaX, deltaY);
191+
192+
// Can't be a tap or long press if we've seen movement
193+
if (deltaMove >= GH_MOVE_THRESHOLD) {
194+
this._state &= ~(GH_ONETAP | GH_TWOTAP | GH_THREETAP | GH_LONGPRESS);
195+
this._stopLongpressTimeout();
196+
}
197197

198198
if (this._tracked.length !== 1) {
199199
this._state &= ~(GH_DRAG);
@@ -213,10 +213,10 @@ export default class GestureHandler {
213213
let prevDeltaMove = Math.hypot(prevTouch.firstX - prevTouch.lastX,
214214
prevTouch.firstY - prevTouch.lastY);
215215

216-
// We know that the current touch moved far enough,
217-
// but unless both touches moved further than their
218-
// threshold we don't want to disqualify any gestures
219-
if (prevDeltaMove > GH_MOVE_THRESHOLD) {
216+
// Unless both touches moved further than their threshold we
217+
// don't want to disqualify any gestures right now
218+
if (deltaMove > GH_MOVE_THRESHOLD &&
219+
prevDeltaMove > GH_MOVE_THRESHOLD) {
220220

221221
// The angle difference between the direction of the touch points
222222
let deltaAngle = Math.abs(touch.angle - prevTouch.angle);
@@ -234,7 +234,8 @@ export default class GestureHandler {
234234
}
235235
} else if (!this._isTwoTouchTimeoutRunning()) {
236236
// We can't determine the gesture right now, let's
237-
// wait and see if more events are on their way
237+
// wait and see if more events are on their way.
238+
// If not, we'll have to decide which gesture it is.
238239
this._startTwoTouchTimeout();
239240
}
240241
}

tests/test.gesturehandler.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ describe('Gesture handler', function () {
580580
touchStart(1, 50.0, 40.0);
581581
touchStart(2, 60.0, 40.0);
582582
touchMove(1, 80.0, 40.0);
583-
touchMove(2, 110.0, 40.0);
583+
touchMove(2, 90.0, 40.0);
584584

585585
expect(gestures).to.not.have.been.called;
586586

@@ -601,15 +601,15 @@ describe('Gesture handler', function () {
601601
detail: { type: 'twodrag',
602602
clientX: 55.0,
603603
clientY: 40.0,
604-
magnitudeX: 40.0,
604+
magnitudeX: 30.0,
605605
magnitudeY: 0.0 } }));
606606
});
607607

608608
it('should handle slow vertical two finger drag', function () {
609609
touchStart(1, 40.0, 40.0);
610610
touchStart(2, 40.0, 60.0);
611611
touchMove(2, 40.0, 80.0);
612-
touchMove(1, 40.0, 100.0);
612+
touchMove(1, 40.0, 80.0);
613613

614614
expect(gestures).to.not.have.been.called;
615615

@@ -631,14 +631,14 @@ describe('Gesture handler', function () {
631631
clientX: 40.0,
632632
clientY: 50.0,
633633
magnitudeX: 0.0,
634-
magnitudeY: 40.0 } }));
634+
magnitudeY: 30.0 } }));
635635
});
636636

637637
it('should handle slow diagonal two finger drag', function () {
638638
touchStart(1, 50.0, 40.0);
639639
touchStart(2, 40.0, 60.0);
640640
touchMove(1, 70.0, 60.0);
641-
touchMove(2, 90.0, 110.0);
641+
touchMove(2, 60.0, 80.0);
642642

643643
expect(gestures).to.not.have.been.called;
644644

@@ -659,8 +659,8 @@ describe('Gesture handler', function () {
659659
detail: { type: 'twodrag',
660660
clientX: 45.0,
661661
clientY: 50.0,
662-
magnitudeX: 35.0,
663-
magnitudeY: 35.0 } }));
662+
magnitudeX: 20.0,
663+
magnitudeY: 20.0 } }));
664664
});
665665

666666
it('should ignore too slow two finger drag', function () {
@@ -783,7 +783,7 @@ describe('Gesture handler', function () {
783783
it('should handle pinching inwards slowly', function () {
784784
touchStart(1, 0.0, 0.0);
785785
touchStart(2, 130.0, 130.0);
786-
touchMove(1, 50.0, 40.0);
786+
touchMove(1, 30.0, 20.0);
787787
touchMove(2, 100.0, 130.0);
788788

789789
expect(gestures).to.not.have.been.called;
@@ -805,14 +805,14 @@ describe('Gesture handler', function () {
805805
detail: { type: 'pinch',
806806
clientX: 65.0,
807807
clientY: 65.0,
808-
magnitudeX: 50.0,
809-
magnitudeY: 90.0 } }));
808+
magnitudeX: 70.0,
809+
magnitudeY: 110.0 } }));
810810
});
811811

812812
it('should handle pinching outwards slowly', function () {
813813
touchStart(1, 100.0, 130.0);
814814
touchStart(2, 110.0, 130.0);
815-
touchMove(2, 200.0, 130.0);
815+
touchMove(2, 130.0, 130.0);
816816

817817
expect(gestures).to.not.have.been.called;
818818

@@ -833,7 +833,7 @@ describe('Gesture handler', function () {
833833
detail: { type: 'pinch',
834834
clientX: 105.0,
835835
clientY: 130.0,
836-
magnitudeX: 100.0,
836+
magnitudeX: 30.0,
837837
magnitudeY: 0.0 } }));
838838
});
839839

0 commit comments

Comments
 (0)