$ git show chart-scrub
Price chart scrub
Drag across a line chart; a cursor follows your finger and the price label updates on the UI thread.
Inspired by CoinbaseFrom 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.
// path built with d3-shape: line().x(...).y(...).curve(curveBasis)(points)
const x = useSharedValue(WIDTH);
const pan = Gesture.Pan()
.onBegin(e => { x.value = clamp(e.x, 0, WIDTH); })
.onUpdate(e => { x.value = clamp(e.x, 0, WIDTH); })
.onEnd(() => { x.value = withTiming(WIDTH); });
const cursor = useAnimatedStyle(() => ({ transform: [{ translateX: x.value }] }));
const price = useDerivedValue(() => {
const i = Math.round((x.value / WIDTH) * (points.length - 1));
return '$' + points[i].value.toFixed(2);
});
// <ReText text={price} /> from react-native-redash (animated TextInput, no re-renders)
// dot on the curve: translateY = getYForX(parsePath(d), x.value) from react-native-redash