|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useRef } from "react"; |
| 4 | + |
| 5 | +// Design-token celebration palette (dark theme literals — pragmatic for canvas, |
| 6 | +// where CSS vars aren't directly paintable): mint accent + accent-soft, warning, |
| 7 | +// info, and white. |
| 8 | +const COLORS = ["#2dd4bf", "#6ee7d6", "#f5b544", "#5fa8f5", "#ffffff"]; |
| 9 | + |
| 10 | +interface Particle { |
| 11 | + x: number; |
| 12 | + y: number; |
| 13 | + vx: number; |
| 14 | + vy: number; |
| 15 | + size: number; |
| 16 | + rot: number; |
| 17 | + vr: number; |
| 18 | + color: string; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Lightweight one-shot confetti burst on a full-screen canvas. Particles spray |
| 23 | + * up/out from centre, fall under gravity, and fade over ~1.7s, after which the |
| 24 | + * component clears the canvas and unmounts itself. Decorative only: |
| 25 | + * pointer-events-none, aria-hidden, high z-index. Fully gated on |
| 26 | + * prefers-reduced-motion — renders nothing when the user prefers reduced motion. |
| 27 | + */ |
| 28 | +export function Confetti() { |
| 29 | + const canvasRef = useRef<HTMLCanvasElement | null>(null); |
| 30 | + const prefersReduced = |
| 31 | + typeof window !== "undefined" && |
| 32 | + window.matchMedia?.("(prefers-reduced-motion: reduce)").matches; |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + if (prefersReduced) return; |
| 36 | + const canvas = canvasRef.current; |
| 37 | + if (!canvas) return; |
| 38 | + const ctx = canvas.getContext("2d"); |
| 39 | + if (!ctx) return; |
| 40 | + |
| 41 | + const dpr = Math.min(window.devicePixelRatio || 1, 2); |
| 42 | + const w = window.innerWidth; |
| 43 | + const h = window.innerHeight; |
| 44 | + canvas.width = w * dpr; |
| 45 | + canvas.height = h * dpr; |
| 46 | + ctx.scale(dpr, dpr); |
| 47 | + |
| 48 | + const cx = w / 2; |
| 49 | + const cy = h * 0.42; |
| 50 | + const count = 140; |
| 51 | + const particles: Particle[] = Array.from({ length: count }, () => { |
| 52 | + const angle = -Math.PI / 2 + (Math.random() - 0.5) * Math.PI * 1.1; |
| 53 | + const speed = 6 + Math.random() * 9; |
| 54 | + return { |
| 55 | + x: cx, |
| 56 | + y: cy, |
| 57 | + vx: Math.cos(angle) * speed, |
| 58 | + vy: Math.sin(angle) * speed, |
| 59 | + size: 4 + Math.random() * 5, |
| 60 | + rot: Math.random() * Math.PI, |
| 61 | + vr: (Math.random() - 0.5) * 0.3, |
| 62 | + color: COLORS[Math.floor(Math.random() * COLORS.length)], |
| 63 | + }; |
| 64 | + }); |
| 65 | + |
| 66 | + const DURATION = 1700; |
| 67 | + const start = performance.now(); |
| 68 | + let raf = 0; |
| 69 | + |
| 70 | + const tick = (now: number) => { |
| 71 | + const elapsed = now - start; |
| 72 | + const progress = elapsed / DURATION; |
| 73 | + ctx.clearRect(0, 0, w, h); |
| 74 | + |
| 75 | + if (progress >= 1) { |
| 76 | + return; // done — leave canvas cleared |
| 77 | + } |
| 78 | + |
| 79 | + const alpha = progress < 0.7 ? 1 : 1 - (progress - 0.7) / 0.3; |
| 80 | + |
| 81 | + for (const p of particles) { |
| 82 | + p.vy += 0.28; // gravity |
| 83 | + p.vx *= 0.99; |
| 84 | + p.x += p.vx; |
| 85 | + p.y += p.vy; |
| 86 | + p.rot += p.vr; |
| 87 | + |
| 88 | + ctx.save(); |
| 89 | + ctx.globalAlpha = Math.max(0, alpha); |
| 90 | + ctx.translate(p.x, p.y); |
| 91 | + ctx.rotate(p.rot); |
| 92 | + ctx.fillStyle = p.color; |
| 93 | + ctx.fillRect(-p.size / 2, -p.size / 2, p.size, p.size * 0.6); |
| 94 | + ctx.restore(); |
| 95 | + } |
| 96 | + |
| 97 | + raf = requestAnimationFrame(tick); |
| 98 | + }; |
| 99 | + |
| 100 | + raf = requestAnimationFrame(tick); |
| 101 | + return () => cancelAnimationFrame(raf); |
| 102 | + }, [prefersReduced]); |
| 103 | + |
| 104 | + if (prefersReduced) return null; |
| 105 | + |
| 106 | + return ( |
| 107 | + <canvas |
| 108 | + ref={canvasRef} |
| 109 | + aria-hidden |
| 110 | + className="pointer-events-none fixed inset-0 z-[96] h-full w-full" |
| 111 | + /> |
| 112 | + ); |
| 113 | +} |
0 commit comments