The frame
A frame is one waveform: it draws a sound on a canvas inside your element, follows an audio or video element while it plays, and lets people seek it. Everything else in SonicFrame is built on it.
Making one
import { SonicFrame } from 'sonicframe';
const frame = new SonicFrame({
container: document.querySelector('#wave'),
media: document.querySelector('audio'),
peaks: [0.1, 0.4, 0.8, 0.5, 0.2],
});
SonicFrame.create(options) does the same as
new SonicFrame(options). Only
container is required; a frame with no
media shows a position you set with
setProgress(ratio).
Your element keeps its size
SonicFrame adds a canvas inside the container and fills it. The container keeps the size your CSS gives it, so give it a height; the width follows the page. When it changes size, the wave is drawn again, with every bar on whole device pixels, so it stays sharp at any zoom or screen density.
#wave {
height: 3rem;
}
frame.destroy() removes the canvas and
gives the element back exactly as it was. It is safe to
call twice.
Following the sound
With media bound, the played part moves
smoothly while the sound plays, from one animation loop
shared by every frame on the page, which runs only while
something plays on screen. A paused page costs nothing.
-
bind(media)follows another element, or none (null). -
duration(an option, orsetDuration(seconds)) is used until the media knows its own length. -
rewindOnEnd: trueshows the start again when the sound ends.
Lazy by default
A frame keeps its canvas pixels only while it is on or
near the screen, and asks for its peaks the first time
it gets there. A page with hundreds of recordings stays
light. lazy: false draws everything at
once.
A picture, or a control
By default the frame is a slider: a click, a drag (touch
included) or the keys seek. interact: false
turns it into a picture only, marked
role="img", for lists where the row itself
opens the sound.
Try it
Next steps
- Peaks: where the shape comes from.
- Playing and seeking: the methods.
- Frame options: every option, with its default.