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.
Show the code CSS times each tile, a shader draws it
- The markup. One
drawablewrapper holds the grid. Each tile has aview-transition-name. - The clock. The canvas is the scope and has
view-transition-name: none, so it keeps drawing. - Read the clock.
- The fragment shader and the matrix.
uPis the tile's progress. The matrix turns the tile and applies perspective. - The texture. The page has one WebGL context, held by
lib/element-gl.js. The wrapper is uploaded withtexElementSubImage2D(). - Draw each tile. The old box was measured before the change, the new box is the layout now.
- 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.
Inbox
Drafts
Sent
Show the code two scopes, one inside the other
- The markup.
- The outer scope. The canvas can't be named (its name is
none), so its wrapper is. - Start both.
- 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.
Show the code the browser moves the canvases, each canvas distorts its own row
- The markup. One canvas for each row, because a canvas child can't be a transition element and a canvas can.
- The class.
- Draw a row in strips.
- 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.
Show the code read ::view-transition-group(), draw there
- The markup.
- The names. The canvas has a name so that its live picture is shown without a cross-fade.
- Read the group's box.
- 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
- The markup. The app is one
drawableelement with a name, so it has one group to time. - Every change goes through
go(). The old picture has to be taken in apainthandler, before the transition starts. - Draw from the old picture to the live app.
- 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.
Findings
- The canvas-scoped clock drives a WebGL shader. A WebGL canvas can be the scope of a transition like a 2D one. The shader gets one number for each tile, and CSS still sets the duration and easing. The page holds one WebGL context, through
lib/element-gl.js. - Nesting works when the canvas has
view-transition-scope: allall the time. Without it, a transition on an ancestor that is already running has taken the names inside the canvas, and a transition started on the canvas then is rejected with "Snapshot capture failed". Started in the other order, or in the same task, both run without it. view-transition-classmatches canvases in a scoped transition, so one rule times a list of canvases. Each canvas stays live in its new snapshot and can distort its own content while the browser moves it.- The group box of a canvas child can be drawn at, in a document transition. The canvas has to put the child's hit box back at its destination after each draw. If it does not, the group is retargeted to the place of the last draw every frame and stops moving.
- One canvas can draw every change of an app. The old state is an
ElementImagetaken in apainthandler. It stays drawable after the DOM has changed, and can be drawn in pieces with the nine-argumentdrawElementImage(). - The old state is a picture. In experiment 5 only the new state is live and takes input during the change.
<live-transition>keeps both states live, at the cost of having both in the DOM.