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.

Clock0.0 s
Counter
Level 40
The video appears here.
Show the code snapshots to bitmaps to an encoder
  1. The markup. Each child of <dom-rewind> is recorded.
  2. Inside the element: keep a snapshot when a child repaints.
  3. Inside the element: frames(). Each past frame is redrawn from its snapshots and copied with createImageBitmap().
  4. Encode. Each bitmap is drawn over a background colour on a plain OffscreenCanvas. A VideoFrame is made from that canvas and given to the encoder.
  5. The WebM file. The chunks from a VP8 or VP9 encoder are written into a WebM container in memory.
  6. Decode back. For the codecs with no container here, and in the live checks.
  7. 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
  1. The CSS.
  2. Build. The same pads go into one drawable element or into one each.
  3. 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.
  4. 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.
  5. The one hit box.
  6. Routing. A capture listener on the canvas stops the browser's press and sends a copy to the element under the moved hit box.
  7. 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