software-mansion / react-native-gesture-handler

Declarative API exposing platform native touch and gesture system to React Native.
https://docs.swmansion.com/react-native-gesture-handler/
MIT License
6.14k stars 982 forks source link

[Web] Send relative coords in event. #2944

Closed m-bert closed 5 months ago

m-bert commented 5 months ago

Description

Changes made in #2938 and #2939 allowed us to calculate x and y relative to given View with the same method on PointerEvents and TouchEvents. This PR changes logic in GestureHandler base class, so that now it sends relative coordinates calculated by tracker.

This change, combined with the one from #2943, makes handlers send correct relative coords.

[!NOTE] New coords may not be correct if the view is rotated, or some of its ancestors are scaled. These cases will be handled in different PRs.

Test plan

Copy snippet below and paste it into transformNativeEvent in GestureHandler.ts

Logs snippet ```jsx const rect = this.delegate.measureView(); const old = { x: lastCoords.x - rect.pageX, y: lastCoords.y - rect.pageY, }; console.table({ oldX: old.x, oldY: old.y, newX: lastRelativeCoords.x, newY: lastRelativeCoords.y, dx: old.x - lastRelativeCoords.x, dy: old.y - lastRelativeCoords.y, }); ```

Here's example video that shows difference between those calculations:

https://github.com/software-mansion/react-native-gesture-handler/assets/63123542/d3a0575c-3821-4d2a-ac96-a0c3a870a1f5

j-piasecki commented 5 months ago

Actually, don't we want this also for focal and anchor points?

m-bert commented 5 months ago

@j-piasecki well, probably we do, but that's something that I was looking at while investigating transformations example in Update examples PR. I think we can leave it as it is and then check if using relative coords helps in that case (at first I thought it does, but seems like there's more to it)