sparkles
Small twinkling sparkles around a badge, a "new" label, a premium button or a product highlight.
Live preview needs WebGPU or WebGL 2. 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.
This example failed to draw.
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. The tabs below are the example above, with the values you set.
To change its code, copy the source into your effects folder; the copy replaces the built-in.
npx @dioramajs/diorama add sparkles<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>import { start } from '@dioramajs/diorama'
const app = start()app.attach('.badge', 'sparkles', { style: 'star', trigger: 'loop', count: 6, scale: 0.7, spread: 14, duration: 1.1,})import { start } from '@dioramajs/diorama'import sparkles from '@dioramajs/particles/sparkles'
const app = start()app.attach('.badge', sparkles({ style: 'star', trigger: 'loop', count: 6, scale: 0.7, spread: 14, duration: 1.1,}))Page transitions
Sparkles fade with the page as it leaves. How a flight flies.
Params
Every param is also an attribute, and both app.attach() and the handle take it by name.
| Param | Type | Default | Description |
|---|---|---|---|
triggerrebuildsdata-dio-trigger | 'hover' | 'click' | 'loop' | 'view' | 'hover' |
|
stylerebuildsdata-dio-style | 'star' | 'dot' | 'cross' | 'star' |
|
countdata-dio-count | number1 to 64 | 12 | Sparkles twinkling at once, or flying out per burst.At most low 8 by quality tier. |
scaledata-dio-scale | number0.25 to 4 | 1 | Size of the sparkles over a base of 30 px. |
colordata-dio-color | color | '#ffd75e' | Color of the sparkles, any CSS color; some come out whiter. |
spreaddata-dio-spread | number0 to 120 px | 24 | How far past the element’s edges sparkles appear and bursts fly. |
durationdata-dio-duration | number0.3 to 4 s | 1.1 | Life of one sparkle. |
fpsdata-dio-fps | number0 to 60 | 30 | Most frames a second of the loop trigger; 0 freezes it.At most low 20 by quality tier. |
seedrebuildsdata-dio-seed | number1 to 100 | 1 | Seed of the randomness. |
Params marked rebuilds re-run the effect's setup when they change, so set them before the element shows.
Inputs and frames
It reads delta, height, opacity and 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. How a frame is decided.
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.
To change what reduced motion does, patch it in start().
import { start } from '@dioramajs/diorama'
start({ effects: { sparkles: { reducedMotion: 'static' } } })Cost and quality
- Download
- 2.6 KB min+gzip 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.
- Frames
- Draws while on screen
It runs from the low tier up. Each tier changes this: The quality plugin picks the tier for each device.
| Tier | What changes |
|---|---|
low | At most 8 sparkles and 20 frames a second for loop. |
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.
Level 3 complete!
You matched all twelve animals to their homes.
Next levelLive preview needs WebGPU or WebGL 2. A Level 3 complete heading on a cream page that bursts with light blue dots as it comes into view.
This example failed to draw.
<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.