Parts of the native media player in a canvas
Chrome builds the controls of <video> and <audio> as elements in a shadow tree that pages can't open. I wanted to know what
a canvas can still do with them: restyle the parts, let the player's state drive a shader, and draw the controls apart from the picture.
1. Restyle the parts
Tap the video to show its controls. Everything in the picture is drawn into a canvas.
2. The player's state drives a shader, in CSS
Muted
Pause it: the picture fades to grey over half a second. Drag the timeline: it turns to blocks while seeking. Unmute it: the label goes. No script changes the shader.
3. Controls and picture as two layers
Show the code player parts, state, layers
- 1. The markup. A plain
<video controls>in a<div fx>. - 1. The rules that restyle the parts.
- 2. State to shader, in CSS.
:has(video:paused)on the element that carriesfxsets a custom property, and a transition moves it. - 2. The effect that reads the two properties.
- 2. The fallback when the browser has no media pseudo-classes.
- 3. The markup. The video is a child of the canvas, 80 px tall.
- 3. The CSS that leaves only the controls.
- 3. An audio element as the control strip. Its controls stay visible during playback. The video follows it.
- 3. HTML controls restyled from the video's state.
:has(#lv:paused)on the canvas sets the label of the play button. - 3. The paint handler.
drawImage(video)draws the frame with a filter.drawElementImage()draws the controls, and sets where they take clicks.
How it works
- The controls can't be reached as elements. They can be reached with CSS: about twenty
::-webkit-media-controls-*pseudo-elements, and::cuefor captions. A canvas draws whatever those rules produce. - The state of the player is visible to CSS through
:paused,:seeking,:mutedand others where the browser has them.:has()carries that state to a parent, and a registered custom property carries it into a shader. - A canvas can get a video frame two ways: as part of the element's picture, with controls and captions, or bare with
drawImage(video). Part 3 uses the bare frame for the picture and reduces the element to its controls.
Live checks
Each check builds its own small canvas in a corner of the window. The players above aren't touched.
| Result | Experiment | Detail |
|---|
Findings
From Chromium 157. The rows above say what your browser does.
- Works: pseudo-element rules are drawn. Background, colour,
accent-color,border-radius,display: noneandzoomon::-webkit-media-controls-*parts all show in the canvas, on<video>and<audio>, and the restyled controls keep working. - Works behind a flag: state pseudo-classes.
:paused,:playing,:seeking,:muted,:buffering,:stalledand:volume-lockedaren't valid selectors in Chromium 157 by default. chromestatus lists them for Chrome 158. In 157 with experimental web platform features on they work, and:has(video:paused)on a canvas child changes a custom property that the shader reads.:picture-in-pictureand:fullscreenwork without the flag. Without the pseudo-classes, event listeners that set three attributes give the same result. - Works: HTML controls with no state code. In part 3 the labels of the HTML play and mute buttons and the busy ring are CSS
contentanddisplayrules under:has(#lv:paused),:has(#lv:muted)and:has(#lv:buffering). The style change alone raisespaint. Position and time still need script: CSS has no value forcurrentTime. - Works: the controls as their own layer.
object-position: -9999px 0moves the frame out of the element's box and leaves the controls on a clear background. An 80 px tall<video>is then a control strip. The canvas draws the frame fromdrawImage(video)with any filter, and draws the strip anywhere, at any size. Hit testing follows the strip. - Limit: no script access.
video.shadowRootisnullandattachShadow()throwsNotSupportedError. One part can't be givendrawable, measured, or drawn by itself. The page can't ask where the play button is. - Limit: nothing can be put inside. Children of
<video>aren't rendered, so a canvas or any HTML can't sit among the controls. HTML over the video is a sibling in the same drawn element (see HTML overlays on video). - Limit: the controls stay inside the element's box. A transform on the panel that moves it below the video is clipped. To put controls outside the picture, shrink the element to the controls (part 3) instead of moving the controls.
- Limit: the buttons of a video fade during playback. After about three seconds of playback with no pointer over the video, Chrome fades the controls and then sets the panel to
display: none.opacity: 1 !importanton::-webkit-media-controls-paneland::-webkit-media-controls-timelinekeeps the timeline. The row of buttons is::-internal-media-controls-button-panel, a name pages can't use, so no rule keeps it. It comes back when the pointer moves over the strip or the strip is tapped. - Works: an audio element as a control strip that never fades. The controls of
<audio>stay visible. An audio element playing the same file, with a muted video that copies its play, pause, seek and speed, gives native controls under a picture the canvas draws. It has no fullscreen or captions button, and the two decoders need a drift correction. - Limit: one element, one hit region. The control strip can be moved and scaled as a whole. Its buttons can't be placed one by one.
- Native against HTML controls. Native controls bring the menu, captions switch, playback speed, Picture-in-Picture, keyboard and accessibility for no code, and can only be restyled part by part. Controls written in HTML take about 25 lines for play, seek, time and mute, can be laid out and drawn piece by piece, and have none of the rest until it's written.
- Not verified here: which state pseudo-classes Chrome 159 on Android has by default, and how the touch layout of the controls (larger buttons, double-tap to seek) fits an 80 px strip.