forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseTapGestures.ts
177 lines (149 loc) · 6.31 KB
/
useTapGestures.ts
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* eslint-disable no-param-reassign */
import {useCallback, useMemo} from 'react';
import type {TapGesture} from 'react-native-gesture-handler';
import {Gesture} from 'react-native-gesture-handler';
import {runOnJS, withSpring} from 'react-native-reanimated';
import {DOUBLE_TAP_SCALE, SPRING_CONFIG} from './constants';
import type {MultiGestureCanvasVariables} from './types';
import * as MultiGestureCanvasUtils from './utils';
type UseTapGesturesProps = Pick<
MultiGestureCanvasVariables,
| 'canvasSize'
| 'contentSize'
| 'minContentScale'
| 'maxContentScale'
| 'offsetX'
| 'offsetY'
| 'pinchScale'
| 'zoomScale'
| 'shouldDisableTransformationGestures'
| 'reset'
| 'stopAnimation'
| 'onScaleChanged'
| 'onTap'
>;
const useTapGestures = ({
canvasSize,
contentSize,
minContentScale,
maxContentScale,
offsetX,
offsetY,
pinchScale,
zoomScale,
reset,
stopAnimation,
shouldDisableTransformationGestures,
onScaleChanged,
onTap,
}: UseTapGesturesProps): {singleTapGesture: TapGesture; doubleTapGesture: TapGesture} => {
// The content size after scaling it with minimum scale to fit the content into the canvas
const scaledContentWidth = useMemo(() => contentSize.width * minContentScale, [contentSize.width, minContentScale]);
const scaledContentHeight = useMemo(() => contentSize.height * minContentScale, [contentSize.height, minContentScale]);
// On double tap the content should be zoomed to fill, but at least zoomed by DOUBLE_TAP_SCALE
const doubleTapScale = useMemo(() => Math.max(DOUBLE_TAP_SCALE, maxContentScale / minContentScale), [maxContentScale, minContentScale]);
const zoomToCoordinates = useCallback(
(focalX: number, focalY: number, callback: () => void) => {
'worklet';
stopAnimation();
// By how much the canvas is bigger than the content horizontally and vertically per side
const horizontalCanvasOffset = Math.max(0, (canvasSize.width - scaledContentWidth) / 2);
const verticalCanvasOffset = Math.max(0, (canvasSize.height - scaledContentHeight) / 2);
// We need to adjust the focal point to take into account the canvas offset
// The focal point cannot be outside of the content's bounds
const adjustedFocalPoint = {
x: MultiGestureCanvasUtils.clamp(focalX - horizontalCanvasOffset, 0, scaledContentWidth),
y: MultiGestureCanvasUtils.clamp(focalY - verticalCanvasOffset, 0, scaledContentHeight),
};
// The center of the canvas
const canvasCenter = {
x: canvasSize.width / 2,
y: canvasSize.height / 2,
};
// The center of the content before zooming
const originalContentCenter = {
x: scaledContentWidth / 2,
y: scaledContentHeight / 2,
};
// The size of the content after zooming
const zoomedContentSize = {
width: scaledContentWidth * doubleTapScale,
height: scaledContentHeight * doubleTapScale,
};
// The center of the zoomed content
const zoomedContentCenter = {
x: zoomedContentSize.width / 2,
y: zoomedContentSize.height / 2,
};
// By how much the zoomed content is bigger/smaller than the canvas.
const zoomedContentOffset = {
x: zoomedContentCenter.x - canvasCenter.x,
y: zoomedContentCenter.y - canvasCenter.y,
};
// How much the content needs to be shifted based on the focal point
const shiftingFactor = {
x: adjustedFocalPoint.x / originalContentCenter.x - 1,
y: adjustedFocalPoint.y / originalContentCenter.y - 1,
};
// The offset after applying the focal point adjusted shift.
// We need to invert the shift, because the content is moving in the opposite direction (* -1)
const offsetAfterZooming = {
x: zoomedContentOffset.x * (shiftingFactor.x * -1),
y: zoomedContentOffset.y * (shiftingFactor.y * -1),
};
// If the zoomed content is less tall than the canvas, we need to reset the vertical offset
if (zoomedContentSize.height < canvasSize.height) {
offsetAfterZooming.y = 0;
}
offsetX.set(withSpring(offsetAfterZooming.x, SPRING_CONFIG));
offsetY.set(withSpring(offsetAfterZooming.y, SPRING_CONFIG));
zoomScale.set(withSpring(doubleTapScale, SPRING_CONFIG, callback));
pinchScale.set(doubleTapScale);
},
[stopAnimation, canvasSize.width, canvasSize.height, scaledContentWidth, scaledContentHeight, doubleTapScale, offsetX, offsetY, zoomScale, pinchScale],
);
const doubleTapGesture = Gesture.Tap()
// The first argument is not used, but must be defined
.onTouchesDown((_evt, state) => {
'worklet';
if (!shouldDisableTransformationGestures.get()) {
return;
}
state.fail();
})
.numberOfTaps(2)
.maxDelay(150)
.maxDistance(20)
.onEnd((evt) => {
'worklet';
const triggerScaleChangedEvent = () => {
'worklet';
if (onScaleChanged != null) {
runOnJS(onScaleChanged)(zoomScale.get());
}
};
// If the content is already zoomed, we want to reset the zoom,
// otherwise we want to zoom in
if (zoomScale.get() > 1) {
reset(true, triggerScaleChangedEvent);
} else {
zoomToCoordinates(evt.x, evt.y, triggerScaleChangedEvent);
}
});
const singleTapGesture = Gesture.Tap()
.numberOfTaps(1)
.maxDuration(125)
.onBegin(() => {
'worklet';
stopAnimation();
})
.onFinalize((_evt, success) => {
'worklet';
if (!success || onTap === undefined) {
return;
}
runOnJS(onTap)();
});
return {singleTapGesture, doubleTapGesture};
};
export default useTapGestures;