-
-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathuseTouchMove.ts
More file actions
150 lines (127 loc) · 4.48 KB
/
useTouchMove.ts
File metadata and controls
150 lines (127 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import * as React from 'react';
import { useRef, useState } from 'react';
type TouchEventHandler = (e: TouchEvent) => void;
type WheelEventHandler = (e: WheelEvent) => void;
const MIN_SWIPE_DISTANCE = 0.1;
const STOP_SWIPE_DISTANCE = 0.01;
const REFRESH_INTERVAL = 20;
const SPEED_OFF_MULTIPLE = 0.995 ** REFRESH_INTERVAL;
// ================================= Hook =================================
export default function useTouchMove(
ref: React.RefObject<HTMLDivElement>,
onOffset: (offsetX: number, offsetY: number) => boolean,
) {
const [touchPosition, setTouchPosition] = useState<{ x: number; y: number }>();
const [lastTimestamp, setLastTimestamp] = useState<number>(0);
const [lastTimeDiff, setLastTimeDiff] = useState<number>(0);
const [lastOffset, setLastOffset] = useState<{ x: number; y: number }>();
const motionRef = useRef<number>();
// ========================= Events =========================
// >>> Touch events
function onTouchStart(e: TouchEvent) {
const { screenX, screenY } = e.touches[0];
setTouchPosition({ x: screenX, y: screenY });
window.clearInterval(motionRef.current);
}
function onTouchMove(e: TouchEvent) {
if (!touchPosition) return;
// e.preventDefault();
const { screenX, screenY } = e.touches[0];
setTouchPosition({ x: screenX, y: screenY });
const offsetX = screenX - touchPosition.x;
const offsetY = screenY - touchPosition.y;
onOffset(offsetX, offsetY);
const now = Date.now();
setLastTimestamp(now);
setLastTimeDiff(now - lastTimestamp);
setLastOffset({ x: offsetX, y: offsetY });
}
function onTouchEnd() {
if (!touchPosition) return;
setTouchPosition(null);
setLastOffset(null);
// Swipe if needed
if (lastOffset) {
const distanceX = lastOffset.x / lastTimeDiff;
const distanceY = lastOffset.y / lastTimeDiff;
const absX = Math.abs(distanceX);
const absY = Math.abs(distanceY);
// Skip swipe if low distance
if (Math.max(absX, absY) < MIN_SWIPE_DISTANCE) return;
let currentX = distanceX;
let currentY = distanceY;
motionRef.current = window.setInterval(() => {
if (Math.abs(currentX) < STOP_SWIPE_DISTANCE && Math.abs(currentY) < STOP_SWIPE_DISTANCE) {
window.clearInterval(motionRef.current);
return;
}
currentX *= SPEED_OFF_MULTIPLE;
currentY *= SPEED_OFF_MULTIPLE;
onOffset(currentX * REFRESH_INTERVAL, currentY * REFRESH_INTERVAL);
}, REFRESH_INTERVAL);
}
}
// >>> Wheel event
const lastWheelDirectionRef = useRef<'x' | 'y'>();
function onWheel(e: WheelEvent) {
const { deltaX, deltaY } = e;
// Convert both to x & y since wheel only happened on PC
let mixed: number = 0;
const absX = Math.abs(deltaX);
const absY = Math.abs(deltaY);
if (absX === absY) {
mixed = lastWheelDirectionRef.current === 'x' ? deltaX : deltaY;
} else if (absX > absY) {
mixed = deltaX;
lastWheelDirectionRef.current = 'x';
} else {
mixed = deltaY;
lastWheelDirectionRef.current = 'y';
}
if (onOffset(-mixed, -mixed)) {
e.preventDefault();
}
}
// ========================= Effect =========================
const touchEventsRef = useRef<{
onTouchStart: TouchEventHandler;
onTouchMove: TouchEventHandler;
onTouchEnd: TouchEventHandler;
onWheel: WheelEventHandler;
}>(null);
touchEventsRef.current = { onTouchStart, onTouchMove, onTouchEnd, onWheel };
React.useEffect(() => {
const abortController = new AbortController();
function onProxyTouchStart(e: TouchEvent) {
touchEventsRef.current.onTouchStart(e);
}
function onProxyTouchMove(e: TouchEvent) {
touchEventsRef.current.onTouchMove(e);
}
function onProxyTouchEnd(e: TouchEvent) {
touchEventsRef.current.onTouchEnd(e);
}
function onProxyWheel(e: WheelEvent) {
touchEventsRef.current.onWheel(e);
}
document.addEventListener('touchmove', onProxyTouchMove, {
passive: false,
signal: abortController.signal,
});
document.addEventListener('touchend', onProxyTouchEnd, {
passive: true,
signal: abortController.signal,
});
ref.current.addEventListener('touchstart', onProxyTouchStart, {
passive: true,
signal: abortController.signal,
});
ref.current.addEventListener('wheel', onProxyWheel, {
passive: false,
signal: abortController.signal,
});
return () => {
abortController.abort();
};
}, []);
}