Skip to content

Commit db0d934

Browse files
authoredApr 6, 2023
Fix/551 (#596)
1 parent 43e751a commit db0d934

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed
 

‎.changeset/hip-ducks-serve.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@use-gesture/core': patch
3+
---
4+
5+
fix: add a try catch when calculating the distance and angle as apparently some events might be undefined on Windows #551

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

+8
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ export class PinchEngine extends Engine<'pinch'> {
9090
state._touchIds = Array.from(ctrlTouchIds).slice(0, 2) as [number, number]
9191

9292
const payload = touchDistanceAngle(event, state._touchIds)
93+
94+
if (!payload) return
9395
this.pinchStart(event, payload)
9496
}
9597

@@ -116,6 +118,8 @@ export class PinchEngine extends Engine<'pinch'> {
116118

117119
// @ts-ignore
118120
const payload = distanceAngle(...Array.from(_pointerEvents.values()))
121+
122+
if (!payload) return
119123
this.pinchStart(event, payload)
120124
}
121125

@@ -132,6 +136,8 @@ export class PinchEngine extends Engine<'pinch'> {
132136
touchMove(event: TouchEvent) {
133137
if (!this.state._active) return
134138
const payload = touchDistanceAngle(event, this.state._touchIds)
139+
140+
if (!payload) return
135141
this.pinchMove(event, payload)
136142
}
137143

@@ -143,6 +149,8 @@ export class PinchEngine extends Engine<'pinch'> {
143149
if (!this.state._active) return
144150
// @ts-ignore
145151
const payload = distanceAngle(...Array.from(_pointerEvents.values()))
152+
153+
if (!payload) return
146154
this.pinchMove(event, payload)
147155
}
148156

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

+14-9
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,20 @@ function getValueEvent<EventType extends TouchEvent | PointerEvent>(
7373
}
7474

7575
export function distanceAngle(P1: Touch | PointerEvent, P2: Touch | PointerEvent) {
76-
const dx = P2.clientX - P1.clientX
77-
const dy = P2.clientY - P1.clientY
78-
const cx = (P2.clientX + P1.clientX) / 2
79-
const cy = (P2.clientY + P1.clientY) / 2
80-
81-
const distance = Math.hypot(dx, dy)
82-
const angle = -(Math.atan2(dx, dy) * 180) / Math.PI
83-
const origin = [cx, cy] as Vector2
84-
return { angle, distance, origin }
76+
// add a try catch
77+
// attempt to fix https://github.com/pmndrs/use-gesture/issues/551
78+
try {
79+
const dx = P2.clientX - P1.clientX
80+
const dy = P2.clientY - P1.clientY
81+
const cx = (P2.clientX + P1.clientX) / 2
82+
const cy = (P2.clientY + P1.clientY) / 2
83+
84+
const distance = Math.hypot(dx, dy)
85+
const angle = -(Math.atan2(dx, dy) * 180) / Math.PI
86+
const origin = [cx, cy] as Vector2
87+
return { angle, distance, origin }
88+
} catch {}
89+
return null
8590
}
8691

8792
export function touchIds(event: TouchEvent) {

0 commit comments

Comments
 (0)
Please sign in to comment.