$ git show confetti
Confetti burst
Pieces shoot up from a point, spin, fall under gravity and fade, for wins and completed goals.
// Physics per piece is easiest as a worklet, so this is the same shared-value code in Reanimated 4
const COLORS = ['#f43f5e', '#f59e0b', '#10b981', '#3b82f6', '#a855f7'];
const GRAVITY = 900; // px per second²
const pieces = useMemo(() => Array.from({ length: 40 }, (_, i) => ({
angle: -Math.PI / 2 + (Math.random() - 0.5) * 1.6, // mostly upward
speed: 450 + Math.random() * 350, // px per second
spin: (Math.random() - 0.5) * 1440, // degrees per second
color: COLORS[i % COLORS.length],
})), []);
const t = useSharedValue(0); // seconds since the burst
const fire = () => {
t.value = withSequence(
withTiming(0, { duration: 0 }),
withTiming(1.6, { duration: 1600, easing: Easing.linear }),
);
};
function Piece({ p, t }) {
const style = useAnimatedStyle(() => {
const x = Math.cos(p.angle) * p.speed * t.value;
const y = Math.sin(p.angle) * p.speed * t.value + 0.5 * GRAVITY * t.value * t.value;
return {
opacity: t.value === 0 ? 0 : interpolate(t.value, [0, 1.2, 1.6], [1, 1, 0]),
transform: [{ translateX: x }, { translateY: y }, { rotate: p.spin * t.value + 'deg' }],
};
});
return <Animated.View style={[styles.piece, { backgroundColor: p.color }, style]} />; // 6 x 10
}
{/* Zero-size origin where the burst starts, e.g. the center of the button */}
<View pointerEvents="none" style={styles.origin}>
{pieces.map((p, i) => <Piece key={i} p={p} t={t} />)}
</View>