Repository navigation
Commit ce243ef
authored
[Web] Map event coordinates through the view's full transform (#4566)
## Description
Follow-up to #4531.
On web the relative `x`/`y` of an event were computed as `(clientX -
rect.left) / scaleX`, which only holds for a positive, axis-aligned
scale on the view itself. A mirrored view (`scaleX: -1`) reported
negative coordinates, a rotated one divided by zero, and transforms on
ancestors (a zoomed container) were ignored entirely. Native reports
coordinates in the view's own space through the whole hierarchy, so web
disagreed with it in all of those cases.
This replaces `calculateViewScale` with `viewportToLocal`, which inverts
the linear part of the view's transform composed with its ancestors'
around the center of the bounding rect. The center of the rect is where
the view's own center lands under any affine transform, so no
transform-origin or layout position is needed. The untransformed size
comes from `offsetWidth`/`offsetHeight`, or is solved back from the
bounds for SVG elements which have neither.
All four places that mapped coordinates now share it:
`PointerEventManager`, `WheelEventManager`, the hover payload of
`GestureHandlerButton.web` and
`GestureHandlerWebDelegate.absoluteToLocal` (which had its own
`DOMMatrix` version for the pinch focal point and rotation anchor). The
helper also steps into the `display: contents` wrapper the v3 detectors
attach to, so v3 gets the transform of the child the same way the
delegate already did.
Reported coordinates change for views with a transformed ancestor, which
previously received visual pixels. Perspective is not handled.
### Comparison
| Before | After |
|---------|--------|
|
https://github.com/user-attachments/assets/c66772e4-8760-47c2-98c5-36dd6c9ce8de
|
https://github.com/user-attachments/assets/9d8916a3-3970-4114-a79a-2fcbf3ce2729
|
## Test plan
- `yarn test src/web` in the package, new `utils.test.ts` covers scale,
mirror, 90/180 degree rotation, the `scale` property, `matrix3d`, SVG,
nested and rotated ancestors and the contents wrapper.
- Test screen with a box per code path (pointer pan, trackpad pan, hover
gesture, Touchable hover, pinch focal, rotation anchor, v2 pan, SVG
pan), each transformed or inside a transformed parent, with a dot placed
at the reported `x`/`y`. The dot sits under the pointer in every box on
web, as it does on native.
<details>
<summary>Tested on the following code:</summary>
```tsx
import React from 'react';
import type { ViewStyle } from 'react-native';
import { ScrollView, StyleSheet, Text, View } from 'react-native';
import {
Gesture,
GestureDetector,
Touchable,
useHoverGesture,
usePanGesture,
usePinchGesture,
useRotationGesture,
} from 'react-native-gesture-handler';
import Animated, {
useAnimatedStyle,
useSharedValue,
} from 'react-native-reanimated';
import Svg, { Rect } from 'react-native-svg';
// Every box is transformed (or sits inside a transformed parent) and the red
// dot is placed at the coordinates the gesture reports, that is in the box's
// own coordinate space. When the coordinates are mapped correctly the dot sits
// under the pointer in every box, on every platform. Each section exercises a
// different code path of the web coordinate mapping.
const DOT = 16;
const MIRROR: ViewStyle['transform'] = [{ scaleX: -1 }];
function useDot() {
const x = useSharedValue(-DOT);
const y = useSharedValue(-DOT);
const style = useAnimatedStyle(() => ({
transform: [
{ translateX: x.value - DOT / 2 },
{ translateY: y.value - DOT / 2 },
],
}));
return { x, y, style };
}
type BoxProps = {
ref?: React.Ref<React.ComponentRef<typeof View>>;
label: string;
note?: string;
transform?: ViewStyle['transform'];
dotStyle: ReturnType<typeof useDot>['style'];
children?: React.ReactNode;
};
function Box({ ref, label, note, transform, dotStyle, children }: BoxProps) {
return (
<View ref={ref} style={[styles.box, { transform }]}>
<Text style={styles.label}>{label}</Text>
{note && <Text style={styles.note}>{note}</Text>}
{children}
<Animated.View style={[styles.dot, dotStyle]} />
</View>
);
}
function Section({
title,
children,
}: {
title: string;
children: React.ReactNode;
}) {
return (
<View style={styles.section}>
<Text style={styles.title}>{title}</Text>
<View style={styles.row}>{children}</View>
</View>
);
}
// Pointer path: PointerEventManager.
const PAN_BOXES: {
label: string;
transform?: ViewStyle['transform'];
parentTransform?: ViewStyle['transform'];
}[] = [
{ label: 'plain' },
{ label: 'scale 1.5', transform: [{ scale: 1.5 }] },
{ label: 'scaleX -1', transform: MIRROR },
{ label: 'rotate 90', transform: [{ rotate: '90deg' }] },
{ label: 'rotate 180', transform: [{ rotate: '180deg' }] },
{ label: 'rotate 30', transform: [{ rotate: '30deg' }] },
{ label: 'parent scale 1.5', parentTransform: [{ scale: 1.5 }] },
{
label: 'parent rotate 45, scaleX -1',
transform: MIRROR,
parentTransform: [{ rotate: '45deg' }],
},
];
function PanProbe({
label,
transform,
parentTransform,
}: (typeof PAN_BOXES)[number]) {
const dot = useDot();
const pan = usePanGesture({
minDistance: 0,
onBegin: (event) => {
dot.x.value = event.x;
dot.y.value = event.y;
},
onUpdate: (event) => {
dot.x.value = event.x;
dot.y.value = event.y;
},
});
return (
<View style={{ transform: parentTransform }}>
<GestureDetector gesture={pan}>
<Box label={label} transform={transform} dotStyle={dot.style} />
</GestureDetector>
</View>
);
}
// Wheel path: WheelEventManager. The pointer does not move, the manager
// accumulates the scroll deltas from where the cursor sits.
function TrackpadProbe() {
const dot = useDot();
const pan = usePanGesture({
enableTrackpadTwoFingerGesture: true,
onBegin: (event) => {
dot.x.value = event.x;
dot.y.value = event.y;
},
onUpdate: (event) => {
dot.x.value = event.x;
dot.y.value = event.y;
},
});
return (
<GestureDetector gesture={pan}>
<Box
label="trackpad pan, scaleX -1"
note="two-finger swipe, the dot starts under the cursor and follows the swipe"
transform={MIRROR}
dotStyle={dot.style}
/>
</GestureDetector>
);
}
// Pointer path for hover events.
function HoverProbe() {
const dot = useDot();
const hover = useHoverGesture({
onBegin: (event) => {
dot.x.value = event.x;
dot.y.value = event.y;
},
onUpdate: (event) => {
dot.x.value = event.x;
dot.y.value = event.y;
},
});
return (
<GestureDetector gesture={hover}>
<Box
label="hover, scaleX -1"
note="mouse, no press"
transform={MIRROR}
dotStyle={dot.style}
/>
</GestureDetector>
);
}
// Button path: GestureHandlerButton.web hover payload.
function TouchableHoverProbe() {
const dot = useDot();
return (
<Touchable
style={[styles.box, { transform: MIRROR }]}
onHoverIn={(event) => {
dot.x.value = event.x;
dot.y.value = event.y;
}}>
<Text style={styles.label}>Touchable hover, scaleX -1</Text>
<Text style={styles.note}>
mouse, the dot marks where the cursor entered the box
</Text>
<Animated.View style={[styles.dot, dot.style]} />
</Touchable>
);
}
// Delegate path: absoluteToLocal for the Pinch focal point and the Rotation
// anchor.
function PinchProbe() {
const dot = useDot();
const pinch = usePinchGesture({
onUpdate: (event) => {
dot.x.value = event.focalX;
dot.y.value = event.focalY;
},
});
return (
<GestureDetector gesture={pinch}>
<Box
label="pinch focal, scaleX -1"
note="two fingers, the dot sits between them"
transform={MIRROR}
dotStyle={dot.style}
/>
</GestureDetector>
);
}
function RotationProbe() {
const dot = useDot();
const rotation = useRotationGesture({
onUpdate: (event) => {
dot.x.value = event.anchorX;
dot.y.value = event.anchorY;
},
});
return (
<GestureDetector gesture={rotation}>
<Box
label="rotation anchor, rotate 90"
note="two fingers, the dot sits between them"
transform={[{ rotate: '90deg' }]}
dotStyle={dot.style}
/>
</GestureDetector>
);
}
// Legacy API: the handler attaches to the box element itself, not to a
// wrapper.
function LegacyPanProbe() {
const dot = useDot();
const pan = Gesture.Pan()
.minDistance(0)
.onBegin((event) => {
dot.x.value = event.x;
dot.y.value = event.y;
})
.onUpdate((event) => {
dot.x.value = event.x;
dot.y.value = event.y;
});
return (
<GestureDetector gesture={pan}>
<Box label="v2 pan, scaleX -1" transform={MIRROR} dotStyle={dot.style} />
</GestureDetector>
);
}
// SVG elements have no offset size, the mapping recovers it from the bounds.
// The mirror sits on the wrapper so the svg itself stays untransformed.
function SvgProbe() {
const dot = useDot();
const pan = Gesture.Pan()
.minDistance(0)
.onBegin((event) => {
dot.x.value = event.x;
dot.y.value = event.y;
})
.onUpdate((event) => {
dot.x.value = event.x;
dot.y.value = event.y;
});
return (
<View style={[styles.box, { transform: MIRROR }]}>
<GestureDetector gesture={pan}>
<Svg width={BOX} height={BOX}>
<Rect x="0" y="0" width={BOX} height={BOX} fill="#001a72" />
<Rect x="0" y="0" width={BOX / 2} height={BOX / 2} fill="#1a3a9a" />
</Svg>
</GestureDetector>
<Text style={[styles.label, styles.overlay]}>
svg pan, parent scaleX -1
</Text>
<Animated.View style={[styles.dot, dot.style]} />
</View>
);
}
export default function EmptyExample() {
return (
<ScrollView contentContainerStyle={styles.container}>
<Section title="Pan, pointer or touch">
{PAN_BOXES.map((box) => (
<PanProbe key={box.label} {...box} />
))}
</Section>
<Section title="Other paths">
<TrackpadProbe />
<HoverProbe />
<TouchableHoverProbe />
<PinchProbe />
<RotationProbe />
<LegacyPanProbe />
<SvgProbe />
</Section>
</ScrollView>
);
}
const BOX = 120;
const styles = StyleSheet.create({
container: {
padding: 16,
},
section: {
marginBottom: 24,
},
title: {
fontSize: 18,
fontWeight: '600',
marginBottom: 8,
},
row: {
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'center',
},
box: {
width: BOX,
height: BOX,
margin: 40,
backgroundColor: '#001a72',
},
overlay: {
position: 'absolute',
left: 0,
top: 0,
},
label: {
color: 'white',
margin: 8,
},
note: {
color: '#c9d1ff',
fontSize: 10,
marginHorizontal: 8,
},
dot: {
position: 'absolute',
left: 0,
top: 0,
width: DOT,
height: DOT,
borderRadius: DOT / 2,
backgroundColor: '#ff6259',
},
});
```
</details>1 parent ab44f1e commit ce243ef
10 files changed
Lines changed: 557 additions & 99 deletions
File tree
- packages/react-native-gesture-handler/src
- components
- web
- __tests__
- handlers
- __tests__
- tools
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
17 | | - | |
| 16 | + | |
18 | 17 | | |
| 18 | + | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
34 | | - | |
| 34 | + | |
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
41 | | - | |
42 | 40 | | |
43 | 41 | | |
| 42 | + | |
| 43 | + | |
44 | 44 | | |
45 | 45 | | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
53 | 49 | | |
54 | 50 | | |
55 | 51 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
0 commit comments