$ git show spin-wheel
Spinning prize wheel
Tap spin and the wheel whips round and slowly winds down onto a prize, the pointer flicking as each segment passes.
import { scheduleOnRN } from 'react-native-worklets';
// Per-frame ticks need the live angle, so Reanimated 4 keeps a shared value; scheduleOnRN replaces runOnJS
const PRIZES = ['10%', 'Free ship', '5%', 'Try again', '20%', 'Gift'];
const SEG = 360 / PRIZES.length;
const rotation = useSharedValue(0);
const pointer = useSharedValue(0);
const [spinning, setSpinning] = useState(false);
const spin = () => {
if (spinning) return;
setSpinning(true);
const prize = Math.floor(Math.random() * PRIZES.length); // or the result from your server
const jitter = (Math.random() - 0.5) * SEG * 0.7; // don't always stop dead center
const current = rotation.value % 360;
// segment i sits at i * SEG clockwise from the top, so rotate it back under the pointer
const target = rotation.value - current + 360 * 6 + (360 - (prize * SEG + SEG / 2)) + jitter;
rotation.value = withTiming(target, { duration: 4500, easing: Easing.bezier(0.15, 0.75, 0.2, 1) }, (done) => {
if (done) scheduleOnRN(finish, prize);
});
};
const finish = (prize) => { setSpinning(false); onWin(PRIZES[prize]); };
// Each time a segment edge passes the pointer: flick the pointer and tick a haptic
useAnimatedReaction(
() => Math.floor(rotation.value / SEG),
(edge, prev) => {
if (prev === null || edge === prev) return;
pointer.value = withSequence(withTiming(-18, { duration: 40 }), withSpring(0, { damping: 8, stiffness: 400 }));
scheduleOnRN(Haptics.selectionAsync);
},
);
const wheel = useAnimatedStyle(() => ({ transform: [{ rotate: rotation.value + 'deg' }] }));
const flap = useAnimatedStyle(() => ({ transform: [{ rotate: pointer.value + 'deg' }] }));
<View style={{ alignItems: 'center' }}>
<Animated.View style={[styles.pointer, { transformOrigin: 'top' }, flap]} /> {/* small triangle, zIndex above the wheel */}
<Animated.View style={[{ width: 260, height: 260 }, wheel]}>
<WheelFace prizes={PRIZES} /> {/* react-native-svg: one Path wedge + Text label per prize */}
</Animated.View>
<Button title={spinning ? 'Spinning…' : 'Spin'} onPress={spin} disabled={spinning} />
</View>