# confetti

A burst of confetti from an element, a shower or fireworks to celebrate a sign-up, an order or a donation.

count pieces fly out of the element’s box at angle with spread and speed, slow down in the air, fall with gravity and tumble in 3D, face and back in different shades; ribbons bend, and every piece fades over the last fifth of its life. The rain motion lets the pieces fall from above the viewport over its whole width instead, and fireworks bursts five shells one after another at random points of its upper half. The pieces draw over everything and the element stays as it is.

Module `particles`, category Celebration and particles. Import `@dioramajs/particles/confetti`. [Source](https://github.com/MasterKurosava/diorama/blob/main/packages/particles/src/confetti.ts)

A checkout card whose Place order button bursts confetti over the page when clicked.

```html title="index.html"
<div class="cf-shop">
  <section class="cf-order" aria-label="Order summary">
    <p class="cf-kicker">Your order</p>
    <ul class="cf-lines">
      <li><span>Field notebook, 3-pack</span><span>$24.00</span></li>
      <li><span>Brass pencil</span><span>$18.00</span></li>
      <li><span>Shipping</span><span>Free</span></li>
    </ul>
    <p class="cf-total"><span>Total</span><span>$42.00</span></p>
    <button type="button" class="cf-pay" data-dio="confetti">Place order</button>
    <p class="cf-status" role="status"></p>
  </section>
</div>
```

## Usage

Put it on: any element: a button, a section, a form.

`start()` registers `confetti`, so the attribute alone puts it on an element.

```html
<button type="button" data-dio="confetti" data-dio-count="120">Add to calendar</button>
```

In page transitions: A burst fades with the page as it leaves.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `trigger` (rebuilds) | `data-dio-trigger` | `'click' \| 'view' \| 'event' \| 'manual'` | `'click'` | click, view once on screen, a DOM event, or manual through fire. |
| `motion` (rebuilds) | `data-dio-motion` | `'burst' \| 'rain' \| 'fireworks'` | `'burst'` | burst fires the pieces out of the element, for a button or a form; rain lets them fall from above the whole viewport for a few seconds, for a thank-you or results page; fireworks bursts shells in every direction over the upper viewport, for a launch or a big win. Rain and fireworks ignore origin, angle and spread. |
| `event` (rebuilds) | `data-dio-event` | `string` | `''` | Name of the DOM event on the element for the event trigger. |
| `fire` | `data-dio-fire` | `number` 0 to 10 | `0` | Every increase fires a burst with the manual trigger. |
| `once` | `data-dio-once` | `boolean` | `false` | At most one burst per page. |
| `origin` (rebuilds) | `data-dio-origin` | `'center' \| 'edges' \| 'pointer'` | `'center'` | center of the box, its edges, or the pointer. |
| `count` | `data-dio-count` | `number` 1 to 1200 | `200` | Pieces per burst. At most low 80, medium 140. |
| `angle` | `data-dio-angle` | `number` 0 to 360° | `90` | Direction of the burst; 90 is up. |
| `spread` | `data-dio-spread` | `number` 0 to 360° | `70` | Spread around the direction. |
| `speed` | `data-dio-speed` | `number` 100 to 4000 px/s | `1400` | Initial speed of the pieces. |
| `gravity` | `data-dio-gravity` | `number` 0 to 5000 | `1600` | Gravity in px per second squared. |
| `decay` | `data-dio-decay` | `number` 0 to 5 | `1.2` | Air drag per second, as in canvas-confetti. |
| `duration` | `data-dio-duration` | `number` 0.5 to 10 s | `3` | Life of a piece. |
| `scale` | `data-dio-scale` | `number` 0.2 to 4 | `1` | Size of the pieces over a base of 10 px. |
| `colors` (rebuilds) | `data-dio-colors` | `string` | `'#ff5a36 #ffd23f #3ec1d3 #7b5cff #ffffff'` | Colors separated by spaces, any CSS colors. |
| `shapes` (rebuilds) | `data-dio-shapes` | `'mix' \| 'rect' \| 'circle' \| 'ribbon'` | `'mix'` | mix, rect, circle or ribbon. |
| `seed` (rebuilds) | `data-dio-seed` | `number` 1 to 100 | `1` | Seed of the randomness. |

Changing a param marked rebuilds re-runs the effect setup.

### Variants

- `motion`: `burst`, `rain`, `fireworks`
  - `burst` fires the pieces out of the element, for a button or a form.
  - `rain` lets them fall from above the whole viewport for a few seconds, for a thank-you or results page.
  - `fireworks` bursts shells in every direction over the upper viewport, for a launch or a big win. Rain and fireworks ignore origin, angle and spread.
- `trigger`: `click`, `view`, `event`, `manual`
- `origin`: `center`, `edges`, `pointer`
- `shapes`: `mix`, `rect`, `circle`, `ribbon`

## Inputs and frames

Reads [`presence`](/docs/api/inputs#presence). Draws only while something on it moves, then stops. Only while pieces fly; none at rest. See [how a frame is decided](/docs/concepts/frames).

## Reduced motion and touch

With reduced motion it is off and the element shows as plain HTML. Off: neither the module nor the engine loads for it, and the page is unchanged.

Runs on every device. A tap fires like a click.

```ts
import { start } from '@dioramajs/diorama'

start({ effects: { confetti: { reducedMotion: 'static' } } })
```

## Cost and quality

Loads 4.2 KB the first time a page uses it. GPU: One instanced draw for a pool of 1200 pieces. Memory: Instance attributes of the pool, under 100 KB.

- low: At most 80 pieces.
- medium: At most 140 pieces.

See [the quality plugin](/docs/effects/quality).

## Recipes

### Celebrate a sign-up when the server says yes

A click is too early for a form: the request can still fail. Use the `event` trigger and dispatch the event once your server answers, so the burst marks a sign-up that happened.

A free trial form that bursts confetti once the sign-up request succeeds.

```html title="index.html"
<form class="cf-signup" data-dio="confetti" data-dio-trigger="event" data-dio-event="signup:done">
  <label for="cf-email">Work email</label>
  <div class="cf-row">
    <input id="cf-email" type="email" name="email" value="you@example.com" required />
    <button type="submit">Start free trial</button>
  </div>
  <p class="cf-note" role="status">14 days free, no card needed.</p>
</form>
```

```html title="panel.html"
<p>Press Start free trial. The burst waits for the stand-in server, 0.6 s.</p>
```

```ts title="script.ts"
// Stands in for the sign-up request: once it answers, the form dispatches the event the confetti
// waits for and says what happened in its status line.
export default function mount(example: HTMLElement): () => void {
  const form = example.querySelector<HTMLFormElement>('.cf-signup')
  const button = form?.querySelector('button')
  const note = form?.querySelector('.cf-note')
  if (!form || !button || !note) return () => {}
  let timer = 0
  const onSubmit = (e: SubmitEvent) => {
    e.preventDefault()
    button.disabled = true
    button.textContent = 'Creating your account'
    timer = window.setTimeout(() => {
      form.dispatchEvent(new Event('signup:done'))
      note.textContent =
        'You are in. Check your inbox to confirm your email.'
      button.disabled = false
      button.textContent = 'Start free trial'
    }, 600)
  }
  form.addEventListener('submit', onSubmit)
  return () => {
    clearTimeout(timer)
    form.removeEventListener('submit', onSubmit)
  }
}
```

```ts title="src/signup.ts"
const form = document.querySelector('form.signup') as HTMLFormElement

form.addEventListener('submit', async (event) => {
  event.preventDefault()
  const response = await fetch('/api/signup', {
    method: 'POST',
    body: new FormData(form),
  })
  if (response.ok) form.dispatchEvent(new Event('signup:done'))
})
```

### Rain on a donation thank-you page

The `view` trigger fires the first time half the section is in view, and `rain` lets the pieces fall from above the whole viewport for a few seconds. With `once` it rains one time on the page, however often the visitor scrolls back.

```html
<section
  class="thanks"
  data-dio="confetti"
  data-dio-trigger="view"
  data-dio-motion="rain"
  data-dio-count="300"
  data-dio-once="true"
>
  <h1>Thank you. Your gift plants 40 trees.</h1>
  <p>A receipt for your tax records is on its way to your inbox.</p>
</section>
```

### Fireworks when a course is complete

The `manual` trigger leaves the moment to your code: every increase of `fire` is one burst. Fire it when the last lesson is marked done, and let `fireworks` burst shells over the upper half of the viewport.

The last lesson of a course marked complete, with fireworks bursting over the page.

```html title="index.html"
<div class="cf-course" data-dio="confetti" data-dio-trigger="manual" data-dio-motion="fireworks" data-dio-shapes="circle">
  <p class="cf-step">Lesson 12 of 12</p>
  <p class="cf-name">Final project: a week of dinners for under $40</p>
  <div class="cf-bar"><span></span></div>
  <button type="button">Mark as complete</button>
  <p class="cf-said" role="status"></p>
</div>
```

```html title="panel.html"
<button type="button" data-reset>Start over</button>
<p>Mark the lesson complete to fire the fireworks.</p>
```

```ts title="script.ts"
import { app } from '../../../../docs/diorama.ts'

// Completing the last lesson fills the bar and fires the fireworks through the manual trigger.
export default function mount(example: HTMLElement): () => void {
  const course = example.querySelector<HTMLElement>('.cf-course')
  const button = course?.querySelector('button')
  const said = course?.querySelector('.cf-said')
  const reset = example.querySelector<HTMLButtonElement>('[data-reset]')
  if (!course || !button || !said) return () => {}
  const onDone = () => {
    const confetti = app.effects.get(course, 'confetti')
    if (confetti) confetti.params.fire++
    course.style.setProperty('--done', '1')
    button.disabled = true
    button.textContent = 'Completed'
    said.textContent =
      'Course complete. Your certificate is ready to download.'
  }
  const onReset = () => {
    course.style.removeProperty('--done')
    button.disabled = false
    button.textContent = 'Mark as complete'
    said.textContent = ''
  }
  button.addEventListener('click', onDone)
  reset?.addEventListener('click', onReset)
  return () => {
    button.removeEventListener('click', onDone)
    reset?.removeEventListener('click', onReset)
  }
}
```

```ts
import { app } from './diorama'

const course = document.querySelector('.cf-course')
const confetti = course && app.effects.get(course, 'confetti')
if (confetti) confetti.params.fire++
```

## Notes

Under reduced motion neither the module nor the engine loads for it, and the page stays as it is. Treat the burst as decoration: say what happened in the page itself, as the examples do in a status line.

The effect never cancels the click. A form still submits and a link still opens; when the page changes right away, the pieces fade out with it during a page transition.

The pieces are drawn in one instanced draw from a pool of 1200, and frames run only while pieces fly. The quality tier caps `count` at 80 on the low tier and 140 on the medium one, so markup that asks for 400 pieces gets 140 on a device at the medium tier.

`trigger`, `motion`, `origin`, `shapes`, `colors` and `seed` rebuild the effect when they change. Counts, speeds and angles change live and apply to the next burst.

## Related

- [drops](/docs/effects/drops.md): Water drops on glass over a hero or the whole page: rain that beads and runs, or meltwater left by a thaw.
- [emoji](/docs/effects/emoji.md): Emoji or small pictures that float up from a like button, burst out of a success state or rain over a section.
- [snow](/docs/effects/snow.md): Snow, cherry petals or autumn leaves drifting down over a section or the whole page, for a seasonal campaign.
- [sparkles](/docs/effects/sparkles.md): Small twinkling sparkles around a badge, a "new" label, a premium button or a product highlight.
- [assemble](/docs/effects/assemble.md): Thousands of lit particles fly out of the depth around the page and gather into a heading.
- [dust](/docs/effects/dust.md): A heading crumbles into fine sand that lifts off the page and drifts away in depth as it scrolls out, or on a click.
- [voxel](/docs/effects/voxel.md): A heading built from small lit 3D cubes that tumble in, bounce into place and settle into the text.
- [Reduced motion](/docs/guides/reduced-motion.md)
- [Performance and quality](/docs/guides/performance.md)
