beat
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.
Kite Frequency Sundown warm-up, a 20 s excerpt
Live preview needs WebGPU or WebGL 2. An album cover next to a player that swells and glows on every kick of the track.
This example failed to draw.
Or use the player. The cover pulses only while the music plays.
Usage
Put it on any element near an <audio> or <video>: a cover image, a logo, a card or the play button; the player is the nearest one around it, or the one data-dio-for names. start() registers beat, 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 beat<div class="bt-player"> <img class="bt-cover" src="/media/records/vinyl-cover.webp" data-dio="beat" /> <div class="bt-side"> <p class="bt-title"> Kite Frequency <span>Sundown warm-up, a 20 s excerpt</span> </p> <audio src="/media/audio/kite-frequency.mp3" controls preload="metadata" > <track kind="captions" src="/media/audio/kite-frequency.vtt" srclang="en" label="English" /> </audio> </div></div>import { start } from '@dioramajs/diorama'
const app = start()app.attach('.bt-cover', 'beat', { style: 'glow', by: 'beat', color: '#ff5a36', zoom: 1.06, shake: 0, glow: 0.8, spread: 28, sensitivity: 0.5, duration: 0.25, fps: 60,})import { start } from '@dioramajs/diorama'import beat from '@dioramajs/audio/beat'
const app = start()app.attach('.bt-cover', beat({ style: 'glow', by: 'beat', color: '#ff5a36', zoom: 1.06, shake: 0, glow: 0.8, spread: 28, sensitivity: 0.5, duration: 0.25, fps: 60,}))Page transitions
The halo fades with the page; the element itself leaves with the DOM. 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 |
|---|---|---|---|
forrebuildsdata-dio-for | string | '' | Selector of the audio or video; empty takes the first one in the closest container that holds one. |
byrebuildsdata-dio-by | 'beat' | 'bass' | 'level' | 'beat' |
|
stylerebuildsdata-dio-style | 'glow' | 'ring' | 'glow' |
|
colordata-dio-color | color | '#ff5a36' | Color of the halo and the rings, any CSS color. |
zoomdata-dio-zoom | number1 to 1.3 | 1.06 | Scale of the element at the top of a pulse, 1.06 grows it by 6 %; 1 or 0 keeps its size. |
shakedata-dio-shake | number0 to 12 px | 0 | How far the element jolts on a beat, in a new direction each time; 0 keeps it still. |
glowdata-dio-glow | number0 to 1 | 0.8 | Strength of the halo and the rings; 0 draws nothing and leaves only zoom and shake. |
spreaddata-dio-spread | number0 to 96 px | 28 | How far the halo reaches and the rings travel past the edge. |
sensitivitydata-dio-sensitivity | number0 to 1 | 0.5 | How readily a rise in the bass counts as a beat; higher catches softer beats. |
durationdata-dio-duration | number0.05 to 1.5 s | 0.25 | Time a pulse takes to settle back. |
fpsdata-dio-fps | number1 to 60 | 60 | Frame cap while the music plays.At most low 30, medium 45 by quality tier. |
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, opacity and reducedMotion
. Draws at a steady rate while it is on screen and stops when it leaves. None while the player is paused; at most fps while it plays, then until the last pulse and ring fade. How a frame is decided.
Reduced motion and touch
With reduced motion it shows a still frame: motion inputs are zero and time stands still. No zoom, shake or rings: while the music plays the halo holds a steady soft light, and the sound is never analyzed.
Runs on every device. The same as with a mouse: the pulse follows the sound, not the pointer.
To change what reduced motion does, patch it in start().
import { start } from '@dioramajs/diorama'
start({ effects: { beat: { reducedMotion: 'off' } } })Cost and quality
- Download
- 4.0 KB min+gzip the first time a page uses it
- GPU
- One quad around the element drawn by a fragment shader, only while a pulse or a ring shows.
- Memory
- A few kilobytes of analyzer data.
- 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 30 fps. |
medium | At most 45 fps. |
Recipes
A play button that sends out rings
The ring style sends a thin ring out from the element’s edge on each beat, which suits a round play button or an avatar. The button stays a real button: focus, keyboard and its own label keep working.
Signal Radio Kite Frequency, sundown warm-up
Live preview needs WebGPU or WebGL 2. A round play button that sends out a ring on every beat while the music plays.
This example failed to draw.
<div class="bt-radio"> <audio src="/media/audio/kite-frequency.mp3" preload="metadata" > <track kind="captions" src="/media/audio/kite-frequency.vtt" srclang="en" label="English" /> </audio> <button type="button" class="bt-play" aria-label="Play Kite Frequency" data-dio="beat" data-dio-style="ring" data-dio-zoom="1.03" data-dio-color="#7fd3ff" data-dio-spread="56" >Play</button> <p class="bt-now"> Signal Radio <span>Kite Frequency, sundown warm-up</span> </p></div>A podcast cover that breathes
Speech has no beat. by="level" follows the loudness instead, so the cover swells a little with the voice and rests in the pauses. Keep zoom close to 1 for long listening.
<figure class="episode"> <img src="/img/ep42.jpg" data-dio="beat" data-dio-by="level" data-dio-zoom="1.02" data-dio-glow="0.5" > <audio src="/audio/ep42.mp3" controls preload="none"></audio></figure>A card that jolts, without any light
glow="0" draws nothing and leaves only the motion of the element itself; shake jolts it a few pixels in a new direction on each beat. A ticket or a line-up card on a festival page reacts to the set that plays.
<article class="act" data-dio="beat" data-dio-for="#set" data-dio-glow="0" data-dio-shake="4" data-dio-zoom="1.02"> <h3>Tonight, 21:00</h3> <p>Live from the main stage.</p></article>Notes
The effect never starts the sound: it waits for the page’s own player, and it draws nothing and takes no frames while that player is paused. With for left empty it takes the first <audio> or <video> of the closest container that holds one, so a cover and its player can sit in one card without an id.
The element stays live DOM. Its growth and jolt are the CSS scale and translate properties, written only while a pulse shows and put back after, so a link or a button keeps working and its text stays sharp. When another effect draws the element, such as an image effect on the cover, the pulse moves that picture instead. The halo and the rings draw around the element’s rounded shape, never over it.
Beats are rises in the bass above its recent average, at most three a second whatever the tempo, under the flash threshold of WCAG 2.3.1. Faster music pulses on every other beat. Like the visualizer, it reads the sound through Web Audio, so a file from another origin needs crossorigin="anonymous" and an Access-Control-Allow-Origin header, or the effect fails with EFFECT_CORS and the sound plays on untouched.
Under reduced motion nothing scales, shakes or travels, and the sound is not analyzed: while the music plays the halo holds a steady soft light.