# sparkles

Small twinkling sparkles around a badge, a "new" label, a premium button or a product highlight.

Four-point stars, soft dots or thin lens glints of scale times 30 px twinkle around the element: each grows, turns a little and fades over about duration seconds, spread up to spread px past its edges, some whiter than color. A click bursts count of them out of the pointer and they slow down as they fly. They draw over the page; the element stays DOM and keeps every click.

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

An upgrade panel on a deep violet page with a gold New badge that twinkles all the time and a Go Pro button that sparkles on hover.

```html title="index.html"
<div class="upgrade">
  <p class="plan">Studio plan <span class="badge" data-dio="sparkles" data-dio-scale="0.7" data-dio-spread="14" data-dio-trigger="loop" data-dio-count="6">New</span></p>
  <p class="pitch">Unlimited projects, brand kits and 4K export.</p>
  <a class="pro" href="/upgrade" data-dio="sparkles" data-dio-count="14">Go Pro</a>
</div>
```

## Usage

Put it on: any element: a badge, a label, a button, a card, a product photo.

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

```html
<span class="badge" data-dio="sparkles" data-dio-scale="0.75" data-dio-spread="14">New</span>
```

In page transitions: Sparkles fade with the page as it leaves.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `trigger` (rebuilds) | `data-dio-trigger` | `'hover' \| 'click' \| 'loop' \| 'view'` | `'hover'` | hover on hover or focus; click bursts from the pointer; loop nonstop; view once in view. |
| `style` (rebuilds) | `data-dio-style` | `'star' \| 'dot' \| 'cross'` | `'star'` | star draws four-point sparkles; dot draws soft round dots; cross draws thin lens glints. |
| `count` | `data-dio-count` | `number` 1 to 64 | `12` | Sparkles twinkling at once, or flying out per burst. At most low 8. |
| `scale` | `data-dio-scale` | `number` 0.25 to 4 | `1` | Size of the sparkles over a base of 30 px. |
| `color` | `data-dio-color` | `color` | `'#ffd75e'` | Color of the sparkles, any CSS color; some come out whiter. |
| `spread` | `data-dio-spread` | `number` 0 to 120 px | `24` | How far past the element’s edges sparkles appear and bursts fly. |
| `duration` | `data-dio-duration` | `number` 0.3 to 4 s | `1.1` | Life of one sparkle. |
| `fps` | `data-dio-fps` | `number` 0 to 60 | `30` | Most frames a second of the loop trigger; 0 freezes it. At most low 20. |
| `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

- `style`: `star`, `dot`, `cross`
  - `star` draws four-point sparkles.
  - `dot` draws soft round dots.
  - `cross` draws thin lens glints.
- `trigger`: `hover`, `click`, `loop`, `view`
  - `hover` on hover or focus.
  - `click` bursts from the pointer.
  - `loop` nonstop.
  - `view` once in view.

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`height`](/docs/api/inputs#input-height), [`opacity`](/docs/api/inputs#opacity), [`width`](/docs/api/inputs#input-width). Draws at a steady rate while it is on screen and stops when it leaves. Only while sparkles live: none at rest for hover, click and view; at most fps a second while on screen for loop. 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 bursts like a click and starts a few hover sparkles; loop and view work as on desktop.

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

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

## Cost and quality

Loads 2.6 KB the first time a page uses it. GPU: One instanced draw of at most 64 small quads, only while sparkles live. Memory: Instance attributes for 64 sparkles, about 3 KB.

- low: At most 8 sparkles and 20 frames a second for loop.

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

## Recipes

### A reward when a level is done

On a learning site for kids, `trigger="view"` bursts once the first time half of the heading is in view. Round `dot` sparkles in a soft color read as confetti for the very young; a larger `spread` lets them fly clear of the text.

A Level 3 complete heading on a cream page that bursts with light blue dots as it comes into view.

```html title="index.html"
<section class="reward">
  <h3 data-dio="sparkles" data-dio-trigger="view" data-dio-style="dot" data-dio-count="24" data-dio-spread="40" data-dio-color="#7dd3fc">Level 3 complete!</h3>
  <p>You matched all twelve animals to their homes.</p>
  <a class="next" href="/reward">Next level</a>
</section>
```

## Notes

The element stays DOM and keeps every click: the sparkles draw over the page on the canvas, and the click goes on to the link, button or form. A click with the pointer bursts from where it landed; a keyboard click bursts from the element’s edge. Keyboard focus starts the `hover` sparkles too.

Nothing is drawn at rest with `hover`, `click` and `view`. With `loop` the sparkles twinkle at most `fps` frames a second while the element is on screen and stop while it is off screen or the tab is hidden, so keep `always` for one or two elements a page.

Under reduced motion neither the module nor the engine loads for it, and the element stays as it is.

`trigger`, `style` and `seed` rebuild the effect when they change; the other params change live.

## Related

- [confetti](/docs/effects/confetti.md): A burst of confetti from an element, a shower or fireworks to celebrate a sign-up, an order or a donation.
- [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.
- [assemble](/docs/effects/assemble.md): Thousands of lit particles fly out of the depth around the page and gather into a heading.
- [beam](/docs/effects/beam.md): A light beam that runs around the rounded border of a feature card, an input field or a highlight box.
- [beat](/docs/effects/beat.md): A cover, logo, card or play button that pulses with the music of a page player: it scales, shakes and glows, or sends out rings, on the beat.
- [coin](/docs/effects/coin.md): A coin, medal or token struck from a logo or a photo: it turns, catches the light and flips over.
- [Reduced motion](/docs/guides/reduced-motion.md)
- [Performance and quality](/docs/guides/performance.md)
