Skip to content

Commit 6f4c09b

Browse files
Andaristdbismut
andauthoredOct 30, 2022
fix(pinch): cap the movement of wheel events based on the provided bounds (#549)
* fix(pinch): cap the movemenet of wheel events based on the provided bounds * chore: minor refactor in helper function Co-authored-by: David Bismut <david.bismut@gmail.com>
1 parent ec69119 commit 6f4c09b

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed
 

‎.changeset/curvy-pens-laugh.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@use-gesture/core': patch
3+
---
4+
5+
fix: rolls back wheel-based pinch movement to bounds (thanks [@Andarist](https://github.com/Andarist)!)

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

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Engine } from './Engine'
22
import { touchDistanceAngle, distanceAngle, wheelValues } from '../utils/events'
33
import { V } from '../utils/maths'
44
import { Vector2, WebKitGestureEvent } from '../types'
5+
import { clampStateInternalMovementToBounds } from '../utils/state'
56

67
const SCALE_ANGLE_RATIO_INTENT_DEG = 30
78
const PINCH_WHEEL_RATIO = 100
@@ -265,6 +266,9 @@ export class PinchEngine extends Engine<'pinch'> {
265266
state._delta = [(-wheelValues(event)[1] / PINCH_WHEEL_RATIO) * state.offset[0], 0]
266267
V.addTo(state._movement, state._delta)
267268

269+
// _movement rolls back to when it passed the bounds.
270+
clampStateInternalMovementToBounds(state)
271+
268272
this.state.origin = [event.clientX, event.clientY]
269273

270274
this.compute(event)

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

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { CoordinatesEngine } from './CoordinatesEngine'
22
import { wheelValues } from '../utils/events'
33
import { V } from '../utils/maths'
4+
import { clampStateInternalMovementToBounds } from '../utils/state'
45

56
export interface WheelEngine extends CoordinatesEngine<'wheel'> {
67
wheel(this: WheelEngine, event: WheelEvent): void
@@ -23,17 +24,7 @@ export class WheelEngine extends CoordinatesEngine<'wheel'> {
2324
V.addTo(state._movement, state._delta)
2425

2526
// _movement rolls back to when it passed the bounds.
26-
const [ox, oy] = state.overflow
27-
const [dx, dy] = state._delta
28-
const [dirx, diry] = state._direction
29-
30-
if ((ox < 0 && dx > 0 && dirx < 0) || (ox > 0 && dx < 0 && dirx > 0)) {
31-
state._movement[0] = state._movementBound[0] as number
32-
}
33-
34-
if ((oy < 0 && dy > 0 && diry < 0) || (oy > 0 && dy < 0 && diry > 0)) {
35-
state._movement[1] = state._movementBound[1] as number
36-
}
27+
clampStateInternalMovementToBounds(state)
3728

3829
this.compute(event)
3930
this.emit()

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

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { CommonGestureState } from '../types'
2+
3+
// _movement rolls back to when it passed the bounds.
4+
/**
5+
* @note code is currently used in WheelEngine and PinchEngine.
6+
*/
7+
export function clampStateInternalMovementToBounds(state: CommonGestureState) {
8+
const [ox, oy] = state.overflow
9+
const [dx, dy] = state._delta
10+
const [dirx, diry] = state._direction
11+
12+
if ((ox < 0 && dx > 0 && dirx < 0) || (ox > 0 && dx < 0 && dirx > 0)) {
13+
state._movement[0] = state._movementBound[0] as number
14+
}
15+
16+
if ((oy < 0 && dy > 0 && diry < 0) || (oy > 0 && dy < 0 && diry > 0)) {
17+
state._movement[1] = state._movementBound[1] as number
18+
}
19+
}

0 commit comments

Comments
 (0)
Please sign in to comment.