WaveformSeekBar
Live AnalyserNode waveform that IS the seek bar β click/drag to seek, keyboard model, zero-dep.
Live demo
01Playground β press play, then click/drag the waveform to seek
Keyboard: Tab to the waveform, then β/β Β±5s Β· PgUp/PgDn Β±30s Β· Home/End. The first click also resumes the suspended AudioContext (browser autoplay policy).
Implementation
02<script lang="ts">
import WaveformSeekBar from '$lib/components/WaveformSeekBar.svelte';
β
let audioEl: HTMLAudioElement;
</script>
β
<audio bind:this={audioEl} src="/audio/episode-01.mp3" preload="metadata"></audio>
<WaveformSeekBar audio={audioEl} mode="waveform" height={72} />WaveformSeekBar pipes the audio element through an AnalyserNode and paints live bars to canvas β time-domain (waveform) or frequency-domain (bars). Click/drag seeks via pointer capture; the played region tints from currentTime/duration. Keyboard: arrows Β±5s, PageUp/Down Β±30s, Home/End to bounds. No Web Audio (or reduced-motion) degrades gracefully: the seek bar and role=slider keyboard model keep working. rAF runs only while playing.
Logic explainer
03What Does It Do? (Plain English)
A live audio waveform that is the seek bar β bars move with the music (via the Web Audio AnalyserNode), the played region tints, and clicking or dragging anywhere on the waveform seeks the track there. The SoundCloud signature interaction, zero dependencies.
How It Works (Pseudo-Code)
on mount (client only):
if Web Audio exists: create/borrow AudioContext β MediaElementSource β AnalyserNode
else: degrade to seek-bar-only (nothing breaks, just no live bars)
while playing: rAF loop β getByteTimeDomainData (waveform) or getByteFrequencyData (bars)
β decimate to β€300 columns β draw; played fraction tinted via currentTime/duration
pointer down/drag on canvas β fraction Γ duration β audio.currentTime
keyboard: β/β Β±5s Β· PageUp/Down Β±30s Β· Home/End to bounds
paused β rAF cancelled (idle-off); seeks repaint once so the tint still tracksSharing an AudioContext
Pass audioCtx when pairing with AudioPlayer (or anything else on the same page) β one context per page is the polite ceiling, and createMediaElementSource throws if the same element is sourced twice. The component only closes contexts it owns.
State Flow Diagram
mounted ββWeb Audio?βββΆ live (suspended shimmer until first gesture)
β β play β rendering loop β pause β idle
βββ no Web Audio βββΆ degraded (full seek + keyboard, static canvas)Props Reference
| Prop | Type | Default | Description |
|---|---|---|---|
| audio | HTMLAudioElement |
required | Element to visualise + seek |
| audioCtx | AudioContext |
β | Shared context (we create one otherwise) |
| mode | 'waveform' | 'bars' |
'waveform' |
Time- vs frequency-domain |
| height | number |
64 |
Canvas height (px) |
| class | string |
'' |
Extra classes |
Edge Cases
- Autoplay policy: AudioContext starts suspended β a shimmer placeholder shows until the first gesture resumes it.
- No Web Audio (test envs, ancient browsers): full seek + keyboard still work; the canvas simply stays quiet.
- Rapid dragging: pointer capture keeps the seek glued to the pointer outside the canvas.
- SSR: all Web Audio work is inside
onMountβ server render is a static box.
Dependencies
Zero. Web Audio API + Canvas 2D only (no wavesurfer/tone).
File Structure
src/lib/components/WaveformSeekBar.svelte β component
src/lib/components/WaveformSeekBar.md β this doc
src/lib/components/WaveformSeekBar.test.ts β 6 DOM/a11y contract tests
src/routes/waveformseekbar/+page.svelte β demo (paired with an audio track)