Partial updates and moved elements drawn by a canvas
The tiles below are children of a canvas. Four of them hold a named range, <?start name="orders"?> to <?end?>. A stream of
<template for="orders"> elements replaces each range, in any order, with no script touching the tiles. The canvas draws every tile as its own texture, so it can
show which tile a patch is landing in, and it can fly a tile from its old place to its new one when moveBefore() reorders the list.
Show the code ranges, patches, one texture per tile, moves
- Ranges and a marker in the markup. Each tile is a
drawableelement inside a drawable child of the canvas. - The patches. Each is a
<template for>. It carries newstartandendinstructions, so the range can be patched again. - Send them. Only the
Unsafestreaming methods apply patches; the sanitising ones drop processing instructions. - Which tile changed, and which tile is new. A
MutationObserveron the grid. - Reorder with
moveBefore(). Record where each tile is drawn, move the elements, and let the canvas travel from the old place to the new one. - On
paint. Upload the tiles inchangedElements, then draw each tile at its place. - The fragment shader. A rim and a scan line on a tile that just changed, a moving band on a tile that still holds its placeholder, and the shadow of a tile in flight.
How it works
Each tile is a drawable element with its own texture. The patch buttons write <template for> elements into the document with streamAppendHTMLUnsafe(), 14 characters at a time. The browser puts each template's content into the range of the same name. A MutationObserver records which tile changed and when; the paint event's changedElements says which textures to upload again. A shuffle records where each tile is drawn, reorders the elements, and the canvas draws each quad on a path from the old place to the tile's new layout box for 600 ms.
Live checks
They run on a separate small canvas and take the focus for a moment.
| Check | Result | Detail |
|---|---|---|
A streamed <template for> replaces a range in a canvas child | not run | |
| The sanitising stream leaves the range alone | not run | |
The patch raises paint, and changedElements names the patched element | not run | |
| A drawable element added by a patch is drawn at the next paint | not run | |
moveBefore() keeps focus and animation time; insertBefore() does not | not run |
Findings
- A
<template for="name">written throughstreamAppendHTMLUnsafe()replaces the range of that name, including a range that was in the document before the stream started. The content appears in the range while the template is still open, so text streams into the middle of the page (Chromium 157). - The sanitising methods (
streamAppendHTML(),appendHTML()) remove processing instructions and patch templates, so nothing is patched. The one-shot methods (appendHTMLUnsafe(),insertAdjacentHTML()) insert the template and leave it unapplied.innerHTMLapplies a patch only to a range in the same string. - When a template closes, its
startandendinstructions are removed. A patch that should be repeatable has to write them again inside its own content. - Each chunk of a patch raises one
paintevent with no call torequestPaint(), andchangedElementsnames the tile that holds the range, not the whole document. Only that tile is uploaded again. - A
drawableelement that arrives inside a patch has a picture at the nextpaintevent and can be uploaded then. It needs no registration. moveBefore()on a tile keeps the focus and the selection of an input inside it and keeps its CSS animation running. The tile's texture is still valid after the move, so the canvas only changes where it draws it.insertBefore()on the same tile drops the focus and restarts the animation.- Moving a drawable element to a different canvas with
moveBefore()also keeps focus; the new canvas gets apaintevent and can draw it.