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
- The element that receives the stream is a child of the canvas.
- Write chunks into it.
streamAppendHTML()returns aWritableStream. Browsers without it get the same chunks throughdocument.write()on a second document. - Record when each block appeared and when it last changed. A
MutationObservermaps every change to its top-level block. - On
paint, upload the element as a texture and draw it. The rectangles and ages of the blocks go to the shader as uniforms. - The fragment shader. One pass. Each effect is a mask computed from a block's age.
- The upload.
texElementSubImage2D()into a texture of exactly the element's picture size. - 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.
| Check | Result | Detail |
|---|---|---|
The browser has streamAppendHTML() | not run | |
Each chunk raises a paint event | not run | |
| The shader output contains the streamed text | not run | |
| A point on a streamed paragraph hit tests to that paragraph | not run |
Findings
streamAppendHTML()raises onepaintevent per chunk with no call torequestPaint()(Chromium 157). The page counts the events under the stage.- The
paintevent doesn't say which block is new:changedElementslists drawable children of the canvas, and the whole document is one child. AMutationObserversupplies the per-block timing. - The document scrolls inside its own box while it's drawn through the shader, and it keeps to the bottom while text arrives. Block rectangles are measured every frame, so the effect stays on its block during a scroll.
- Chromium 153 has no
streamAppendHTML(). Writing the chunks withdocument.write()into a second document, after moving that document's container into the canvas child, gives the same result there. - Not verified here: the on-device model. The test browser reports the Prompt API as downloadable and can't download it, so the model button has only been run against a stub.