Skip to content

Confetti API

ConfettiSimulation

Extends LimitedFrameRateCanvas.

Constructor

typescript
new ConfettiSimulation(canvas: HTMLCanvasElement, config?: ConfettiSimulationConfig)

Methods

fire(config): void

Fires a burst of confetti particles. All config properties are optional and merged with the defaults.

typescript
sim.fire(config: Partial<Config>): void

start(): void

Starts the simulation loop. Called automatically by fire() if not already running.

stop(): void

Stops the simulation loop.

destroy(): void

Stops the simulation and removes all event listeners.


Config

Configuration object for a confetti burst.

typescript
interface Config {
    angle: number;
    colors: string[];
    decay: number;
    gravity: number;
    particles: number;
    shapes: Shape[];
    spread: number;
    startVelocity: number;
    ticks: number;
    x: number;
    y: number;
}
PropertyTypeDefaultDescription
anglenumber90Launch angle in degrees. 90 is straight up.
colorsstring[]7 vibrant colorsArray of hex color strings.
decaynumber0.9Velocity decay factor per tick. Lower = faster slowdown.
gravitynumber1Gravity acceleration. Higher = faster fall.
particlesnumber50Number of confetti particles to emit.
shapesShape[]All 6 shapesShapes to randomly select from.
spreadnumber45Spread angle in degrees.
startVelocitynumber45Initial particle velocity.
ticksnumber200Lifetime in ticks before a particle disappears.
xnumber0.5Horizontal position (0-1, normalized to canvas width).
ynumber0.5Vertical position (0-1, normalized to canvas height).

All properties are optional when calling fire(). Omitted properties use the defaults above.

TIP

startVelocity and gravity are automatically scaled by the simulation's scale setting.


ConfettiSimulationConfig

typescript
interface ConfettiSimulationConfig {
    scale?: number;
    canvasOptions?: CanvasRenderingContext2DSettings;
}
PropertyTypeDefaultDescription
scalenumber1Scales particle size, velocity, and gravity proportionally.
canvasOptionsCanvasRenderingContext2DSettings{colorSpace: 'display-p3'}Options passed to canvas.getContext('2d').

Shape

The visual shape of a confetti particle.

typescript
type Shape = 'circle' | 'diamond' | 'ribbon' | 'square' | 'star' | 'triangle';

Defaults

The default color palette:

typescript
[
    '#26ccff', '#a25afd', '#ff5e7e',
    '#88ff5a', '#fcff42', '#ffa62d', '#ff36ff'
]