View Transitions API with HTML-in-canvas

Four experiments with document.startViewTransition(), view-transition-name, element.startViewTransition() and cross-document transitions, each with a <canvas layoutsubtree> in the page. The live transitions page doesn't use this API. This page does.

1. A canvas that draws HTML, inside a view transition

The green area is a canvas. The white card is a child element of the canvas, drawn with drawElementImage() at a slight angle. "Swap layout" changes the grid inside a view transition. Choose which element has a view-transition-name and where the transition starts.

Card in a canvas
1
2
3
Show the code a canvas with HTML children in a view transition
  1. The markup. The card is a child of the canvas, so the page doesn't paint it. The canvas does.
  2. The names. The canvas is a replaced element, so it can carry a view-transition-name like an image can.
  3. Draw the card. 2D drawElementImage() also sets where clicks on the card land.
  4. Start the transition. startViewTransition() exists on document and, in Chromium 157, on every element.
Half way: the canvas cross-fades and the card shows in both places
Name on the canvas, half way through. Headless Chromium 157.
Half way with only the card's own group shown: nothing is drawn
Name on the card, half way, with every other group hidden. The card's group draws nothing.
A transition started on the canvas element: the canvas area is empty
Started on the canvas element, half way. The canvas area is empty.

2. A shader in place of the cross-fade

The box below is a <live-transition> element with a view-transition-name. The view transition moves the box. The old snapshot is hidden and the new one isn't faded, so what shows is the live canvas, and the canvas runs a shader from the first state to the second.

First state

Moved by the view transition.

Second state

Drawn by a shader.

side
Show the code the view transition moves the box, the canvas draws the change
  1. The markup.
  2. Turn off the cross-fade for this name. The new snapshot of a view transition is live, so a canvas that keeps drawing shows through it.
  3. Prepare the shader first, then start both. Rendering is paused while the update callback runs, so anything that waits for a paint event has to happen before startViewTransition().

3. Between two documents

Two pages opt in with @view-transition { navigation: auto }. Each has a canvas with the same view-transition-name. The second link also passes the old page's canvas pixels to the new page, which burns from them to its own content.

Open the two pages, cross-fade
Open the two pages, shader between documents

4. A transition scoped to the canvas, drawn by the canvas

The six items below are HTML in a grid, inside one canvas. "Shuffle" calls canvas.startViewTransition() and reorders them. The browser makes a group for each named item but shows none of them, because they're inside the canvas. The canvas reads each group's animation as a clock and draws the item on its way from the old box to the new one, with an arc and a turn that a group animation couldn't add to live HTML.

One
Two
Three
FourHTML text
Five
Six
Show the code CSS times the move, the canvas draws it
  1. The markup. One drawable wrapper holds the grid, so CSS grid lays the items out. Each item has a view-transition-name.
  2. The clock. The group's own keyframes are replaced by one that animates a registered custom property from 0 to 1. Duration, easing and per-item delay stay in CSS.
  3. Start the transition on the canvas. Measure the old boxes first. The new ones are the layout after the callback.
  4. Draw each item. The nine-argument drawElementImage() copies one item's rectangle out of the wrapper's picture to wherever the clock says it is.

Live checks

These run when this section is on screen, on the fixtures in the box below. Two of them start a transition on the whole document for a moment. Nothing visible changes in it.

test rig
child
A
B

Findings

Not tried yet

References