$ git show joystick
Joystick
A thumbstick you can drag anywhere inside its ring; let go and it springs back to the center.
// Unchanged in Reanimated 4: a pure gesture, all on the UI thread
const R = 50; // base radius minus knob radius
const x = useSharedValue(0), y = useSharedValue(0);
const pan = Gesture.Pan()
.onUpdate((e) => {
const d = Math.hypot(e.translationX, e.translationY);
const k = d > R ? R / d : 1; // scale back onto the circle edge
x.value = e.translationX * k;
y.value = e.translationY * k;
})
.onEnd(() => {
x.value = withSpring(0, { damping: 12, stiffness: 220 });
y.value = withSpring(0, { damping: 12, stiffness: 220 });
});
// Normalized -1..1 direction for your game or camera, read on the UI thread
const direction = useDerivedValue(() => ({ x: x.value / R, y: y.value / R }));
const knob = useAnimatedStyle(() => ({ transform: [{ translateX: x.value }, { translateY: y.value }] }));
<View style={styles.base}> {/* 140 x 140 circle, centered content */}
<GestureDetector gesture={pan}>
<Animated.View style={[styles.knob, knob]} /> {/* 40 x 40 circle */}
</GestureDetector>
</View>