Captions drawn apart from the video with track.activeCues
The text track of this video is hidden, so the video draws no captions. The page reads track.activeCues, builds each cue as
HTML and the canvas draws that element by itself, away from the frame. Press Play, choose where the captions are drawn, and tap a word to
move the video to it.
The video hasn't been played yet.
Show the code a hidden track, cues as elements, three ways to draw them
- The markup. The video, a control bar and an empty caption box are children of the canvas. The
<track>has nodefaultattribute. - Follow the active cues.
track.mode = 'hidden'loads the file without showing it. Oncuechange, one element is added for each cue intrack.activeCues. - Make an element from a cue.
getCueAsHTML()returns a fragment. Time stamps inside the cue arrive as processing instructions, and each word after one gets that time. - Connect the video.
requestVideoFrameCallback()gives the media time of the frame on screen, and the words are marked for that time. - Draw. One
drawElementImage()call each for the video, the control bar and the captions. The mask mode draws the captions, then draws the frame over them withsource-in. - The CSS. The word states are attribute selectors with a colour transition.
- The whole library file.
How it works
- A text track has three modes. In
hiddenthe browser loads the file, keepsactiveCuescurrent and firescuechange, and the video element draws nothing for it. VTTCue.getCueAsHTML()turns the cue text into nodes:<v Ada>becomes<span title="Ada">and<00:00:01.626>becomes atimestampprocessing instruction./lib/cue-layer.jswraps each word in a<span>that carries the last time stamp before it.- The caption box is a second
drawablechild of the canvas. The paint handler chooses where it goes and at what size. The hit-test region follows the draw call, so a tap on a drawn word reaches its<span>, and the click handler setsvideo.currentTime. - In the mask mode the canvas clips to the caption area, draws the captions, sets
globalCompositeOperation = 'source-in'and draws the video frame over the same area. The frame is kept only where the text is. - In the third mode the canvas reads
cue.positionandcue.align(set in the file asposition:4% align:start) and puts the bubble on that side of the frame. It grows from 70% to full size over the first 0.2 s of the cue in media time. - The clip and its cue file are generated by
tools/speech-clip/make.mjs. Cue times are measured. The word times inside a cue are estimates from the number of letters.
Live checks
Each check builds its own small video and canvas in a corner of the window. The player above isn't touched.
| Result | Experiment | Detail |
|---|
Findings
From Chromium 157 headless with software video decoding, 2026-11-21. The rows above say what your browser does.
- Works: a hidden track gives the cues without drawing them. With
mode = 'hidden'the video's picture in the canvas has no caption pixels, andactiveCuesandcuechangebehave as they do for a shown track. - Works: cues as HTML in a second drawable element. The caption element can be drawn under the frame, scaled, or over the frame, and its words take taps where they are drawn.
- Works:
getCueAsHTML()keeps voices, classes and time stamps. The time stamps areProcessingInstructionnodes with the targettimestamp. Nothing else in the DOM exposes them. - Measured:
cuechangelateness. The fourth check reports how far the media time had moved past a cue's start whencuechangearrived. For a frame-exact result (an export, for example), comparetrack.cueswith the frame's media time:layer.update(time, { exact: true })does that. - Works: the frame as the fill of the text.
source-inwith a clip keeps the composite inside the caption area. The canvas background has to be CSS, because the area must be clear before the text is drawn. - Limit: native caption styling isn't available to the page.
::cue(:past)and::cue(:future)style the browser's own rendering, and that rendering stays inside the video's box. The elements here are styled with ordinary selectors. - Not verified: a hardware-decoded video on Android as a direct
drawablechild, sound in step with the lit words on a real device, and a screen reader's reading of the caption box.