Confetti

A one-shot celebration: small paper pieces burst up and out from an element, flutter, tumble and fall under gravity, and are gone inside two and a half seconds. A service, not a component: call burst() from wherever the moment is.

Installation

Import the class, and @use the entry point in your global styles. Nothing warns you when the @use is missing.

import { WrConfetti } from 'ngwr/animations/confetti';

Usage

Inject WrConfetti and call burst() with the element the pieces should come from. The first burst makes one fixed, full-viewport canvas in document.body, aria-hidden and under no pointer, and starts one frame loop that draws on the CPU every frame while pieces move, and stops when none are left; a burst that comes while pieces are in the air joins the same canvas, and the canvas goes when the last piece has faded or dropped off the bottom. Each piece is a rectangle, every fifth a thin strip, turning on the screen plane while its drawn height follows a cosine, so it flips like paper.

private readonly confetti = inject(WrConfetti);

protected celebrate(button: HTMLElement): void {
  this.confetti.burst(button);
}

Options

count is how many pieces a burst has; seed fixes the layout, so the same seed gives the same burst. from may be a plain rect in client coordinates instead of an element: a rect as wide as the viewport and no height showers the whole page from its top edge.

this.confetti.burst(button, { count: 160 });
// The same seed gives the same burst, which a spec can pin.
this.confetti.burst({ left: 0, top: 0, width: innerWidth, height: 0 }, { seed: 7 });

Colours

The pieces take the five --wr-confetti-color-* hooks on the canvas, the primary, success, warning, danger and info intents by default, resolved through the cascade when a burst starts, so the scheme the reader is in is the one that paints. Bare intents, not inks: confetti is a graphic, not text.

.wr-confetti {
  --wr-confetti-color-1: var(--wr-color-primary);
  --wr-confetti-color-2: var(--wr-color-success);
}

Accessibility

PartBehaviour
Screen readersThe canvas is aria-hidden, holds no content and takes no pointer.
Reduced motion Under prefers-reduced-motion: reduce a burst does nothing, and a preference that flips mid-burst clears the canvas at once. Under forced-colors: active nothing is drawn.
Hidden documentA hidden tab pauses the loop; a visible one resumes it where it stopped.
ServerA burst on the server does nothing: the canvas exists only in the browser.

API

NameDescriptionTypeDefault
burst(from, options?)Throws a burst up from from: an element, whose client rect is read now, or a rect in client coordinates. Pieces start anywhere inside the rect, so a wide block showers along its width and a button bursts from a point.(from: Element | WrConfettiRect, options?: WrConfettiOptions) => void—
clear()Stops the loop and takes the canvas and every piece out. Idempotent.() => void—
activeWhether pieces are in the air.booleanfalse
options.countPieces in the burst.number84
options.seedSeeds the layout, so the same seed gives the same burst.numberrandom

CSS variables

Set these on the element itself, with a rule more specific than its Declared on selector. In a global stylesheet, put a class of yours on an ancestor it really has: <body> for an overlay panel, which renders outside your markup. In a component stylesheet the selector alone works, but only for an element in that component's own template. A value on a wrapper or :root is shadowed, because ngwr/animations/confetti declares each default on its own element.

VariableDefaultDeclared on
--wr-confetti-color-1var(--wr-color-primary).wr-confetti
--wr-confetti-color-2var(--wr-color-success).wr-confetti
--wr-confetti-color-3var(--wr-color-warning).wr-confetti
--wr-confetti-color-4var(--wr-color-danger).wr-confetti
--wr-confetti-color-5var(--wr-color-info).wr-confetti

See also