Skip to content

Commit 43e751a

Browse files
authoredApr 6, 2023
Fix/swipe 592 (#595)
1 parent ec433ae commit 43e751a

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed
 

‎.changeset/mighty-poets-collect.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@use-gesture/core': patch
3+
---
4+
5+
fix: calculate swipe from raw movement (\_movement) #592

‎documentation/pages/docs/state.mdx

+4-3
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ const bind = useXXXX(state => {
3535
distance, // offset distance per axis
3636
direction, // direction per axis
3737
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
4042
type, // event type
4143
target, // event target
4244
currentTarget, // event currentTarget
43-
timeStamp, // timestamp of the event
4445
first, // true when it's the first event
4546
last, // true when it's the last event
4647
active, // true when the gesture is active

‎packages/core/src/engines/DragEngine.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -233,16 +233,18 @@ export class DragEngine extends CoordinatesEngine<'drag'> {
233233
if (state.tap && config.filterTaps) {
234234
state._force = true
235235
} 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
239238
const [svx, svy] = config.swipe.velocity
240239
const [sx, sy] = config.swipe.distance
241240
const sdt = config.swipe.duration
242241

243242
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)
246248
}
247249
}
248250

‎packages/core/src/engines/Engine.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export abstract class Engine<Key extends GestureKey> {
166166
state.args = args
167167
state.axis = undefined
168168
state.memo = undefined
169-
state.elapsedTime = 0
169+
state.elapsedTime = state.timeDelta = 0
170170
state.direction = [0, 0]
171171
state.distance = [0, 0]
172172
state.overflow = [0, 0]
@@ -349,6 +349,7 @@ export abstract class Engine<Key extends GestureKey> {
349349
if (!state.first && dt > 0) {
350350
// calculates kinematics unless the gesture starts or ends
351351
state.velocity = [absoluteDelta[0] / dt, absoluteDelta[1] / dt]
352+
state.timeDelta = dt
352353
}
353354
}
354355
}

‎packages/core/src/types/state.ts

+4
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ export type CommonGestureState = {
162162
* Elapsed time (ms) of the current gesture.
163163
*/
164164
elapsedTime: number
165+
/**
166+
* Time delta (ms) with the previous event.
167+
*/
168+
timeDelta: number
165169
/**
166170
* Event type.
167171
*/

0 commit comments

Comments
 (0)
Please sign in to comment.