Diorama

Made for a big screen.

This site is one 3D scene, built for a computer: a wide window and a mouse. Open dioramajs.dev on a desktop or a laptop to see it.This site is one 3D scene, built for a wide window. Make this one wider, or open dioramajs.dev on a larger screen.

GitHub

DioramaDocs

beat

SoundAlso inBlocks and cards

@dioramajs/audio/beatSource

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.

View as Markdown
On this page
Kite Frequency, cover art

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.

Kite Frequency is a sundown warm-up generated with Web Audio for the Blue Signal demo. Nothing moves until you press play.

Or use the player. The cover pulses only while the music plays.

Params of beat
style

glow lights a soft halo around the element that breathes with the pulse.

by

beat jumps on each kick drum, for music with a beat.

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.

Terminal window
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.

ParamTypeDefaultDescription
forrebuildsdata-dio-forstring''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'
  • beat jumps on each kick drum, for music with a beat.
  • bass follows the bass line smoothly, for slow or ambient music.
  • level follows the loudness, for podcasts and talks.
stylerebuildsdata-dio-style'glow' | 'ring''glow'
  • glow lights a soft halo around the element that breathes with the pulse.
  • ring sends a thin ring out from its edge on each beat, for round play buttons and avatars.
colordata-dio-colorcolor'#ff5a36'Color of the halo and the rings, any CSS color.
zoomdata-dio-zoomnumber1 to 1.31.06Scale of the element at the top of a pulse, 1.06 grows it by 6 %; 1 or 0 keeps its size.
shakedata-dio-shakenumber0 to 12 px0How far the element jolts on a beat, in a new direction each time; 0 keeps it still.
glowdata-dio-glownumber0 to 10.8Strength of the halo and the rings; 0 draws nothing and leaves only zoom and shake.
spreaddata-dio-spreadnumber0 to 96 px28How far the halo reaches and the rings travel past the edge.
sensitivitydata-dio-sensitivitynumber0 to 10.5How readily a rise in the bass counts as a beat; higher catches softer beats.
durationdata-dio-durationnumber0.05 to 1.5 s0.25Time a pulse takes to settle back.
fpsdata-dio-fpsnumber1 to 6060Frame 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.

TierWhat changes
lowAt most 30 fps.
mediumAt 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.

Params of beat
<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.