Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getRelativeCoords computing #5926

Merged
merged 9 commits into from
Apr 25, 2024
Merged

Conversation

szydlovsky
Copy link
Contributor

Summary

Inspired by #5810, seems like getRelativeCoords wrongly computes relative cords. For some reason, an absolute point was being compared to relative cords of given component (x and y from measure instead of pageX and pageY).

Test plan

You can run the following code (start a pan gesture from the pink square and the coords are computed at the point of gesture end relatively to the purple square - see console logs):

Code
import React from 'react';
import Animated, {
  useAnimatedRef,
  getRelativeCoords,
} from 'react-native-reanimated';
import { View, StyleSheet } from 'react-native';
import { Gesture, GestureDetector } from 'react-native-gesture-handler';

export default function EmptyExample() {
  const aref = useAnimatedRef();

  const panGesture = Gesture.Pan().onEnd((event) => {
    const coords = getRelativeCoords(aref, event.absoluteX, event.absoluteY);
    console.log(coords);
  });

  return (
    <View style={styles.container}>
      <Animated.View
        style={[styles.parentView, { backgroundColor: 'green' }]}
      />
      <Animated.View ref={aref} style={styles.parentView}>
        <GestureDetector gesture={panGesture}>
          <Animated.View style={styles.box} />
        </GestureDetector>
      </Animated.View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  parentView: {
    width: 200,
    height: 200,
    backgroundColor: 'purple',
    justifyContent: 'center',
    alignItems: 'center',
  },
  box: { width: 100, height: 100, backgroundColor: 'pink' },
});

@szydlovsky
Copy link
Contributor Author

Runtime tests coming soon ™️

@szydlovsky szydlovsky requested review from tjzel and Latropos and removed request for tomekzaw April 24, 2024 16:19
Copy link
Contributor

@Latropos Latropos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work, I've left some type suggestions!

@szydlovsky szydlovsky added this pull request to the merge queue Apr 25, 2024
Merged via the queue into main with commit 28618e4 Apr 25, 2024
8 checks passed
@szydlovsky szydlovsky deleted the @szydlovsky/fix-getRelativeCoords branch April 25, 2024 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

getRelativeCoords not work correctly on android
2 participants