View transitions drawn by a canvas

Five experiments in which startViewTransition() supplies the timing and a canvas draws the pictures. The content is HTML inside <canvas layoutsubtree>. Press the button under each one. The clocks show which pictures are live.

1. A WebGL shader timed by a transition scoped to the canvas

"Shuffle" calls canvas.startViewTransition() and reorders six HTML tiles. The browser animates a hidden group for each tile. The canvas reads each group's progress, turns the tile once about its vertical axis and lifts it toward the viewer. The fragment shader ripples the tile and splits its colour channels along the direction of travel.

One
Two
Three
FourHTML text
Five
Six
Show the code CSS times each tile, a shader draws it
  1. The markup. One drawable wrapper holds the grid. Each tile has a view-transition-name.
  2. The clock. The canvas is the scope and has view-transition-name: none, so it keeps drawing.
  3. Read the clock.
  4. The fragment shader and the matrix. uP is the tile's progress. The matrix turns the tile and applies perspective.
  5. The texture. The page has one WebGL context, held by lib/element-gl.js. The wrapper is uploaded with texElementSubImage2D().
  6. Draw each tile. The old box was measured before the change, the new box is the layout now.
  7. Start the transition.

2. A canvas-scoped transition inside a transition on its ancestor

The sidebar and the canvas are in a grid. One transition is scoped to the grid and resizes the sidebar. The other is scoped to the canvas and shuffles its tiles. Start them in either order. The checkbox removes view-transition-scope: all from the canvas, which changes the result of the first button.

Sidebar
Inbox
Drafts
Sent
A
B
C
D
Show the code two scopes, one inside the other
  1. The markup.
  2. The outer scope. The canvas can't be named (its name is none), so its wrapper is.
  3. Start both.
  4. The 2D drawing used by the canvas. The nine-argument drawElementImage() copies one tile out of the wrapper's picture.

3. view-transition-class with one canvas for each list item

Each row is its own canvas with one HTML child, a view-transition-name and the class row. The transition is scoped to the list and the browser moves the canvases. While it runs, each canvas draws its row in horizontal strips that bow sideways like a plucked string, further for rows that travel further.

Mirabel71
Anselm94
Zofia38
Kwame56
Ines82
Show the code the browser moves the canvases, each canvas distorts its own row
  1. The markup. One canvas for each row, because a canvas child can't be a transition element and a canvas can.
  2. The class.
  3. Draw a row in strips.
  4. Start the transition on the list.

4. The group box of a canvas child as the position to draw at

This one is a document transition. The white card is a child of the canvas with a view-transition-name. Its group has no picture, but it has the right box at every frame. The canvas reads that box and draws the card there, stretched along its direction of travel, with copies behind it. The orange label below is ordinary HTML in the same transition, moved by the browser.

Card
HTML outside the canvas
Show the code read ::view-transition-group(), draw there
  1. The markup.
  2. The names. The canvas has a name so that its live picture is shown without a cross-fade.
  3. Read the group's box.
  4. Draw at the box, then put the hit box back. Without the last line the transition doesn't move: the browser takes the place of the last draw as the card's new position and retargets the group to it.

5. One canvas draws every change of a small app

The whole app below is one HTML element in one canvas. Every change (a tab, opening a message, the theme) goes through one function. It takes an ElementImage of the app as it is, starts a transition scoped to the canvas, changes the DOM, and draws from the old picture to the live app with the chosen effect.

Show the code one function for every change
  1. The markup. The app is one drawable element with a name, so it has one group to time.
  2. Every change goes through go(). The old picture has to be taken in a paint handler, before the transition starts.
  3. Draw from the old picture to the live app.
  4. The helper that keeps the canvas painting while a transition runs.

How it works

A view transition gives two things: timing (a set of CSS animations, one for each named element) and pictures (the old and new snapshots). Inside a canvas the pictures aren't available, because the canvas renders none of its contents and no call hands a snapshot to script. The timing is available. Experiments 1, 2 and 5 scope the transition to the canvas and read the group animations as clocks. Experiment 4 uses a document transition and reads the group's box. Experiment 3 leaves the moving to the browser and uses one canvas for each moving thing.

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 under half a second. Nothing visible changes in it.

test rig

Findings