Streamed HTML drawn as it arrives

Markup is written into an element in small chunks with streamAppendHTML(). The element is a child of a canvas, and each chunk raises the canvas's paint event. A fragment shader draws every new block through an effect and makes the block that is being written glow. The text is DOM the whole time, so you can select it, scroll it and press the button when it arrives.

Press a button above to start a stream.

The results are in the Live checks table below.

Show the code stream, track blocks, draw
  1. The element that receives the stream is a child of the canvas.
  2. Write chunks into it. streamAppendHTML() returns a WritableStream. Browsers without it get the same chunks through document.write() on a second document.
  3. Record when each block appeared and when it last changed. A MutationObserver maps every change to its top-level block.
  4. On paint, upload the element as a texture and draw it. The rectangles and ages of the blocks go to the shader as uniforms.
  5. The fragment shader. One pass. Each effect is a mask computed from a block's age.
  6. The upload. texElementSubImage2D() into a texture of exactly the element's picture size.
  7. Losing the WebGL context. The page shows the document as plain HTML until the context comes back.

How it works

The document is one drawable child of the canvas. A writer from streamAppendHTML() receives the chunks. A MutationObserver records when each top-level block appeared and last changed. On every paint event the element is uploaded as one texture with texElementSubImage2D() and drawn on one quad; the fragment shader gets the rectangles and ages of the newest blocks as uniforms and applies the effect inside them. The quad covers the element's layout box, so updateElementGeometry() gets an identity matrix.

Live checks

The button under the demo runs these. They use the demo itself, so it has to be on screen: a point outside the window can't be hit tested.

CheckResultDetail
The browser has streamAppendHTML()not run
Each chunk raises a paint eventnot run
The shader output contains the streamed textnot run
A point on a streamed paragraph hit tests to that paragraphnot run

Findings