Confetti
Dependency-free canvas confetti for React: an imperative confetti() helper with origin, angle, spread, gravity, colors and shapes, plus a ConfettiButton that bursts from itself on click. Every burst shares one full-viewport canvas that is created on demand and removed the moment the last piece lands.
- Zero dependencies
- Canvas, DPR aware
- Created on demand
- Promise per burst
- prefers-reduced-motion
The burst starts from the button itself.
"use client";
import { PartyPopper } from "lucide-react";
import { ConfettiButton } from "@/components/dusk/confetti/confetti";
export default function ConfettiDemo() {
return (
<div className="flex flex-col items-center gap-3 text-center">
<ConfettiButton size="lg">
<PartyPopper />
Celebrate
</ConfettiButton>
<p className="text-sm text-white/60">The burst starts from the button itself.</p>
</div>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/confetti.json1. Copy the source into
components/dusk/confetti/confetti.tsx.confetti.tsx "use client"; import * as React from "react"; import { Button, type ButtonProps } from "@/components/ui/button"; export type ConfettiShape = "square" | "circle" | "star" | "ribbon"; export interface ConfettiOptions { /** Where the burst starts, as fractions of the viewport (0–1). Default: the middle, a little below centre. */ origin?: { x?: number; y?: number }; /** Direction of the burst in degrees: 90 is straight up, 0 is right, 180 is left. */ angle?: number; /** How wide the burst fans out, in degrees. */ spread?: number; /** Number of pieces in this burst. */ particleCount?: number; /** Launch speed in px per frame (at 60 fps). */ startVelocity?: number; /** Downward pull per frame. `0` floats, `2` drops fast. */ gravity?: number; /** Air resistance: the share of velocity kept each frame (0–1). */ decay?: number; /** Constant sideways push per frame, like wind. Negative blows left. */ drift?: number; /** Lifetime of each piece in frames (at 60 fps). Pieces fade out over the last third. */ ticks?: number; /** Any CSS colors, including theme variables. */ colors?: string[]; /** Shapes to pick from at random. */ shapes?: ConfettiShape[]; /** Size multiplier for every piece. */ scalar?: number; /** z-index of the shared full-viewport canvas. */ zIndex?: number; /** Skip the burst when the user prefers reduced motion. Leave this on unless the confetti is essential. */ disableForReducedMotion?: boolean; } type Piece = { x: number; y: number; vx: number; vy: number; decay: number; gravity: number; drift: number; size: number; color: string; shape: ConfettiShape; rotation: number; spin: number; /** Phase of the 3D flip; its cosine squashes the piece vertically. */ flip: number; flipSpeed: number; wobble: number; wobbleSpeed: number; age: number; life: number; burst: Burst; }; type Burst = { alive: number; resolve: () => void }; // Muted, desaturated tones so a burst reads as celebration, not neon. const DEFAULT_COLORS = [ "var(--color-primary)", "#e4e4e7", "#a1a1aa", "#9db4cf", "#e3c58f", "#c4b5d9", ]; const DEFAULT_SHAPES: ConfettiShape[] = ["square", "circle", "ribbon"]; // One canvas and one animation loop are shared by every burst on the page. let canvas: HTMLCanvasElement | null = null; let ctx: CanvasRenderingContext2D | null = null; let raf = 0; let last = 0; let viewW = 0; let viewH = 0; let pieces: Piece[] = []; const colorCache = new Map<string, string>(); function prefersReducedMotion() { return typeof window !== "undefined" && window.matchMedia("(prefers-reduced-motion: reduce)").matches; } /** Turns any CSS color (variables, oklch…) into something the canvas understands. Cached per color. */ function resolveColor(color: string): string { const cached = colorCache.get(color); if (cached) return cached; let resolved = color; if (color.includes("var(")) { const probe = document.createElement("span"); probe.style.color = color; probe.style.display = "none"; document.body.appendChild(probe); resolved = getComputedStyle(probe).color || "#ffffff"; probe.remove(); } colorCache.set(color, resolved); return resolved; } function resize() { if (!canvas || !ctx) return; const dpr = Math.min(window.devicePixelRatio || 1, 2); viewW = window.innerWidth; viewH = window.innerHeight; canvas.width = Math.round(viewW * dpr); canvas.height = Math.round(viewH * dpr); ctx.setTransform(dpr, 0, 0, dpr, 0, 0); } function mount(zIndex: number) { if (canvas) { canvas.style.zIndex = String(zIndex); return true; } const el = document.createElement("canvas"); const context = el.getContext("2d"); if (!context) return false; el.setAttribute("aria-hidden", "true"); el.dataset.slot = "confetti-canvas"; Object.assign(el.style, { position: "fixed", inset: "0", width: "100%", height: "100%", pointerEvents: "none", zIndex: String(zIndex), }); document.body.appendChild(el); canvas = el; ctx = context; resize(); window.addEventListener("resize", resize); return true; } function unmount() { cancelAnimationFrame(raf); raf = 0; last = 0; window.removeEventListener("resize", resize); canvas?.remove(); canvas = null; ctx = null; } function drawStar(c: CanvasRenderingContext2D, r: number) { c.beginPath(); for (let i = 0; i < 10; i++) { const a = (i * Math.PI) / 5 - Math.PI / 2; const rr = i % 2 === 0 ? r : r * 0.45; c.lineTo(Math.cos(a) * rr, Math.sin(a) * rr); } c.closePath(); c.fill(); } function drawPiece(c: CanvasRenderingContext2D, p: Piece) { const fadeStart = p.life * 0.66; const alpha = p.age < fadeStart ? 1 : Math.max(0, 1 - (p.age - fadeStart) / (p.life - fadeStart)); c.save(); c.globalAlpha = alpha; c.fillStyle = p.color; c.translate(p.x + Math.cos(p.wobble) * p.size * 0.6, p.y); c.rotate(p.rotation); // A squash between -1 and 1 reads as the piece tumbling through the air. c.scale(1, Math.cos(p.flip)); const s = p.size; switch (p.shape) { case "circle": c.beginPath(); c.ellipse(0, 0, s * 0.5, s * 0.5, 0, 0, Math.PI * 2); c.fill(); break; case "star": drawStar(c, s * 0.7); break; case "ribbon": c.fillRect(-s * 0.2, -s * 0.9, s * 0.4, s * 1.8); break; default: c.fillRect(-s * 0.5, -s * 0.5, s, s * 0.7); } c.restore(); } function frame(now: number) { if (!ctx) return; // Physics is tuned per 60 fps frame; scale by elapsed time so 120 Hz screens don't run double speed. const dt = last ? Math.min((now - last) / (1000 / 60), 3) : 1; last = now; ctx.clearRect(0, 0, viewW, viewH); const next: Piece[] = []; for (const p of pieces) { p.age += dt; const drag = Math.pow(p.decay, dt); p.vx *= drag; p.vy = p.vy * drag + p.gravity * dt; p.x += (p.vx + p.drift) * dt; p.y += p.vy * dt; p.rotation += p.spin * dt; p.flip += p.flipSpeed * dt; p.wobble += p.wobbleSpeed * dt; if (p.age >= p.life || p.y > viewH + 40) { p.burst.alive -= 1; if (p.burst.alive === 0) p.burst.resolve(); continue; } drawPiece(ctx, p); next.push(p); } pieces = next; if (pieces.length === 0) { unmount(); return; } raf = requestAnimationFrame(frame); } /** * Fires a burst of confetti from a point on the screen. Safe to call as often * as you like: every burst shares one fixed, full-viewport canvas that is * created on demand and removed as soon as the last piece settles. * * Resolves when this burst has finished. Does nothing on the server, and * (by default) when the user prefers reduced motion. */ export function confetti(options: ConfettiOptions = {}): Promise<void> { const { origin, angle = 90, spread = 60, particleCount = 80, startVelocity = 22, gravity = 0.55, decay = 0.93, drift = 0, ticks = 200, colors = DEFAULT_COLORS, shapes = DEFAULT_SHAPES, scalar = 1, zIndex = 100, disableForReducedMotion = true, } = options; if (typeof window === "undefined") return Promise.resolve(); if (disableForReducedMotion && prefersReducedMotion()) return Promise.resolve(); const count = Math.max(0, Math.round(particleCount)); if (count === 0 || !mount(zIndex)) return Promise.resolve(); const palette = (colors.length ? colors : DEFAULT_COLORS).map(resolveColor); const shapeList = shapes.length ? shapes : DEFAULT_SHAPES; const ox = (origin?.x ?? 0.5) * viewW; const oy = (origin?.y ?? 0.6) * viewH; const base = (angle * Math.PI) / 180; const fan = (spread * Math.PI) / 180; return new Promise<void>((resolve) => { const burst: Burst = { alive: count, resolve }; for (let i = 0; i < count; i++) { // Randomness only ever runs in event handlers, never during render. const dir = base + (Math.random() - 0.5) * fan; const speed = startVelocity * (0.55 + Math.random() * 0.45); pieces.push({ x: ox, y: oy, vx: Math.cos(dir) * speed, vy: -Math.sin(dir) * speed, decay, gravity, drift, size: (6 + Math.random() * 5) * scalar, color: palette[Math.floor(Math.random() * palette.length)], shape: shapeList[Math.floor(Math.random() * shapeList.length)], rotation: Math.random() * Math.PI * 2, spin: (Math.random() - 0.5) * 0.3, flip: Math.random() * Math.PI * 2, flipSpeed: 0.08 + Math.random() * 0.12, wobble: Math.random() * Math.PI * 2, wobbleSpeed: 0.05 + Math.random() * 0.08, age: 0, life: ticks * (0.8 + Math.random() * 0.4), burst, }); } if (!raf) raf = requestAnimationFrame(frame); }); } /** Clears every piece on screen immediately and removes the canvas. */ confetti.reset = () => { for (const p of pieces) { p.burst.alive -= 1; if (p.burst.alive === 0) p.burst.resolve(); } pieces = []; if (typeof window !== "undefined") unmount(); }; /** The centre of an element as a viewport-relative `origin` for `confetti()`. */ export function originFromElement(element: Element): { x: number; y: number } { const rect = element.getBoundingClientRect(); return { x: (rect.left + rect.width / 2) / window.innerWidth, y: (rect.top + rect.height / 2) / window.innerHeight, }; } export interface ConfettiButtonProps extends ButtonProps { /** Options for the burst. The origin defaults to the centre of the button. */ options?: ConfettiOptions; } /** * A DuskUI Button that bursts confetti from itself when clicked. The click * handler still runs as normal; call `event.preventDefault()` in `onClick` * to skip the burst. No confetti under reduced motion. */ export function ConfettiButton({ options, onClick, loading, ...props }: ConfettiButtonProps) { const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => { onClick?.(event); if (event.defaultPrevented || loading) return; confetti({ ...options, origin: options?.origin ?? originFromElement(event.currentTarget) }); }; return <Button loading={loading} onClick={handleClick} {...props} />; }
The CLI also installs the DuskUI Button for ConfettiButton. The confetti() helper itself has no dependencies and can be called from any client-side code.
Usage
"use client"
import { ConfettiButton, confetti } from "@/components/dusk/confetti/confetti"
export function Success() {
return <ConfettiButton>Celebrate</ConfettiButton>
}
// Or fire it from anywhere, e.g. after a save resolves:
async function onSaved() {
await confetti({ particleCount: 120, spread: 80, origin: { y: 0.7 } })
}Examples
Side cannons
Call confetti() twice with mirrored angles and origins at the screen edges. origin is a fraction of the viewport.
Two cannons from the bottom corners of the screen.
"use client";
import { Rocket } from "lucide-react";
import { confetti } from "@/components/dusk/confetti/confetti";
import { Button } from "@/components/ui/button";
export default function ConfettiSideCannons() {
const fire = () => {
const shared = { particleCount: 70, spread: 55, startVelocity: 30, ticks: 240 };
confetti({ ...shared, angle: 60, origin: { x: 0, y: 0.85 } });
confetti({ ...shared, angle: 120, origin: { x: 1, y: 0.85 } });
};
return (
<div className="flex flex-col items-center gap-3 text-center">
<Button variant="outline" size="lg" onClick={fire}>
<Rocket />
Ship it
</Button>
<p className="text-sm text-white/60">Two cannons from the bottom corners of the screen.</p>
</div>
);
}Fireworks
A spread of 360 makes a ring; a handful fired on an interval with low gravity reads as fireworks. The interval is cleared on unmount.
Six 360° bursts at random spots, fired from a click handler.
"use client";
import * as React from "react";
import { Sparkles } from "lucide-react";
import { confetti } from "@/components/dusk/confetti/confetti";
import { Button } from "@/components/ui/button";
export default function ConfettiFireworks() {
const timer = React.useRef(0);
const [running, setRunning] = React.useState(false);
React.useEffect(() => () => window.clearInterval(timer.current), []);
const launch = () => {
window.clearInterval(timer.current);
setRunning(true);
let shots = 0;
timer.current = window.setInterval(() => {
// Full circle bursts that float down slowly, high on the screen.
confetti({
particleCount: 45,
spread: 360,
startVelocity: 18,
gravity: 0.35,
decay: 0.92,
ticks: 160,
shapes: ["circle", "star"],
scalar: 0.8,
origin: { x: 0.15 + Math.random() * 0.7, y: 0.15 + Math.random() * 0.3 },
});
shots += 1;
if (shots >= 6) {
window.clearInterval(timer.current);
setRunning(false);
}
}, 320);
};
return (
<div className="flex flex-col items-center gap-3 text-center">
<Button variant="soft" size="lg" onClick={launch} aria-busy={running || undefined}>
<Sparkles />
Fireworks
</Button>
<p className="text-sm text-white/60">Six 360° bursts at random spots, fired from a click handler.</p>
</div>
);
}Shapes, colors & physics
Pick from square, circle, star and ribbon, pass any CSS colors (theme variables included), and tune gravity, drift and scalar. ConfettiButton takes them through options.
"use client";
import { Star } from "lucide-react";
import { ConfettiButton } from "@/components/dusk/confetti/confetti";
export default function ConfettiCustom() {
return (
<div className="flex flex-wrap items-center justify-center gap-3">
<ConfettiButton
variant="outline"
options={{ shapes: ["star"], colors: ["#e3c58f", "#f1e2c2", "#e4e4e7"], scalar: 1.3, particleCount: 50 }}
>
<Star />
Gold stars
</ConfettiButton>
<ConfettiButton
variant="secondary"
options={{ shapes: ["ribbon"], colors: ["#c4b5d9", "#9db4cf", "#d4d4d8"], spread: 100, gravity: 0.3, drift: 0.6 }}
>
Ribbons in the wind
</ConfettiButton>
<ConfettiButton
variant="ghost"
options={{ angle: 270, spread: 120, startVelocity: 10, gravity: 0.8, particleCount: 40 }}
>
Drop it
</ConfettiButton>
</div>
);
}API Reference
confetti(options) returns a promise that resolves when that burst has settled; confetti.reset() clears the screen. ConfettiButton accepts every DuskUI Button prop plus options; calling event.preventDefault() in its onClick skips the burst. originFromElement(el) turns an element into an origin.
| Prop | Type | Default | Description |
|---|---|---|---|
| origin | { x?: number; y?: number } | { x: 0.5, y: 0.6 } | Start point as fractions of the viewport. ConfettiButton defaults it to its own centre. |
| angle | number | 90 | Launch direction in degrees. 90 is up, 0 is right, 270 is down. |
| spread | number | 60 | Width of the fan in degrees. 360 is a full ring. |
| particleCount | number | 80 | Pieces in the burst. |
| startVelocity | number | 22 | Launch speed in px per 60 fps frame. |
| gravity | number | 0.55 | Downward pull per frame. |
| decay | number | 0.93 | Share of velocity kept each frame (air resistance). |
| drift | number | 0 | Constant sideways push, like wind. |
| ticks | number | 200 | Lifetime in frames. Pieces fade over the last third. |
| colors | string[] | primary plus muted zinc, blue, sand and lilac | Any CSS colors, including var(--…). |
| shapes | ("square" | "circle" | "star" | "ribbon")[] | ["square", "circle", "ribbon"] | Shapes picked at random. |
| scalar | number | 1 | Size multiplier. |
| zIndex | number | 100 | z-index of the shared canvas. |
| disableForReducedMotion | boolean | true | Skip the burst when the user prefers reduced motion. |
Accessibility
- The canvas is aria-hidden, ignores the pointer and is removed from the DOM as soon as the last piece settles, so it never blocks clicks or lingers in the accessibility tree.
- With prefers-reduced-motion nothing is drawn at all. The button and your click handler work exactly the same.
- Confetti is decoration: pair it with a visible or announced success message rather than relying on it to communicate the result.
- Physics is time-based, so it runs at the same speed on 60 Hz and 120 Hz screens, and the canvas is sharp on high-DPI displays.