A <video> element inside a canvas
The player below is a <div fx> that holds a <video controls> with a captions track, and an HTML
label with a button. All of it is drawn into a canvas through a fragment shader. Use the video's own controls and the button, then
change the source and the effect.
Frame
<video> that isn't in a canvas:Show the code a video under a shader
- The markup. The
fxattribute puts the<div>and everything in it into a canvas. The video has nodrawableattribute of its own. - The CSS.
::cuestyles the captions. They're drawn into the canvas with that style. - Two effects written for this page. Each is a GLSL function and the same function in WGSL.
tex(p)samples the element, video frame included. - Reading the frame number.
requestVideoFrameCallback()runs once per presented frame. Writing the number into the label changes the DOM, and the canvas is repainted with the same frame. - Changing the source. The cross-origin and encrypted sources are here to show what the canvas leaves out.
- Clear Key. The licence is a JSON object made in the page.
- Inside the library: the upload and the draw.
How it works
- A
<video>is drawn like any other element. The picture taken bydrawElementImage(),texElementSubImage2D()ordrawElementImageToTexture()holds the current frame, the native controls and the captions. - No render loop is needed for the video. The canvas gets a
paintevent for every new video frame. - The native controls are part of the element, so hit testing reaches them wherever the element's geometry is. The
fxlibrary leaves the element where layout put it, so the play button and the timeline work under the shader.
Live checks
Each check builds its own small canvas in a corner of the window. The player above isn't touched.
| Result | Experiment | Detail |
|---|
Findings
From Chromium 157 with software video decoding and software rendering. The rows above say what your browser does.
- Works: frames, controls, captions. A playing video draws its current frame. Captions from a
<track>are drawn with their::cuestyle. The controls are drawn when the element shows them. - Works: lazy loading counts a canvas child as near the viewport. A
<video loading="lazy">inside a canvas that is in the window loads and is drawn. - Works: a frame drawn through a canvas counts as rendered. A video in an iframe with
allow="media-playback-while-not-visible 'none'"keeps playing when the iframe is a canvas child. Reading the canvas back after a repaint of that iframe crashed the tab in the check, so the check reads only the video's clock. Smaller cases didn't crash and the cause isn't known. - Works: the canvas follows the video clock.
paintfired 30 times a second for a 30 fps clip with no call torequestPaint(). The frame in the canvas was the framerequestVideoFrameCallback()had just reported in 40 of 40 samples, and afterseekedit was the frame asked for in 10 of 10. - Works: input. Play, the timeline, mute and the overflow menu were pressed on the drawn controls, and the menu is drawn in the canvas too. A click on the timeline also seeks with the element drawn at half size in another place. Fullscreen and Picture-in-Picture were entered from script.
- Works: alpha. A VP9 clip with an alpha channel is drawn with it, so the content behind the video shows.
- Works: a camera stream in
srcObjectdraws like a file (tested with Chromium's fake camera). - Left out: cross-origin video without CORS. The video plays, but its pixels are missing from the element's picture and the area is clear. The canvas isn't tainted, so
toDataURL()andgetImageData()still work. Withcrossoriginon the element and a CORS header on the file it is drawn. - Left out: encrypted video. A Clear Key clip plays in a plain element and is black in the canvas.
drawImage(video)gives the same black frame. Widevine wasn't tested. - Left out: a nested
drawable. A video with its owndrawableattribute inside adrawableparent is missing from the parent's picture. Draw it with its own call, or leave the attribute off. - Picture-in-Picture empties the element. While the video is in a Picture-in-Picture window the element paints a black placeholder, the canvas shows that, and
paintnearly stops (2 events in 500 ms). - A canvas can play its own stream. A child
<video>whosesrcObjectis the canvas's owncaptureStream()draws, and the picture repeats inside itself. - Not verified here: hardware-decoded video on a phone, HDR video, and the WebGPU route on screen. The first four live checks answer the first on your device.