File tree 5 files changed +22
-9
lines changed
5 files changed +22
-9
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' @use-gesture/core ' : patch
3
+ ---
4
+
5
+ fix: calculate swipe from raw movement (\_ movement) #592
Original file line number Diff line number Diff line change @@ -35,12 +35,13 @@ const bind = useXXXX(state => {
35
35
distance , // offset distance per axis
36
36
direction , // direction per axis
37
37
overflow , // whether the offset is overflowing bounds (per axis)
38
- startTime , // gesture start time
39
- elapsedTime , // gesture elapsed time
38
+ startTime , // gesture start time (ms)
39
+ timeDelta , // Time delta (ms) with the previous event
40
+ elapsedTime , // gesture elapsed time (ms)
41
+ timeStamp , // timestamp of the event
40
42
type , // event type
41
43
target , // event target
42
44
currentTarget , // event currentTarget
43
- timeStamp , // timestamp of the event
44
45
first , // true when it's the first event
45
46
last , // true when it's the last event
46
47
active , // true when the gesture is active
Original file line number Diff line number Diff line change @@ -233,16 +233,18 @@ export class DragEngine extends CoordinatesEngine<'drag'> {
233
233
if ( state . tap && config . filterTaps ) {
234
234
state . _force = true
235
235
} else {
236
- const [ dirx , diry ] = state . direction
237
- const [ vx , vy ] = state . velocity
238
- const [ mx , my ] = state . movement
236
+ const [ _dx , _dy ] = state . _delta
237
+ const [ _mx , _my ] = state . _movement
239
238
const [ svx , svy ] = config . swipe . velocity
240
239
const [ sx , sy ] = config . swipe . distance
241
240
const sdt = config . swipe . duration
242
241
243
242
if ( state . elapsedTime < sdt ) {
244
- if ( Math . abs ( vx ) > svx && Math . abs ( mx ) > sx ) state . swipe [ 0 ] = dirx
245
- if ( Math . abs ( vy ) > svy && Math . abs ( my ) > sy ) state . swipe [ 1 ] = diry
243
+ const _vx = Math . abs ( _dx / state . timeDelta )
244
+ const _vy = Math . abs ( _dy / state . timeDelta )
245
+
246
+ if ( _vx > svx && Math . abs ( _mx ) > sx ) state . swipe [ 0 ] = Math . sign ( _dx )
247
+ if ( _vy > svy && Math . abs ( _my ) > sy ) state . swipe [ 1 ] = Math . sign ( _dy )
246
248
}
247
249
}
248
250
Original file line number Diff line number Diff line change @@ -166,7 +166,7 @@ export abstract class Engine<Key extends GestureKey> {
166
166
state . args = args
167
167
state . axis = undefined
168
168
state . memo = undefined
169
- state . elapsedTime = 0
169
+ state . elapsedTime = state . timeDelta = 0
170
170
state . direction = [ 0 , 0 ]
171
171
state . distance = [ 0 , 0 ]
172
172
state . overflow = [ 0 , 0 ]
@@ -349,6 +349,7 @@ export abstract class Engine<Key extends GestureKey> {
349
349
if ( ! state . first && dt > 0 ) {
350
350
// calculates kinematics unless the gesture starts or ends
351
351
state . velocity = [ absoluteDelta [ 0 ] / dt , absoluteDelta [ 1 ] / dt ]
352
+ state . timeDelta = dt
352
353
}
353
354
}
354
355
}
Original file line number Diff line number Diff line change @@ -162,6 +162,10 @@ export type CommonGestureState = {
162
162
* Elapsed time (ms) of the current gesture.
163
163
*/
164
164
elapsedTime : number
165
+ /**
166
+ * Time delta (ms) with the previous event.
167
+ */
168
+ timeDelta : number
165
169
/**
166
170
* Event type.
167
171
*/
You can’t perform that action at this time.
0 commit comments