Rewind to video, and two pointers on moving pieces
Two experiments that were left open on the time scrubber and the text particles. The first encodes the recorded past of live HTML as a video file. The second presses moving pieces of HTML with two pointers at once and reports which piece the browser sent each one to.
1. The history of <dom-rewind> fed to a video encoder
The three cards are HTML inside <dom-rewind>, which keeps the last 12 seconds as ElementImage snapshots. Drag a card, press its button or move its slider.
Then encode the last few seconds. Nothing was being recorded as video while you did it: the frames are redrawn from the snapshots afterwards.
Show the code snapshots to bitmaps to an encoder
- The markup. Each child of
<dom-rewind>is recorded. - Inside the element: keep a snapshot when a child repaints.
- Inside the element:
frames(). Each past frame is redrawn from its snapshots and copied withcreateImageBitmap(). - Encode. Each bitmap is drawn over a background colour on a plain
OffscreenCanvas. AVideoFrameis made from that canvas and given to the encoder. - The WebM file. The chunks from a VP8 or VP9 encoder are written into a WebM container in memory.
- Decode back. For the codecs with no container here, and in the live checks.
- The two buttons.
2. Two pointers at once on moving pieces
Eight pads move in one canvas. Each pad is a <button> that counts its own pointerdown events. Press two pads at the same time with two fingers.
A green ring means the pad under that finger ran its listener. A red ring means it did not. With a mouse, use the button under the canvas, which asks the browser about two pads in one task.
Show the code one hit box, eight hit boxes, or routing
- The CSS.
- Build. The same pads go into one
drawableelement or into one each. - Where each pad is. One matrix for each pad. The canvas uses its inverse to say which pad is under a point. That's the answer the browser's target is compared with.
- Draw. With one element, each pad is a nine-argument
drawElementImage()of its rectangle, followed by one draw of the whole element at alpha 0, without which only the last rectangle drawn takes hits. With one element each,updateElementGeometry()places every hit box. - The one hit box.
- Routing. A capture listener on the canvas stops the browser's press and sends a copy to the element under the moved hit box.
- Two questions in one task.
How it works
Experiment 1. <dom-rewind> stores, for each frame, an ElementImage of every child and where it was drawn. Its frames() method draws each stored frame again and yields an ImageBitmap.
encodeRewind() draws the bitmap on a plain canvas, makes a VideoFrame and encodes it. The work isn't real time: four seconds of history can take less or more than four seconds.
The second button is the other route: the element replays the history on screen and MediaRecorder records the canvas stream while it plays, which takes exactly as long as the clip.
Experiment 2. The browser picks the target of a pointerdown before any script runs, using the hit boxes as they are at that moment. A drawable element has one hit box, set by the last draw or by updateElementGeometry().
When one element is drawn in eight places, seven of them have no hit box. A nine-argument draw also limits the hit box to the rectangle that was drawn, so the canvas ends each paint with a draw of the whole element at alpha 0. One element for each pad gives each place its own. The third mode keeps one element and does the browser's part in script: for each pointer it moves the box, asks what is there and dispatches a copy of the event.
Live checks
These wait for a tap. They build small fixtures in a panel at the bottom of the window, encode about one second of video several times and take about 20 seconds. They don't touch the two experiments above.
Findings
- The recorded past of HTML can be encoded as video.
frames()gives oneImageBitmapfor each past frame, aVideoFrameis made from a plain canvas copy, and the VP9 or VP8 chunks go into a WebM file that a<video>plays. The drawable canvas is never read withgetImageData(). - The encoder route isn't tied to real time. It encodes history that was never recorded as video. In headless Chromium 157, 121 frames at 720 by 568 took 0.6 s for 4 s of video. VP9, VP8, AV1 and H.264 encoders all accepted the frames and their output decoded back to the same number of frames. The
MediaRecorderroute has to replay the history on screen and takes as long as the clip. - A video has no live content. The file holds pictures. The button in it can't be pressed and the clock doesn't run.
- One
drawableelement answers for one place at a time. With every pad in one element, three two-finger presses on six pads reached one pad (headless Chromium 157, touches sent through the DevTools protocol). None of the three second fingers reached its pad. - A
pointerdownis decided before the hit box can move, aclickis not. A single tap on a pad that the hit box wasn't under ran nopointerdownlistener, but theclickthat followed went to the right pad, because the canvas had moved the box in between. Two fingers down at once produce noclick. - One element for each piece makes two pointers right. Each piece has its own hit box, placed with
updateElementGeometry()every frame. The same six presses reached six pads. - A canvas can route pointers itself with one element. It moves the hit box under each pointer in turn and asks
elementFromPoint(). Six of six presses reached their pad, and the browser's own target was wrong for four of them. The pad receives a copy of the event, soisTrustedis false, the browser's own pointer capture stays with its first target and:activedoesn't apply. - A nine-argument draw limits the hit box to its source rectangle. After drawing parts of an element, only the last part drawn takes hits, wherever
updateElementGeometry()puts the box. A further draw of the whole element at alpha 0 makes all of it take hits again. - A moved hit box needs an attribute change before the next question. Without it,
elementFromPoint()at a point that was asked before gives the old answer.