$ git show circular-slider
Bedtime circular slider
Drag a knob around a circle; the arc and the time label follow the angle.
Inspired by Apple BedtimeFrom William Candillon’s “Can it be done in React Native?” series. The code here is a simplified rewrite for Reanimated 4 and 3.
This snippet uses APIs that didn’t change between versions, so the same code runs on Reanimated 4 and 3.
const angle = useSharedValue(-Math.PI / 2); // radians, 0 = 3 o'clock
const pan = Gesture.Pan().onUpdate(e => {
angle.value = Math.atan2(e.y - R, e.x - R); // e.x/e.y are relative to the wheel
});
const knob = useAnimatedStyle(() => ({
transform: [
{ translateX: R + R * Math.cos(angle.value) - KNOB / 2 },
{ translateY: R + R * Math.sin(angle.value) - KNOB / 2 },
],
}));
const C = 2 * Math.PI * R;
const arc = useAnimatedProps(() => {
const t = ((angle.value + Math.PI / 2 + 2 * Math.PI) % (2 * Math.PI)) / (2 * Math.PI);
return { strokeDashoffset: C * (1 - t) };
});
const hours = useDerivedValue(() => /* map t to 0..24 */ 0);