Confetti on canvas without dependencies. useConfetti returns fire(), which launches a burst from a point or an element. The canvas is created on demand on top of the page with pointer-events: none and removed once the last particle fades. With reduced motion turned on, fire does nothing.
Preview
Press the button. The burst starts from the button itself.
Installation
Usage
import { Button } from "@/components/ui/button"
import { useConfetti } from "@/components/ui/confetti"
export default function Example() {
const { fire } = useConfetti()
return (
<Button onClick={(e) => fire({ origin: e.currentTarget })}>
Celebrate
</Button>
)
}Examples
Presets
preset sets particle count, spread and speed. Any explicit option overrides the preset.
fire({ preset: "low" })
fire({ preset: "high", origin: buttonRef })
fire({ preset: "burst", particleCount: 300 })From an element
origin accepts viewport coordinates, a DOM element or a ref. The burst starts from the element center.
const target = useRef<HTMLDivElement>(null)
const { fire } = useConfetti({ origin: target, preset: "burst" })
<Button onClick={() => fire()}>Fire at the target</Button>
<div ref={target}>Target</div>Side cannons
Two calls with different origin and angle. Bursts share one canvas.
const { fire } = useConfetti({ preset: "high", spread: 55 })
const y = window.innerHeight * 0.7
fire({ origin: { x: 0, y }, angle: 60 })
fire({ origin: { x: window.innerWidth, y }, angle: 120 })On completion
A small burst from a checkbox when a task is done.
import { Checkbox } from "@/components/ui/checkbox"
const { fire } = useConfetti({ preset: "low", shapes: ["circle", "strip"] })
<Checkbox
onChange={(e) => {
if (e.target.checked) fire({ origin: e.currentTarget.parentElement ?? e.currentTarget })
}}
/>Options
| Option | Type | Default | Description |
|---|---|---|---|
| preset | "low" | "medium" | "high" | "burst" | "medium" | Intensity preset |
| origin | { x, y } | Element | RefObject | - | Launch point: { x, y } in viewport px, an element or a ref |
| particleCount | number | from preset | Number of particles |
| angle | number | 90 | Launch direction in degrees, 90 is up |
| spread | number | from preset | Cone width in degrees |
| startVelocity | number | from preset | Initial speed in px per frame at 60 fps |
| gravity | number | 1 | Downward acceleration |
| decay | number | 0.9 | Velocity multiplier per frame |
| drift | number | 0 | Horizontal wind |
| ticks | number | from preset | Lifetime in frames, 60 per second |
| scalar | number | from preset | Particle size multiplier |
| colors | string[] | - | CSS colors, by default a bright palette |
| shapes | ("square" | "circle" | "strip")[] | all | Particle shapes |
| zIndex | number | 100 | z-index of the canvas |
| respectReducedMotion | boolean | true | Skip the effect for prefers-reduced-motion |
Preset values
low: { particleCount: 30, spread: 55, startVelocity: 35, ticks: 160, scalar: 0.9 }
medium: { particleCount: 50, spread: 45, startVelocity: 45, ticks: 200 }
high: { particleCount: 130, spread: 70, startVelocity: 55, ticks: 240 }
burst: { particleCount: 100, spread: 360, startVelocity: 30, ticks: 180 }ConfettiButton
A button that fires from itself on click. Accepts every button prop plus options.
import { ConfettiButton } from "@/components/ui/confetti"
<ConfettiButton options={{ preset: "high" }}>Celebrate</ConfettiButton>useConfetti
useConfetti(defaults?) returns { fire }. fire(options?) merges options over the defaults and returns a Promise that resolves when the canvas is removed. Outside React use fireConfetti(options) from the same file.
const { fire } = useConfetti({ colors: ["#facc15", "#38bdf8"] })
await fire({ origin: { x: 200, y: 300 } })
// the canvas is gone here
// outside React
import { fireConfetti } from "@/components/ui/confetti"
fireConfetti({ preset: "burst" })Features
- No dependencies: own particle physics on canvas 2D in a single file
- On demand: the canvas appears only for the burst and is removed after it
- No leaks: requestAnimationFrame stops and the resize listener is removed together with the canvas
- Reduced motion: fire returns right away when the user asked for less motion