Media controls and element state
Four ideas that came out of the parts of the native media player. That page found that a canvas can restyle the
native controls and can't reach any one of them. I wanted to know what a canvas could do if media controls were ordinary elements with
named parts and states, whether the state of any element can drive an effect through CSS alone, whether
drawElementImage() takes a pseudo-element or a part, and whether the composed picture can be sent to another screen.
Media controls as a custom element with parts and states
<media-bar> is a mock of customisable media controls: a play button, a timeline, a time and a sound button in an open shadow tree, each with a part name, and custom states for playing and muted. Press Play. The film takes its colour back, because a CSS rule reads media-bar:state(playing). Then tick the box: the canvas stops using the element's layout and draws each part where it wants it, and each part still takes input where it is drawn.
The glass behind the controls is the film drawn a second time with a blur, inside the box of the control. With the box ticked there is no bar: the play button is a part of the shadow tree drawn at more than twice its size in the middle of the film, and the timeline is stretched to the width of the canvas. The component's author made that possible by putting drawable on the parts. A page can't do it for a part it doesn't own.
Show the code
- The video and its controls are two
drawablechildren of the canvas. - The custom element: parts, custom states, and
drawableon each part when asked. - The rule that turns the playing state into
--fx-amount, and the part styles. - Draw the film, the glass and the controls, as one picture or as four.
- Read the numbers, and ask for frames while a transition runs.
State pseudo-classes that drive an effect through :has()
Tick Lamp. Open the opening hours. Press Save with the email field empty, or type something that isn't an address and leave the field. Press Help. Each of the four changes how the card is drawn, and the script has no listener on any of the four elements: a CSS rule for each state sets a number on the canvas.
The numbers have the names that Shader CSS uses for its uniforms. Here a 2D paint handler reads them, so the same rules could drive a shader. A state that belongs to an element outside the canvas works the same way, because :has() is written on an ancestor of both.
Show the code
- The form is one
drawablechild of the canvas. - Four rules, one for each state.
- The four numbers, registered so that they can be eased.
- Draw the card from the four numbers.
- Read the numbers, and ask for frames while a transition runs.
A pseudo-element as a picture of its own
The proposal under test: drawElementImage() that takes a pseudo-element or a ::part(), so that a canvas could draw the quotation mark of this quote apart from its text. The line under the canvas says what the browser did with that call. The drawing shows the way round it, which needs the page to know where the pseudo-element's box is.
A part of an open shadow tree is different: it's an element, and the first demo draws four of them. What's missing is a way to name a box that isn't an element, or an element in a tree the page can't open.
Show the code
- The quote is one
drawablechild. Its mark is a::beforebox. - Hand the pseudo-element to
drawElementImage(). - The way round it: two clipped draws of the whole element.
- Styles. The mark has a fixed box.
Casting the composed canvas
The first canvas on this page, film and controls together, as a stream for another screen. Three routes are tried. The Remote Playback API is asked to play a <video> that shows the stream. The Presentation API is asked to open a receiver page on a second screen. The third route opens the same receiver page in a tab, which works with one device: press Play in the first demo, open the receiver, and come back here.
A receiver page gets messages and no media, so the stream travels over WebRTC and the Presentation connection, or the BroadcastChannel, carries only the offer and the answer. The controls in the stream are pixels: the receiver can't press Play.
Show the code
- Capture the canvas.
- Remote Playback API.
- Presentation API.
- Send the stream over WebRTC.
- The receiver in a tab.
- The receiver page.
How it works
- Custom states are selectors.
<media-bar>adds and removes names inElementInternals.statesfrom the video's events. A rule on the canvas,:has(media-bar:state(playing)), sets--fx-amount, and a transition on the canvas eases it. - The canvas reads the numbers. A change of a custom property raises no
paintevent. The canvas listens fortransitionrunandtransitionendon itself and asks for a frame at a time between them. When no transition runs, nothing runs. - Parts as pictures.
drawElementImage()takes any element that has thedrawableattribute and is a descendant of the canvas, and an element in an open shadow tree of a canvas child counts. The component sets the attribute on its parts when it has theliftattribute. - Pseudo-elements. There is no call that takes one, so the mark is drawn by clipping a draw of its whole element to the mark's box.
- Casting.
canvas.captureStream()gives a video track of the drawn canvas. The sender adds it to anRTCPeerConnectionand exchanges one offer and one answer with the receiver.
Show the code
- Each demo starts when its section comes near the screen.
- The paint handler shared by the three canvases.
- A lost context stops every demo.
- The elements are then shown as plain HTML.
Live checks
The checks wait for a tap. Each builds its own small canvas in a corner of the window, and none of them touches the demos above. :user-invalid isn't checked here, because it needs real input: the tool script types into the field.
Findings
Measured in headless Chromium 157 on 2026-11-21. Not yet run on a phone.
- Media controls built as a custom element can be drawn part by part. An element in an open shadow tree of a canvas child is drawn as its own picture by
drawElementImage()when it has thedrawableattribute. The play button was drawn at 2.2 times its size and the timeline stretched to 335 px, and a touch on each reached the part where it was drawn: the play part's hit region was more than 55 px tall, and a tap at three quarters of the stretched timeline moved the film to 7.7 s of 10. - A part without
drawablecan't be drawn. The call threwInvalidStateError: descendants passed todrawElementImage()must have the attribute. Only the component's author can add it, so this doesn't help with the native controls or with a closed tree. drawElementImage()doesn't take a pseudo-element.Element.pseudo('::before')returns aCSSPseudoElementin this browser, and passing it threwTypeError. So did aShadowRoot. The quotation mark is drawn apart by clipping a draw of the whole quote to a box whose place the style sheet fixes. A pseudo-element with no fixed box couldn't be found this way.- Custom states work as inputs to an effect.
canvas:has(media-bar:state(playing))set--fx-amountto 1 while the fixture video played and back to 0 after pause, with no listener outside the component. :checked,details:open,:user-invalidand:popover-openeach drove a number through:has(). The first three belong to elements drawn in the canvas. The popover is a sibling of the canvas, and its rule is written on their common ancestor.:user-invalidmatched after text that isn't an address was typed and the field was left, and after Save was pressed with the field empty.- A transition of a registered custom property raises no
paint. It raised onetransitionrunand onetransitionendon the canvas and 0 paint events in 0.5 s, with the number at 0.44 after 150 ms of 300. The canvas asks for frames between the two events and for none afterwards. - The composed canvas can be sent as a stream.
captureStream()on the drawable canvas gave one video track, and the film with its controls arrived in the receiver page in a second tab at 378 by 288 over WebRTC, with a BroadcastChannel carrying the offer and the answer. The controls in the stream are pixels. - No cast device was available to the test browser. Remote Playback reported that no device is available for a video that shows the canvas stream, and
prompt()was rejected withNotAllowedError.PresentationRequest.getAvailability()gave false andstart()was rejected withNotFoundError: no screens found. The Presentation route of the receiver page hasn't run. - Not verified: everything on a phone, and both cast routes on a network with a Chromecast or a second screen. Whether Remote Playback offers a device at all for a video whose source is a
MediaStreamisn't known. In one run the frame in the receiver tab showed the glass boxes without the play, time and timeline parts while the sending tab was in the background. In a single tab the stream matched the canvas.