Confetti burst
Particle confetti on a 2D canvas that celebrates the win: submit the tiny beta form and pieces leap from the button itself — gravity, air drag, spin and the theme’s five colours; a density slider (40–240 pieces) and an “again” button keep the party going.
Real scenarios
The component as it is used in a product — not a specimen grid.
A win the whole screen can hear
A particle confetti cannon on a plain 2D canvas — no library, just integration in a rAF loop. Each piece carries its own velocity, gravity, exponential air drag, spin and life; the palette is read from five CSS variables at fire time, so the dark mirror recolours the party too. The loop parks while off screen and dies the moment the last piece lands.
You are on the list
Build 0.9 of Nimbus Mail lands in your inbox on Friday — along with 2,400 other early readers.
canvas 2D · gravity 1500px/s² · exp drag · dpr-aware · parked off screen · silent under reduced motion
Important props
| Prop | Type | Default | What it does |
|---|---|---|---|
| particles | number | '120' | The density of each blast; the slider spans 40–240 pieces in steps of 20. |
| gravity | px/s² | '1500' | The fall acceleration; an exponential air drag dries the horizontal speed so the throw feels real. |
| palette | css vars | '--mcfb-c1 … c5' | The five piece colours as CSS variables; read at fire time so they stay in sync with the dark mirror. |
| prefers-reduced-motion | behaviour | 'skip' | No blast; only the success message stays and the canvas stays empty. |
Snippet
{{-- confettiBurst(): canvas 2D + RAF; particles carry gravity, drag,
spin and life; the loop parks off screen and dies when empty. --}}
<div class="mcfb" x-data="confettiBurst()">
<div class="mcfb-stage">
<canvas class="mcfb-cv" x-ref="cv" aria-hidden="true"></canvas>
<form class="mcfb-card" x-show="!done" @submit.prevent="submit($event)">
<label class="mcfb-label" for="mcfb-mail">Work email</label>
<input id="mcfb-mail" class="mcfb-input" type="email" x-model="email" placeholder="you@studio.com">
<p class="mcfb-err" x-show="error" role="alert">That address looks incomplete.</p>
<button type="submit" class="mcfb-go" x-ref="go">Join the beta</button>
</form>
<div class="mcfb-card mcfb-win" x-show="done" x-cloak>
<h4>You are on the list</h4>
<p>The first Nimbus Mail build lands in your inbox.</p>
</div>
</div>
<div class="mcfb-foot">
<label class="mcfb-density">Density
<input type="range" min="40" max="240" step="20" x-model.number="density">
<span class="mcfb-num" x-text="faNum(density)"></span>
</label>
<button type="button" class="mcfb-again" x-on:click="again()">Celebrate again</button>
</div>
</div>
<style>
.mcfb-stage { position: relative; display: grid; place-items: center; min-block-size: 20rem;
overflow: clip; border-radius: 1.1rem; background: #fafaf9; }
.mcfb-cv { position: absolute; inset: 0; inline-size: 100%; block-size: 100%; pointer-events: none; }
.mcfb-card { position: relative; z-index: 1; display: grid; gap: .8rem; inline-size: min(100%, 22rem);
padding: 1.4rem; border-radius: 1rem; background: #fff; }
.mcfb-go { block-size: 44px; border: none; border-radius: .7rem; background: #4f46e5; color: #fff;
font: 600 .9rem inherit; cursor: pointer; }
</style>
// app/Pages/Beta.jsx — Inertia (React) page; renders the React
// snippet below unchanged, with Inertia's Link wired into the provider.
import { Link } from '@inertiajs/react';
import { NabuXUIProvider } from '@nabuxai/ui-react';
import ConfettiForm from '@/components/ConfettiForm';
export default function Beta() {
return (
<NabuXUIProvider locale="en" linkComponent={Link}>
<main className="nx-page">
<ConfettiForm />
</main>
</NabuXUIProvider>
);
}
// ConfettiForm.jsx — canvas confetti that celebrates the signup
import { useEffect, useRef, useState } from 'react';
import '@nabuxai/ui-core/css';
const COLORS = ['#f43f5e', '#f59e0b', '#10b981', '#3b82f6', '#a855f7'];
export default function ConfettiForm() {
const cvRef = useRef(null);
const parts = useRef([]);
const raf = useRef(0);
const last = useRef(0);
const [density, setDensity] = useState(120);
const [email, setEmail] = useState('');
const [done, setDone] = useState(false);
// Keep the canvas sized to its box (device pixels behind, CSS px drawing).
useEffect(() => {
const cv = cvRef.current;
const size = () => {
const dpr = Math.min(2, window.devicePixelRatio || 1);
cv.width = Math.round(cv.clientWidth * dpr);
cv.height = Math.round(cv.clientHeight * dpr);
cv.getContext('2d').setTransform(dpr, 0, 0, dpr, 0, 0);
};
const ro = new ResizeObserver(size);
ro.observe(cv);
size();
return () => ro.disconnect();
}, []);
useEffect(() => () => cancelAnimationFrame(raf.current), []);
const loop = (now) => {
const cv = cvRef.current, ctx = cv.getContext('2d');
const dt = Math.min(0.032, (now - last.current) / 1000) || 0.016;
last.current = now;
ctx.clearRect(0, 0, cv.clientWidth, cv.clientHeight);
parts.current = parts.current.filter((p) => {
p.t += dt; p.vy += 1500 * dt; p.vx *= Math.exp(-dt * 1.1);
p.x += p.vx * dt; p.y += p.vy * dt; p.rot += p.vr * dt;
if (p.t > p.life || p.y > cv.clientHeight + 40) return false;
ctx.save();
ctx.globalAlpha = Math.max(0, 1 - p.t / p.life);
ctx.translate(p.x, p.y); ctx.rotate(p.rot); ctx.fillStyle = p.c;
ctx.fillRect(-p.r, -p.r * 0.6, p.r * 2, p.r * 1.2);
ctx.restore();
return true;
});
raf.current = parts.current.length ? requestAnimationFrame(loop) : 0;
};
const burst = (x, y) => {
if (matchMedia('(prefers-reduced-motion: reduce)').matches) return;
for (let i = 0; i < density; i++) {
const a = -Math.PI / 2 + (Math.random() - 0.5) * Math.PI * 0.9;
const sp = 320 + Math.random() * 520;
parts.current.push({ x, y, vx: Math.cos(a) * sp, vy: Math.sin(a) * sp,
r: 3 + Math.random() * 4, rot: Math.random() * 6.28, vr: (Math.random() - 0.5) * 14,
c: COLORS[i % COLORS.length], life: 1.6 + Math.random() * 0.9, t: 0 });
}
raf.current ||= requestAnimationFrame(loop);
};
const submit = (e) => {
e.preventDefault();
if (!/.+@.+\..+/.test(email)) return;
const cv = cvRef.current, r = e.target.querySelector('button').getBoundingClientRect();
const b = cv.getBoundingClientRect();
setDone(true);
burst(r.left + r.width / 2 - b.left, r.top - b.top);
};
return (
<>
<style>{`
.mcfb-stage { position: relative; display: grid; place-items: center; min-block-size: 20rem;
overflow: clip; border-radius: 1.1rem; background: #fafaf9; }
.mcfb-cv { position: absolute; inset: 0; inline-size: 100%; block-size: 100%; pointer-events: none; }
.mcfb-card { position: relative; z-index: 1; display: grid; gap: .8rem; inline-size: min(100%, 22rem);
padding: 1.4rem; border-radius: 1rem; background: #fff; font: 400 .9rem system-ui; }
.mcfb-input { block-size: 44px; padding-inline: .9rem; border-radius: .7rem;
border: 1px solid #e7e5e4; background: #f5f5f4; font: inherit; }
.mcfb-go { block-size: 44px; border: none; border-radius: .7rem; background: #4f46e5;
color: #fff; font: 600 .9rem inherit; cursor: pointer; }
.mcfb-density { display: inline-flex; align-items: center; gap: .6rem; font-size: .8rem; color: #78716c; }
.mcfb-num { font-variant-numeric: tabular-nums; font-weight: 600; color: #1c1917; }
`}</style>
<div className="mcfb-stage">
<canvas ref={cvRef} className="mcfb-cv" aria-hidden="true" />
{done ? (
<div className="mcfb-card"><h4>You are on the list</h4>
<p>The first Nimbus Mail build lands in your inbox.</p></div>
) : (
<form className="mcfb-card" onSubmit={submit}>
<label htmlFor="mail">Work email</label>
<input id="mail" value={email} onChange={(e) => setEmail(e.target.value)} placeholder="you@studio.com" />
<button type="submit" className="mcfb-go">Join the beta</button>
</form>
)}
</div>
<label className="mcfb-density">Density
<input type="range" min="40" max="240" step="20" value={density}
onChange={(e) => setDensity(+e.target.value)} />
<span className="mcfb-num">{density}</span>
</label>
</>
);
}
<!-- ConfettiForm.vue — canvas confetti that celebrates the signup -->
<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref } from 'vue';
import '@nabuxai/ui-core/css';
const COLORS = ['#f43f5e', '#f59e0b', '#10b981', '#3b82f6', '#a855f7'];
const cv = ref<HTMLCanvasElement | null>(null);
const density = ref(120);
const email = ref('');
const done = ref(false);
let parts: Array<{x:number,y:number,vx:number,vy:number,r:number,rot:number,vr:number,c:string,life:number,t:number}> = [];
let raf = 0, last = 0, ro: ResizeObserver | null = null;
const loop = (now: number) => {
const c = cv.value!, ctx = c.getContext('2d')!;
const dt = Math.min(0.032, (now - last) / 1000) || 0.016;
last = now;
ctx.clearRect(0, 0, c.clientWidth, c.clientHeight);
parts = parts.filter((p) => {
p.t += dt; p.vy += 1500 * dt; p.vx *= Math.exp(-dt * 1.1);
p.x += p.vx * dt; p.y += p.vy * dt; p.rot += p.vr * dt;
if (p.t > p.life || p.y > c.clientHeight + 40) return false;
ctx.save();
ctx.globalAlpha = Math.max(0, 1 - p.t / p.life);
ctx.translate(p.x, p.y); ctx.rotate(p.rot); ctx.fillStyle = p.c;
ctx.fillRect(-p.r, -p.r * 0.6, p.r * 2, p.r * 1.2);
ctx.restore();
return true;
});
raf = parts.length ? requestAnimationFrame(loop) : 0;
};
const burst = (x: number, y: number) => {
if (matchMedia('(prefers-reduced-motion: reduce)').matches) return;
for (let i = 0; i < density.value; i++) {
const a = -Math.PI / 2 + (Math.random() - 0.5) * Math.PI * 0.9;
const sp = 320 + Math.random() * 520;
parts.push({ x, y, vx: Math.cos(a) * sp, vy: Math.sin(a) * sp,
r: 3 + Math.random() * 4, rot: Math.random() * 6.28, vr: (Math.random() - 0.5) * 14,
c: COLORS[i % COLORS.length], life: 1.6 + Math.random() * 0.9, t: 0 });
}
if (!raf) raf = requestAnimationFrame(loop);
};
const submit = (e: Event) => {
e.preventDefault();
if (!/.+@.+\..+/.test(email.value)) return;
const btn = (e.target as HTMLElement).querySelector('button')!;
const r = btn.getBoundingClientRect(), b = cv.value!.getBoundingClientRect();
done.value = true;
burst(r.left + r.width / 2 - b.left, r.top - b.top);
};
onMounted(() => {
const size = () => {
const c = cv.value!, dpr = Math.min(2, devicePixelRatio || 1);
c.width = Math.round(c.clientWidth * dpr);
c.height = Math.round(c.clientHeight * dpr);
c.getContext('2d')!.setTransform(dpr, 0, 0, dpr, 0, 0);
};
ro = new ResizeObserver(size);
ro.observe(cv.value!);
size();
});
onBeforeUnmount(() => {
cancelAnimationFrame(raf);
ro?.disconnect();
});
</script>
<template>
<div class="mcfb-stage">
<canvas ref="cv" class="mcfb-cv" aria-hidden="true"></canvas>
<div v-if="done" class="mcfb-card"><h4>You are on the list</h4>
<p>The first Nimbus Mail build lands in your inbox.</p></div>
<form v-else class="mcfb-card" @submit="submit">
<label for="mail">Work email</label>
<input id="mail" v-model="email" type="email" placeholder="you@studio.com" />
<button type="submit" class="mcfb-go">Join the beta</button>
</form>
</div>
<label class="mcfb-density">Density
<input v-model.number="density" type="range" min="40" max="240" step="20" />
<span class="mcfb-num">{{ density }}</span>
</label>
</template>
<style scoped>
.mcfb-stage { position: relative; display: grid; place-items: center; min-block-size: 20rem;
overflow: clip; border-radius: 1.1rem; background: #fafaf9; }
.mcfb-cv { position: absolute; inset: 0; inline-size: 100%; block-size: 100%; pointer-events: none; }
.mcfb-card { position: relative; z-index: 1; display: grid; gap: .8rem; inline-size: min(100%, 22rem);
padding: 1.4rem; border-radius: 1rem; background: #fff; font: 400 .9rem system-ui; }
.mcfb-input { block-size: 44px; padding-inline: .9rem; border-radius: .7rem;
border: 1px solid #e7e5e4; background: #f5f5f4; font: inherit; }
.mcfb-go { block-size: 44px; border: none; border-radius: .7rem; background: #4f46e5;
color: #fff; font: 600 .9rem inherit; cursor: pointer; }
.mcfb-density { display: inline-flex; align-items: center; gap: .6rem; font-size: .8rem; color: #78716c; }
.mcfb-num { font-variant-numeric: tabular-nums; font-weight: 600; color: #1c1917; }
</style>
<!-- ConfettiForm.svelte — canvas confetti that celebrates the signup -->
<script lang="ts">
import { onMount } from 'svelte';
import '@nabuxai/ui-core/css';
const COLORS = ['#f43f5e', '#f59e0b', '#10b981', '#3b82f6', '#a855f7'];
let cv: HTMLCanvasElement;
let density = $state(120);
let email = $state('');
let done = $state(false);
let parts: Array<{x:number,y:number,vx:number,vy:number,r:number,rot:number,vr:number,c:string,life:number,t:number}> = [];
let raf = 0, last = 0;
const loop = (now: number) => {
const ctx = cv.getContext('2d')!;
const dt = Math.min(0.032, (now - last) / 1000) || 0.016;
last = now;
ctx.clearRect(0, 0, cv.clientWidth, cv.clientHeight);
parts = parts.filter((p) => {
p.t += dt; p.vy += 1500 * dt; p.vx *= Math.exp(-dt * 1.1);
p.x += p.vx * dt; p.y += p.vy * dt; p.rot += p.vr * dt;
if (p.t > p.life || p.y > cv.clientHeight + 40) return false;
ctx.save();
ctx.globalAlpha = Math.max(0, 1 - p.t / p.life);
ctx.translate(p.x, p.y); ctx.rotate(p.rot); ctx.fillStyle = p.c;
ctx.fillRect(-p.r, -p.r * 0.6, p.r * 2, p.r * 1.2);
ctx.restore();
return true;
});
raf = parts.length ? requestAnimationFrame(loop) : 0;
};
const burst = (x: number, y: number) => {
if (matchMedia('(prefers-reduced-motion: reduce)').matches) return;
for (let i = 0; i < density; i++) {
const a = -Math.PI / 2 + (Math.random() - 0.5) * Math.PI * 0.9;
const sp = 320 + Math.random() * 520;
parts.push({ x, y, vx: Math.cos(a) * sp, vy: Math.sin(a) * sp,
r: 3 + Math.random() * 4, rot: Math.random() * 6.28, vr: (Math.random() - 0.5) * 14,
c: COLORS[i % COLORS.length], life: 1.6 + Math.random() * 0.9, t: 0 });
}
if (!raf) raf = requestAnimationFrame(loop);
};
const submit = (e: SubmitEvent) => {
e.preventDefault();
if (!/.+@.+\..+/.test(email)) return;
const btn = (e.target as HTMLElement).querySelector('button')!;
const r = btn.getBoundingClientRect(), b = cv.getBoundingClientRect();
done = true;
burst(r.left + r.width / 2 - b.left, r.top - b.top);
};
onMount(() => {
const size = () => {
const dpr = Math.min(2, devicePixelRatio || 1);
cv.width = Math.round(cv.clientWidth * dpr);
cv.height = Math.round(cv.clientHeight * dpr);
cv.getContext('2d')!.setTransform(dpr, 0, 0, dpr, 0, 0);
};
const ro = new ResizeObserver(size);
ro.observe(cv);
size();
return () => { cancelAnimationFrame(raf); ro.disconnect(); };
});
</script>
<div class="mcfb-stage">
<canvas bind:this={cv} class="mcfb-cv" aria-hidden="true"></canvas>
{#if done}
<div class="mcfb-card"><h4>You are on the list</h4>
<p>The first Nimbus Mail build lands in your inbox.</p></div>
{:else}
<form class="mcfb-card" onsubmit={submit}>
<label for="mail">Work email</label>
<input id="mail" bind:value={email} type="email" placeholder="you@studio.com" />
<button type="submit" class="mcfb-go">Join the beta</button>
</form>
{/if}
</div>
<label class="mcfb-density">Density
<input bind:value={density} type="range" min="40" max="240" step="20" />
<span class="mcfb-num">{density}</span>
</label>
<style>
.mcfb-stage { position: relative; display: grid; place-items: center; min-block-size: 20rem;
overflow: clip; border-radius: 1.1rem; background: #fafaf9; }
.mcfb-cv { position: absolute; inset: 0; inline-size: 100%; block-size: 100%; pointer-events: none; }
.mcfb-card { position: relative; z-index: 1; display: grid; gap: .8rem; inline-size: min(100%, 22rem);
padding: 1.4rem; border-radius: 1rem; background: #fff; font: 400 .9rem system-ui; }
.mcfb-input { block-size: 44px; padding-inline: .9rem; border-radius: .7rem;
border: 1px solid #e7e5e4; background: #f5f5f4; font: inherit; }
.mcfb-go { block-size: 44px; border: none; border-radius: .7rem; background: #4f46e5;
color: #fff; font: 600 .9rem inherit; cursor: pointer; }
.mcfb-density { display: inline-flex; align-items: center; gap: .6rem; font-size: .8rem; color: #78716c; }
.mcfb-num { font-variant-numeric: tabular-nums; font-weight: 600; color: #1c1917; }
</style>