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. 1. The markup. A plain <video controls> in a <div fx>.
  2. 1. The rules that restyle the parts.
  3. 2. State to shader, in CSS. :has(video:paused) on the element that carries fx sets a custom property, and a transition moves it.
  4. 2. The effect that reads the two properties.
  5. 2. The fallback when the browser has no media pseudo-classes.
  6. 3. The markup. The video is a child of the canvas, 80 px tall.
  7. 3. The CSS that leaves only the controls.
  8. 3. An audio element as the control strip. Its controls stay visible during playback. The video follows it.
  9. 3. HTML controls restyled from the video's state. :has(#lv:paused) on the canvas sets the label of the play button.
  10. 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

Live checks

Each check builds its own small canvas in a corner of the window. The players above aren't touched.

ResultExperimentDetail

Findings

From Chromium 157. The rows above say what your browser does.