A cassette deck drawn from the controls of an <audio> element
The play key, the mute key, the counter and the tape position slider below are the native controls of one <audio controls>
element, laid out by CSS and drawn piece by piece by a canvas. The canvas adds the cassette, whose reels turn with currentTime.
Press the play key, drag the slider, and use Rewind and Forward.
The tape hasn't been played yet.
Show the code native controls in a grid, drawn in pieces
- The markup. An
<audio controls>element and two buttons, each adrawablechild of the canvas. - Lay out the native controls.
::-webkit-media-controls-panelacceptsdisplay: grid, and each named part accepts a grid position, a size, a font and a colour. - Draw the element in pieces. The nine-argument
drawElementImage()copies one row or one cell of the element. The counter gets a glow, a pressed key is drawn 4 px lower, and the play glyph is drawn again, large, on the cassette label. - Put the hit-test region back. Every partial draw moves it. A last full-size draw inside an empty clip restores it.
- The cassette. Drawn with canvas paths from
audio.currentTimeandaudio.duration. - The level meters. An
AnalyserNodeonaudio.captureStream(), which doesn't change what the element plays. - Rewind and Forward. Two lines each.
How it works
- The audio element is as large as the deck: 290 px tall and as wide as the canvas. A rule on
::-webkit-media-controls-panelmakes the panel a grid of four columns and four rows. The timeline is given the second row, the two time displays the third, and the play button and the mute button two cells of the fourth. - The canvas first draws the deck and the cassette with paths. Then it copies rows and cells out of the audio element with the nine-argument
drawElementImage(element, sx, sy, sw, sh, dx, dy, dw, dh). Each copy can have its own shadow, offset or scale. - The play button's glyph is the browser's. The canvas copies the centre of that cell onto the cassette label at twice the size, so the label shows a triangle while the tape is stopped and two bars while it plays, with no state code.
- The pieces are drawn at the places the element's layout has them, so a tap on a drawn key is a tap on the native control. The last draw call restores the hit-test region to that layout.
- The reels don't use a clock. The angle and the amount of tape on each reel are functions of
currentTime, eased over a few frames, so dragging the slider winds the tape.
Live checks
Each check builds its own small deck in a corner of the window. The deck above isn't touched. Checks that play the tape do so muted.
| Result | Experiment | Detail |
|---|
Findings
From Chromium 157 headless, 2026-11-21. The rows above say what your browser does.
- Works: the native controls can be laid out as a grid.
display: gridon::-webkit-media-controls-panelandgrid-rowandgrid-columnon the named parts place the play button, the timeline, both time displays and the volume container. The first check finds the play button in the picture and compares its box with the grid cell. - Works: pieces of one element drawn with different effects. A source rectangle selects a row or a cell. Shadow, offset and scale apply to each copy.
- Works: taps reach the native parts. A real touch on the drawn play key started playback, and a touch on the drawn timeline moved
currentTime(tested with touch input from the test driver, which a live check can't produce). - Found: the element drawn last is on top for hit tests. The audio element's region covers the whole deck. When its restoring draw came after the Rewind and Forward buttons were drawn, taps on those keys went to the audio element. Drawing the two buttons after it fixed that.
- Limit: one hit-test region per element. A piece drawn far from its layout position can't be tapped there. The region can only be the whole element under one transform, which is why the pieces stay near their layout places.
- Limit: internal parts. The overflow button has only the internal name
::-internal-media-controls-overflow-button, which a page's CSS can't use, so it can't be given a grid cell.controlslist="nodownload noplaybackrate"empties its menu and the browser then leaves the button out. - Works:
captureStream()for the meters. The analyser reads the element's sound without taking over its output, so a suspendedAudioContextcosts the meters and not the sound. - Not verified: the touch layout of the audio controls in Chrome 159 on Android (part sizes and which parts exist may differ, which the first check reports), sound output, and
audio.captureStream()on Android.