Shaders that join the native controls and the video frame
One <video controls> gives a WebGL canvas two textures: its controls, and its frame. One fragment shader reads both.
The page also finds where the play button, the timeline and four other parts are, although no script can reach them, so an effect can start at one part.
The controls are the browser's own and work as usual: tap the video to show them, then play, seek, mute, open the menu.
Show the code two textures from one video, finding the parts, the shader
- The markup. One video in a drawable element that fills the canvas.
- The CSS that leaves only the controls in the element's picture.
- Two textures.
texElementSubImage2D()for the controls andtexImage2D(video)for the frame, in thepainthandler. - Finding the parts: the CSS.
- Finding the parts: the readback.
- The fragment shader. Six effects.
uPartsholds the boxes that were found.
How it works
- The video is a child of the canvas and fills it.
object-position: -9999px 0moves its frame out of its box, so the browser paints only the controls for it. That picture goes into one texture. texImage2D(video)on the same element still gives the frame. That's the second texture. One decoder serves both.- The controls are drawn where the element lies, so taps reach them with no extra work.
- To find one part, the page adds a class for a single paint. The class gives each part a flat colour through its pseudo-element. The page uploads that picture to a spare texture, reads it back, and takes the bounding box of each colour. The spare texture is never drawn to the screen.
- The shader gets the boxes as uniforms, with the playback position and the time since the last play or pause.
Live checks
Each check builds its own small player in a corner of the window. The player above isn't touched.
| Result | Experiment | Detail |
|---|
Findings
From Chromium 157 with software rendering. The rows above say what your browser does.
- Works: controls and frame as two textures from one element. A shader can refract the frame through the controls, fill the controls with the frame, or light the frame from a control.
- Works: one part used as a shape. With the box of the play button known, the shader reads its glyph out of the controls texture and draws it at any size. The glyph is the browser's, so it changes between play and pause without any code here.
- Works: the position of one part. A colour per pseudo-element and one readback give the box of the play button, timeline, mute button, fullscreen button and both time displays. The boxes are measured, so they follow the layout the browser chose, including a touch layout.
- Works: the browser's own fade is an input. During playback the browser fades the buttons. The fade arrives in the texture as alpha, so the melt effect shrinks the shapes with it.
- Works: everything the player paints is in the controls texture. The loading ring and the menu are there too, so the effects apply to them with no extra code.
- Limit: parts with no pseudo-element name can't be found. The menu button and the row that holds the buttons have internal names only.
- Limit: one part can't be drawn by itself. The shader can treat the area of a part differently. It can't have the play button as a texture of its own. A second copy of the element with every other part set to
display: nonewould give that, at the cost of another video element. - Limit: the boxes go stale. They are measured after loading, on resize and when the controls come back. A layout change between those moments isn't seen.
- Not built: a WebGPU version of this shader. The page uses WebGL 2.
- Not verified here: a hardware-decoded frame through
texImage2D(video)on Android, the boxes found in the touch layout of the controls, and the frame rate on a phone.