# 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.

While the <audio> or <video> plays, the element swells by zoom on every beat of the kick drum and settles back over duration, with a soft halo in color around its rounded shape, or a ring that travels outward from its edge for each beat. by bass follows the bass line instead of single beats, by level the loudness of any sound, such as a voice. The element itself stays live DOM: links and buttons work, text stays sharp. Paused, nothing moves and nothing draws.

Module `audio`, category Sound. Import `@dioramajs/audio/beat`. [Source](https://github.com/MasterKurosava/diorama/blob/main/packages/audio/src/beat.ts)

An album cover next to a player that swells and glows on every kick of the track.

```html title="index.html"
<div class="bt-player">
  <img class="bt-cover" src="/media/records/vinyl-cover.webp" alt="Kite Frequency, cover art" 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>
```

## 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.

```html
<audio id="track" src="/audio/single.mp3" controls="" preload="metadata"></audio><img class="cover" src="/img/cover.jpg" alt="Night Drive, the single" data-dio="beat" data-dio-for="#track">
```

In page transitions: The halo fades with the page; the element itself leaves with the DOM.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `for` (rebuilds) | `data-dio-for` | `string` | `''` | Selector of the audio or video; empty takes the first one in the closest container that holds one. |
| `by` (rebuilds) | `data-dio-by` | `'beat' \| 'bass' \| 'level'` | `'beat'` | What drives the pulse: 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. |
| `style` (rebuilds) | `data-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. |
| `color` | `data-dio-color` | `color` | `'#ff5a36'` | Color of the halo and the rings, any CSS color. |
| `zoom` | `data-dio-zoom` | `number` 1 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. |
| `shake` | `data-dio-shake` | `number` 0 to 12 px | `0` | How far the element jolts on a beat, in a new direction each time; 0 keeps it still. |
| `glow` | `data-dio-glow` | `number` 0 to 1 | `0.8` | Strength of the halo and the rings; 0 draws nothing and leaves only zoom and shake. |
| `spread` | `data-dio-spread` | `number` 0 to 96 px | `28` | How far the halo reaches and the rings travel past the edge. |
| `sensitivity` | `data-dio-sensitivity` | `number` 0 to 1 | `0.5` | How readily a rise in the bass counts as a beat; higher catches softer beats. |
| `duration` | `data-dio-duration` | `number` 0.05 to 1.5 s | `0.25` | Time a pulse takes to settle back. |
| `fps` | `data-dio-fps` | `number` 1 to 60 | `60` | Frame cap while the music plays. At most low 30, medium 45. |

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

### Variants

- `style`: `glow`, `ring`
  - `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.
- `by`: `beat`, `bass`, `level`
  - `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.

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`opacity`](/docs/api/inputs#opacity), [`reducedMotion`](/docs/api/inputs#input-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. See [how a frame is decided](/docs/concepts/frames).

## 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.

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

start({ effects: { beat: { reducedMotion: 'off' } } })
```

## Cost and quality

Loads 4.0 KB 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.

- low: At most 30 fps.
- medium: At most 45 fps.

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

## 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.

A round play button that sends out a ring on every beat while the music plays.

```html title="index.html"
<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>
```

```ts title="script.ts"
import { player } from '../../visualizer/_player.ts'

// The round button is the player's only control, and the beat runs on it.
export default function mount(example: HTMLElement): () => void {
  return player(
    example,
    example.querySelector<HTMLButtonElement>('.bt-play'),
  )
}
```

### 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.

```html
<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.

```html
<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`](/docs/effects/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`](/docs/errors/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.

## Related

- [visualizer](/docs/effects/visualizer.md): The sound of a page player made visible: live bars, a wave, a ring or a waveform.
- [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.
- [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.
- [dots](/docs/effects/dots.md): A dot grid behind a hero or in a card that answers the pointer: dots near it grow, move aside or glow, then settle back.
- [emboss](/docs/effects/emboss.md): Letters pressed into or raised out of paper, leather, brushed metal, stone or plaster, lit by a light that follows the pointer.
- [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.
- [glow](/docs/effects/glow.md): A spotlight that follows the pointer over a grid of cards and lights up their borders, as on SaaS and developer tool sites.
- [keycaps](/docs/effects/keycaps.md): Each letter sits on a 3D mechanical keycap that presses down as if the word were typed, with a click under the pointer.
- [Reduced motion](/docs/guides/reduced-motion.md)
- [Performance and quality](/docs/guides/performance.md)
