title: React-native reanimated distance calculation to the View
body: <p>Today I want to make an animation to be determined by the user. In my example is when the user moves the square close and hit the red rectangle for example to write something in console. But i don't know how to calculate distance to the rectangle and I can't determine when the square touched the rectangle? So my code sa far -</p>
  <pre><code>import React from &quot;react&quot;;
  import { StyleSheet, View } from &quot;react-native&quot;;
  import {
    GestureHandlerRootView,
    PanGestureHandler,
    PanGestureHandlerGestureEvent,
  } from &quot;react-native-gesture-handler&quot;;
  import Animated, {
    useAnimatedGestureHandler,
    useAnimatedStyle,
    useSharedValue,
    withSpring,
    withTiming,
  } from &quot;react-native-reanimated&quot;;
  
  const SIZE = 100.0;
  const CIRCLE_RADIUS = SIZE * 2;
  
  type ContextType = {
    translateX: number;
    translateY: number;
    size: number;
  };
  
  export default function App() {
    const translateX = useSharedValue(0);
    const translateY = useSharedValue(0);
    const size = useSharedValue(100);
  
    const panGestureEvent = useAnimatedGestureHandler&lt;
      PanGestureHandlerGestureEvent,
      ContextType
    &gt;({
      onStart: (event, context) =&gt; {
        context.translateX = translateX.value;
        context.translateY = translateY.value;
        size.value = 122;
      },
      onActive: (event, context) =&gt; {
        translateX.value = event.translationX + context.translateX;
        translateY.value = event.translationY + context.translateY;
      },
      onEnd: () =&gt; {
        const distance = Math.sqrt(translateX.value ** 2 + translateY.value ** 2);
        size.value = 100;
  
        if (distance &lt; CIRCLE_RADIUS + SIZE / 2) {
          translateX.value = withSpring(30);
          translateY.value = withSpring(50);
        }
      },
    });
  
    const rStyle = useAnimatedStyle(() =&gt; {
      return {
        transform: [
          {
            translateX: translateX.value,
          },
          {
            translateY: translateY.value,
          },
        ],
        width: withTiming(size.value, { duration: 160 }),
        height: withTiming(size.value, { duration: 160 }),
      };
    });
  
    return (
      &lt;GestureHandlerRootView style={{ flex: 1 }}&gt;
        &lt;View style={styles.container}&gt;
          &lt;View style={styles.circle}&gt;
            &lt;PanGestureHandler onGestureEvent={panGestureEvent}&gt;
              &lt;Animated.View
                style={[
                  {
                    width: size.value,
                    height: size.value,
                    backgroundColor: &quot;rgba(0, 0, 256, 0.5)&quot;,
                    borderRadius: 20,
                  },
                  rStyle,
                ]}
              /&gt;
            &lt;/PanGestureHandler&gt;
          &lt;/View&gt;
          &lt;View
      style={{ width: 30, height: 50, backgroundColor: &quot;red&quot;, top: 80 }}
          &gt;&lt;/View&gt;
        &lt;/View&gt;
      &lt;/GestureHandlerRootView&gt;
    );
  }
  
  const styles = StyleSheet.create({
    container: {
      flex: 1,
      backgroundColor: &quot;#fff&quot;,
      alignItems: &quot;center&quot;,
      justifyContent: &quot;center&quot;,
    },
    circle: {
      width: CIRCLE_RADIUS * 2,
      height: CIRCLE_RADIUS * 2,
      alignItems: &quot;center&quot;,
      justifyContent: &quot;center&quot;,
      borderRadius: CIRCLE_RADIUS,
      borderWidth: 5,
      borderColor: &quot;rgba(0, 0, 256, 0.5)&quot;,
    },
  });
  </code></pre>
  <p><a href="https://i.stack.imgur.com/jiPQl.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jiPQl.gif" alt="enter image description here" /></a></p>