Confetti
Deterministic confetti burst for Remotion — seeded particles with gravity, spin, and flutter so every render is identical. Fire it on any frame as a celebratory overlay.
Source
components/kuchi/confetti.tsx
1"use client";23import {4 AbsoluteFill,5 interpolate,6 useCurrentFrame,7 useVideoConfig,8} from "remotion";910export interface ConfettiProps {11 /** Number of confetti pieces. */12 particleCount?: number;13 colors?: string[];14 /** Burst origin as a 0..1 fraction of the canvas. */15 originX?: number;16 originY?: number;17 /** Frame the burst fires on. */18 startFrame?: number;19 /** Lifetime of the burst, in frames. */20 lifetime?: number;21 /** Initial launch speed (px/frame at the 720px reference height). */22 power?: number;23 /** Downward acceleration (px/frame² at the 720px reference height). */24 gravity?: number;25 /** Base piece size in reference px. */26 size?: number;27 /** PRNG seed — same seed renders an identical burst every time. */28 seed?: number;29}3031export interface Particle {32 angle: number;33 speed: number;34 color: string;35 size: number;36 rot0: number;37 spin: number;38 drift: number;39}4041const DEFAULT_COLORS = [42 "#1d9bf0",43 "#ff5da2",44 "#ffd23f",45 "#22c55e",46 "#a855f7",47 "#ff7a45",48];4950// --- Pure helpers (unit-tested) -------------------------------------------5152/** Deterministic PRNG (mulberry32) — returns a function yielding [0, 1). */53export function mulberry32(seed: number): () => number {54 let a = seed >>> 0;55 return () => {56 a = (a + 0x6d2b79f5) | 0;57 let t = Math.imul(a ^ (a >>> 15), 1 | a);58 t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;59 return ((t ^ (t >>> 14)) >>> 0) / 4294967296;60 };61}6263/** Build a deterministic particle set for a seed (stable across renders). */64export function makeParticles({65 count,66 seed,67 colors,68}: {69 count: number;70 seed: number;71 colors: string[];72}): Particle[] {73 const rand = mulberry32(seed);74 const palette = colors.length > 0 ? colors : DEFAULT_COLORS;75 const out: Particle[] = [];76 for (let i = 0; i < count; i++) {77 const angle = rand() * Math.PI * 2;78 const speed = 0.35 + 0.65 * rand();79 out.push({80 angle,81 speed,82 color: palette[Math.floor(rand() * palette.length)],83 size: 0.7 + 0.6 * rand(),84 rot0: rand() * Math.PI * 2,85 spin: (rand() - 0.5) * 0.6,86 drift: (rand() - 0.5) * 2,87 });88 }89 return out;90}9192/** Opacity envelope over a particle's lifetime: quick fade-in, slow fade-out. */93export function particleOpacity(tt: number, lifetime: number): number {94 if (tt < 0 || tt > lifetime) return 0;95 const fadeIn = interpolate(tt, [0, 4], [0, 1], {96 extrapolateLeft: "clamp",97 extrapolateRight: "clamp",98 });99 const fadeOut = interpolate(tt, [lifetime * 0.7, lifetime], [1, 0], {100 extrapolateLeft: "clamp",101 extrapolateRight: "clamp",102 });103 return Math.min(fadeIn, fadeOut);104}105106// --- Component -------------------------------------------------------------107108export function Confetti({109 particleCount = 140,110 colors = DEFAULT_COLORS,111 originX = 0.5,112 originY = 0.5,113 startFrame = 0,114 lifetime = 90,115 power = 17,116 gravity = 0.45,117 size = 13,118 seed = 1,119}: ConfettiProps) {120 const frame = useCurrentFrame();121 const { width, height } = useVideoConfig();122123 const tt = frame - startFrame;124 if (tt < 0 || tt > lifetime) return null;125126 const sc = height / 720;127 const ox = width * originX;128 const oy = height * originY;129 const particles = makeParticles({ count: particleCount, seed, colors });130131 return (132 <AbsoluteFill style={{ pointerEvents: "none" }}>133 {particles.map((p, i) => {134 const opacity = particleOpacity(tt, lifetime);135 if (opacity <= 0) return null;136137 const vx = Math.cos(p.angle) * p.speed * power + p.drift;138 const vy = Math.sin(p.angle) * p.speed * power;139 const x = ox + vx * tt * sc;140 const y = oy + (vy * tt + 0.5 * gravity * tt * tt) * sc;141 const rot = p.rot0 + p.spin * tt;142 const flutter = Math.cos(p.rot0 + p.spin * tt * 1.6);143 const w = size * p.size * sc;144145 return (146 <div147 key={i}148 style={{149 position: "absolute",150 left: x,151 top: y,152 width: w,153 height: w * 0.62,154 borderRadius: 2,155 background: p.color,156 opacity,157 translate: "-50% -50%",158 rotate: `${rot}rad`,159 scale: `${flutter} 1`,160 }}161 />162 );163 })}164 </AbsoluteFill>165 );166}

