View transitions and performance
What a view transition costs: snapshots, paused rendering and pseudo-element animations. How to keep it short, and when to skip it.
- 01Read what a transition costsKnow when Chrome takes its snapshots, why the screen freezes while the update callback runs, and what each view-transition-name adds.
- 02Keep a transition fastDo slow work before startViewTransition(), name only the elements that need it, and keep the animation short.
- 03Know when to skip itRespect prefers-reduced-motion, and know what a cross-document transition costs the next page.
The leaf stopped too
Asha is back on Leaf and Kettle's mint tea reviews. In lessons 32.1 and 32.2, the small leaf next to the title kept swaying. It swayed even while Ravi's ranking code kept the page busy.
Ravi has added a new button: Most helpful first. It runs his ranking code, then puts the 1,000 reviews in their new order. He wanted the change to look smooth, so he wrapped it in a view transition. A view transition animates a page from how it looked before a change to how it looks after. For how the API works, see View transitions in the React course.
Asha taps the button. Nothing happens. The leaf stops too, in the middle of its sway. Almost a second later, the new order fades in. Here is what she saw, in real frames from Chrome:
Here are the main numbers as code:
// Asha taps "Most helpful first". From Chrome's trace, in ms after her tap.const callback = { from: 115.9, to: 596.6 }; // Ravi's update callbackconst noNewFrame = { from: 135.5, to: 935.5 }; // the screen didn't change at allconst listChanged = 952.2; // the first frame with the new order in it console.log(`The update callback ran for ${(callback.to - callback.from).toFixed(1)} ms.`);console.log(`No new frame for ${noNewFrame.to - noNewFrame.from} ms.`);console.log(`The list first changed at ${listChanged} ms.`);- Line 1 is a comment. A trace is Chrome's detailed record of what it did, as in lesson 7.2. A millisecond (ms) is a thousandth of a second.
- Line 2 holds when Ravi's update callback started and ended. That's the function he gave to
startViewTransition(). It changes the page. - Line 3 holds the stretch when Chrome showed no new frame at all. A frame is one picture on the screen.
- Line 4 holds the first frame with the new order in it.
- Line 6 prints
The update callback ran for 480.7 ms.Line 7 printsNo new frame for 800 ms. - Line 8 prints
The list first changed at 952.2 ms.
So for 800 ms, nothing on the screen changed. Not the list, and not the leaf. In lesson 32.1, the compositor kept the leaf swaying through the same ranking code. What's different now?
A view transition works in a fixed order:
- Chrome takes a picture of the page as it is. This picture is called a snapshot.
- Chrome pauses rendering. It stops drawing new frames.
- Chrome runs the update callback, which changes the page.
- When the callback is done, Chrome looks at the new page and starts drawing again.
- It animates from the old picture to the new page.
The pause keeps the old picture on screen while the page changes behind it. And Chrome pauses the compositor's frames too. Its source says pausing rendering "pauses both main and impl frames to ensure no visual updates are presented." Impl frames are the compositor's own frames. That's why the leaf stopped.
At a theatre, the crew changes the set between scenes. First a painted curtain comes down. It shows the last scene, so the audience doesn't watch the crew at work. When the set is ready, the curtain fades away. A quick crew means a short wait. A slow crew means the audience stares at a painting.
- In real life: The painted curtain
- In JavaScript: The old snapshot, ::view-transition-old
- In real life: Lowering the curtain
- In JavaScript: Chrome pausing rendering
- In real life: The crew changing the set
- In JavaScript: The update callback
- In real life: The curtain fading away
- In JavaScript: The animation to the new page
- In real life: A slow crew
- In JavaScript: Ravi's ranking code inside the callback
A view transition photographs the page, pauses rendering while your update callback runs, then animates from the old picture to the new page. Everything inside the callback is time the visitor stares at a still picture. So keep the callback short, name few elements, keep the animation short, and skip it for people who ask for less motion.
This lesson takes each cost in turn: the snapshots, the update callback, moving between pages, the animation, and reduced motion.
How the course recorded it
- The browser: Chrome 157, on a screen the size of Asha's phone, 412 by 823 px. Its CPU was slowed 4 times, as in lessons 32.1 and 32.2. That slows the main thread only.
- The page: Ravi's page from lesson 32.2, with 1,000 reviews and the leaf. A new row under the title holds the button and a link to the Mint tea page in the shop. The ranking code is the same stand-in as in lesson 32.1: 400 ms of work. Each version changes one thing.
- The taps: a real mouse click on the button, on a freshly loaded page. Five taps per version, taking turns. In the first round, Chrome's GPU process did extra one-time work in some transitions. So the numbers come from the other four taps: the slower of the two middle ones.
- The frames: Chrome's own screenshot of every frame it showed, and its view transition events in the trace. Your own times will differ.
- The visits: a click on the link to the Mint tea page, five per version. The tool that drives Chrome turns off paint holding. The course turned it back on, as in a normal Chrome. Paint holding keeps the old page on screen until the new one paints.
Snapshots and the transition pseudo-elements
Here is Ravi's first click handler:
document.querySelector("#sort").addEventListener("click", () => { document.startViewTransition(() => { const order = rankReviews(); list.append(...order); });});- Line 1 listens for clicks on the button.
- Line 2 starts a view transition. Its argument is the update callback.
- Line 3 runs the ranking code and gets the new order. Line 4 moves the cards into it.
The CSS View Transitions spec says what happens next. First, Chrome captures the old state. For every element with a view-transition-name, it takes a snapshot of that element. The page itself always has a name: root.
Then the spec turns on rendering suppression: no new frames. During it, every tap goes to the html element. Chrome runs the update callback in a task of its own. When the callback's promise settles, the pause ends and Chrome captures the new state.
A snapshot isn't taken with a camera. Chrome marks parts of a frame it draws anyway, and its GPU process keeps a copy. The new state isn't a copy at all. Chrome's team puts it this way: "the new snapshot is not a screenshot but actually a live representation".
Last, Chrome builds a tree of pseudo-elements: parts of the page that CSS can style, but that aren't in your HTML. Here is the tree for Ravi's page:
::view-transition
└─ ::view-transition-group(root)
└─ ::view-transition-image-pair(root)
├─ ::view-transition-old(root)
└─ ::view-transition-new(root)- Line 1,
::view-transition, sits on top of everything and covers the screen. - Line 2, the group, moves and resizes from the old place to the new one.
- Line 3, the image pair, holds the two pictures together.
- Line 4 is the old snapshot. It fades out. Line 5 is the live new page. It fades in.
The spec's style sheet gives every group a quarter-second animation: animation-duration: 0.25s. Chrome's trace showed 5 animations on Ravi's page: the group's move, the two fades, and two that blend the pictures. Chrome reported no reason the two fades couldn't run on the compositor. For the other three, it gave the reason "Animation has no visible change": their values stay the same.
Every name costs
Ravi wanted the cards to glide to their new places, not just fade. A card moves on its own only if it has its own name. So he gave every card one:
for (const card of list.children) {
card.style.viewTransitionName = card.id;
}Line 1 goes through all 1,000 cards. Line 2 gives each card its id as its name, like review-1.
Each name means one more snapshot, one more group of four pseudo-elements, and five more animations. Chrome captures every named element that the page lays out, on screen or not.
With 1,000 names, the course watched for 10 seconds. The list never changed. Chrome's trace showed one Recalculate style step that began 476 ms after the tap, and hadn't ended. The transition never got as far as its snapshot.
So Ravi named only the cards near the top: the first four before the sort, and the first four after it.
document.querySelector("#sort").addEventListener("click", () => { const order = rankReviews(); const seen = [...list.children].slice(0, 4).concat(order.slice(0, 4)); for (const card of seen) card.style.viewTransitionName = card.id; const transition = document.startViewTransition(() => list.append(...order)); transition.finished.then(() => { for (const card of seen) card.style.viewTransitionName = ""; });});- Line 2 runs the ranking code, before the transition.
- Line 3 picks the first four cards of the old order and of the new one. Three cards fit on Asha's screen, so four covers them.
- Line 4 names them. Line 5 starts the transition, whose update callback only moves the cards.
- Lines 6 to 8 take the names away when the animation ends.
The course measured both versions:
// One tap each, held still once the animation began. From Chrome.const versions = [ { named: "the page only", animations: 5, gpuMbMore: 37.1 }, { named: "the page and 8 cards", animations: 45, gpuMbMore: 54.3 },];for (const v of versions) { console.log(`${v.named}: ${v.animations} animations, ${v.gpuMbMore} MB more`);}- Lines 3 and 4 hold the animations each transition started, and its extra GPU memory.
- Line 7 prints
the page only: 5 animations, 37.1 MB moreandthe page and 8 cards: 45 animations, 54.3 MB more.
The memory is the GPU process's, while the transition was on screen. The course held each transition still once its animation began. Then it asked Chrome for a memory dump. Eight cards cost about 17 MB more.
Chrome's source limits each capture to twice the screen's longer side. More "would significantly add to the memory overhead". Why it matters: name only the elements that must move on their own. Name them just before the transition, and take the names away after it.
At a party, a photographer can take one photo of the whole room. Or she can take a portrait of every guest, so each one can go anywhere in the album later. A few portraits are quick. A thousand keep everyone waiting, and fill the album.
- In real life: The photo of the whole room
- In JavaScript: The root snapshot
- In real life: A portrait of one guest
- In JavaScript: A snapshot of one named element
- In real life: Placing a portrait somewhere new in the album
- In JavaScript: A group animating one card to its new place
- In real life: A thousand guests waiting in line
- In JavaScript: 1,000 names: no change within 10 seconds
document.querySelector("#sort").addEventListener("click", () => {
const order = rankReviews();
document.startViewTransition(() => list.append(...order));
});925 ms242.7 ms537.1 MB moreOnly the page itself is captured: one snapshot, one group, and its animations.
Keep the update callback short
In Ravi's first version, the ranking code ran inside the update callback. So it ran while rendering was paused. What if it ran before startViewTransition()?
document.querySelector("#sort").addEventListener("click", () => { const order = rankReviews(); document.startViewTransition(() => list.append(...order));});Line 2 runs the ranking code first, while Chrome still draws frames. Line 3 starts the transition. Its update callback only moves the cards.
The course tapped three versions: Ravi's first, this one, and the page with no transition at all.
// Middle of four taps each. Times in ms after the tap.const taps = [ { version: "no transition", listChanged: 757.2, eventTiming: 768 }, { version: "ranking inside the callback", listChanged: 952.2, eventTiming: 136 }, { version: "ranking before it", listChanged: 925, eventTiming: 560 },];for (const t of taps) { console.log(`${t.version}: ${t.listChanged} ms, Event Timing ${t.eventTiming} ms`);}- Lines 3 to 5 hold when the list first changed, and the tap's Event Timing duration. Event Timing is the API behind INP, from lesson 26.1.
- Line 8 prints
no transition: 757.2 ms, Event Timing 768 msandranking inside the callback: 952.2 ms, Event Timing 136 ms. - Then
ranking before it: 925 ms, Event Timing 560 ms.
Look at the frames first. With the ranking code inside the callback, Chrome showed no new frame for 800 ms. With it before the transition, the leaf kept swaying through the ranking code. The longest stretch with no new frame shrank to 350 ms: the snapshots and the new state.
The list changed at about the same time either way. The transition itself added about 170 to 200 ms over the page with none. That's the snapshot's frame, the new state, and the animation's first frame.
Now the Event Timing numbers. The Event Timing spec ends a tap's duration at the next frame after it, as lesson 28.2 showed. With the ranking code inside the callback, that next frame was the snapshot's frame. It looked exactly like the frame before the tap.
So Event Timing measured 136 ms, while Asha waited 952.2 ms for anything to change. Why it matters: INP can't see time spent inside an update callback. When you add a view transition, measure when the new state appears, not just INP.
Chrome's docs give the same advice: "network fetches should be done before calling .startViewTransition(), while the page is still fully interactive". Why it matters: do slow work first. Then start the transition with a callback that only changes the DOM.
document.querySelector("#sort").addEventListener("click", () => {
document.startViewTransition(() => {
const order = rankReviews();
list.append(...order);
});
});952.2 ms931.4 ms, 952.2 ms, 922.8 ms, 985.5 ms800 ms672.9 ms136 ms17Chrome took the snapshot, then paused rendering while the ranking code ran. The screen didn't change at all, and Event Timing ended at the snapshot's frame.
When the update never ends
The spec lets a browser give up on a slow update callback after a time of its own choosing. In Chrome, that's 4 seconds: lines 1403 to 1407 of view_transition.cc. Ravi tried a version whose callback waits for the server. The course made the server take 6 seconds:
document.querySelector("#sort").addEventListener("click", () => { document.startViewTransition(async () => { await fetch("votes.json"); // the server takes 6 seconds list.append(...rankReviews()); });});- Line 2 starts the transition with an
asyncupdate callback. Its promise settles when the function ends. - Line 3 waits for the server. Line 4 moves the cards when the answer comes.
// One tap; the server answered after 6 seconds. ms after the tap.const paused = { from: 141.7, to: 4142.5 };const ready = { rejectedAt: 4143.5, error: "TimeoutError" };const listChanged = 6614.3; console.log(`Rendering paused for ${(paused.to - paused.from).toFixed(1)} ms.`);console.log(`ready failed at ${ready.rejectedAt} ms with ${ready.error}.`);console.log(`The list changed at ${listChanged} ms, with no animation.`);- Line 6 prints
Rendering paused for 4000.8 ms. - Line 7 prints
ready failed at 4143.5 ms with TimeoutError. - Line 8 prints
The list changed at 6614.3 ms, with no animation.
For four seconds, Asha's screen showed nothing new. Then Chrome gave up and skipped the transition. Its ready promise failed with a TimeoutError. When the answer came, the callback still ran and changed the list, with no animation. Why it matters: a slow update costs up to 4 seconds of a frozen screen, and then you lose the animation anyway.
Try it in your own browser. Pick where 500 ms of work runs, then tap the button in the small page:
sort.addEventListener("click", () => {
document.startViewTransition(() => {
work(500);
reverseList();
});
});The tap runs 500 ms of work, then reverses the list. The page says when the animation began, and what Event Timing measured for your tap.
Same-document vs cross-document
So far, everything happened inside one page. The page called startViewTransition() and changed itself. That's a same-document transition.
Ravi wants one more. When Asha opens the Mint tea page from the reviews, the old page should fade into the new one. That's a cross-document transition: from one page load to the next. There's no function to call. Both pages opt in with a CSS rule:
@view-transition {
navigation: auto;
}Lines 1 to 3 are an @view-transition rule. Line 2, navigation: auto, asks for a transition on navigations to and from this page. The Level 2 spec lists the other conditions:
- Both pages are on the same origin.
- The visitor starts the navigation, with a link, a form, or back and forward. Typing an address doesn't count.
- The page stays visible the whole time.
Two events let each page take part. pageswap fires on the old page "at the last moment before a document is about to be unloaded". pagereveal fires on the new page "right before presenting the first frame". In the recording, both events had a viewTransition object when both pages opted in.
In an old slide show, two projectors point at one screen. The first one shows the last slide. When the next slide is in the second projector, the operator dissolves from one to the other. Both projectors must be set up for it. And the dissolve can't start until the next slide is in place.
- In real life: The first projector, still showing its slide
- In JavaScript: The old page, with its snapshot
- In real life: The second projector
- In JavaScript: The new page
- In real life: Setting up both projectors for a dissolve
- In JavaScript: @view-transition on both pages
- In real life: Waiting for the next slide to drop into place
- In JavaScript: The new page loading
- In real life: The dissolve
- In JavaScript: The cross-fade
The course clicked the link to the Mint tea page, with and without the opt-in:
// From the reviews page to the Mint tea page. Middle of four visits each.const visits = [ { version: "no transition", firstPaint: 316.1, fullyShown: 330.7 }, { version: "a cross-document transition", firstPaint: 442.4, fullyShown: 657.5 },];for (const v of visits) { console.log(`${v.version}: first paint ${v.firstPaint} ms, fully shown ${v.fullyShown} ms`);}- Lines 3 and 4 hold the Mint tea page's first contentful paint, from Chrome's trace. Its largest contentful paint, the photo, came at the same moment.
- The other number is the first frame that matched the finished page.
- Line 7 prints
no transition: first paint 316.1 ms, fully shown 330.7 msanda cross-document transition: first paint 442.4 ms, fully shown 657.5 ms.
The server answered both visits after about 270 ms. Without a transition, the Mint tea page painted soon after. With one, the old page first had to take its snapshot. On Ravi's heavy page, slowed 4 times, that took one more frame. So the first paint came 126 ms later. Then the fade took more than 200 ms.
Chrome reported the largest contentful paint at 442.4 ms too, when the fade had only just begun. Why it matters: a cross-document transition delays the next page's first paint, and LCP doesn't count the fade. Keep the old page light, and the fade short.
For its first frame, the new page waits for its render-blocking files, like the style sheets in its head. The Level 2 spec warns that "overusing the render-blocking mechanism could make it so that the old state remains frozen for a long time". Chrome's docs say a navigation that takes more than four seconds skips the transition.
Read the table across: each row is one question, for each kind of transition.
| Question | Same-document | Cross-document |
|---|---|---|
| How does it start? | The page calls startViewTransition(). | The visitor follows a link, or goes back or forward. |
| Who opts in? | The page, by calling the function. | Both pages, with @view-transition. |
| What does the visitor wait for? | The update callback. | The next page, up to its first frame. |
| How long does Chrome wait? | 4 seconds. | 4 seconds. |
| Where can you change it? | The ViewTransition object it returns. | The pageswap and pagereveal events. |
Keeping transitions short
The animation runs after the pause. How long should it be? The default is a quarter of a second. Ravi tried a full second:
::view-transition-group(*) {
animation-duration: 1s;
}Line 1 selects every group. Line 2 sets its animation to one second. The old and new pictures take their duration from the group.
// From ready to finished, middle of four taps each, in ms.const animations = [ { css: "the default", ready: 804.2, finished: 1197.8 }, { css: "animation-duration: 1s", ready: 775.7, finished: 1915.3 },];for (const a of animations) { console.log(`${a.css}: ${(a.finished - a.ready).toFixed(1)} ms`);}- Lines 3 and 4 hold when
readyandfinishedsettled.readysettles as the animation is about to start.finishedsettles when the new page is "fully visible and interactive". - Line 7 prints
the default: 393.6 msandanimation-duration: 1s: 1139.6 ms.
Both took longer than their animation. The animation's first frame needs the main thread, and Ravi's page is heavy. Does a longer animation matter, if it's smooth? The course clicked the middle of Asha's screen 100 ms after ready, and again after finished:
// A click in the middle of Asha's screen. What did it reach?const clicks = [ { when: "100 ms into the animation", target: "html" }, { when: "after finished", target: "h3.review-name in #review-59" },];for (const c of clicks) console.log(`${c.when}: ${c.target}`);- Lines 3 and 4 hold where each click landed. The target is the element the click event went to.
- Line 6 prints
100 ms into the animation: htmlandafter finished: h3.review-name in #review-59.
During the transition, the click reached the html element, not the review under it. The pseudo-elements cover the screen. The spec says the captured elements "do not respond to hit-testing" while it animates. After it, the same click reached a review's name. Why it matters: every millisecond of animation is a millisecond when taps miss your page. Keep it short.
Where a railway crosses a road, gates come down while a train passes. Nobody can cross, even when the road beyond is empty. A short train means a short wait. A long, slow train holds everyone back.
- In real life: The gates coming down
- In JavaScript: ::view-transition covering the screen
- In real life: The train passing
- In JavaScript: The animation playing
- In real life: Someone waiting to cross
- In JavaScript: Asha's tap reaching html
- In real life: A short train
- In JavaScript: The default quarter second
A busy main thread at the start
Lesson 32.1 showed that a compositor animation keeps going while the main thread is busy. Chrome reported no reason the fades and the cards' moves couldn't run on the compositor. Yet in the recording, the animation waited for the main thread before it started. Ravi's busy version adds one line to the eight-card handler:
transition.ready.then(saveChoice);When ready settles, the line runs saveChoice. That stands in for other work: 400 ms on the main thread.
// The first frame with the new order, middle of four taps each, in ms.const taps = [ { version: "8 named cards", listChanged: 957 }, { version: "8 named cards, 400 ms of work at ready", listChanged: 1336.8 },];for (const t of taps) console.log(`${t.version}: ${t.listChanged} ms`);- Lines 3 and 4 hold when the new order first reached the screen.
- Line 6 prints
8 named cards: 957 msand8 named cards, 400 ms of work at ready: 1336.8 ms.
The animation didn't freeze half-way. It hadn't started. No new frame came for 766.6 ms. Then it played the same frames as the version without the work, about 380 ms later. Why it matters: keep the main thread free right after ready, or the animation waits.
Reduced motion
Some people feel dizzy or sick when things move on a screen. Others find it hard to focus. They can ask their system for less motion. The Media Queries spec shows that wish to CSS as prefers-reduced-motion. Its value is reduce for motion that can "trigger discomfort for those with vestibular motion sensitivity". The vestibular system is the inner ear's sense of balance.
The first way to respect it is CSS that turns the transition's animations off:
@media (prefers-reduced-motion: reduce) {
::view-transition-group(*),
::view-transition-old(*),
::view-transition-new(*) {
animation: none;
}
}- Line 1 applies the rule only when the visitor asks for less motion.
- Lines 2 to 4 select every group, every old picture and every new one.
- Line 5 removes their animations.
The second way skips the transition in the script. Ravi adds one line to his handler, right after the ranking code:
if (matchMedia("(prefers-reduced-motion: reduce)").matches) return list.append(...order);matchMedia() checks a media query from JavaScript. Its matches is true when Asha asks for less motion. Then the line moves the cards and returns, before any name or transition.
The course told Chrome that Asha asks for less motion, with DevTools' Emulation.setEmulatedMedia. Then it tapped both versions:
// Asha asked for less motion. First frame with the new order, in ms.const taps = [ { version: "CSS turns the animations off", listChanged: 909.1, paused: 250.3 }, { version: "the script skips the transition", listChanged: 710.7, paused: 0 },];for (const t of taps) console.log(`${t.version}: ${t.listChanged} ms, paused ${t.paused} ms`);- Lines 3 and 4 hold when the new order reached the screen, and how long Chrome paused rendering.
- Line 6 prints
CSS turns the animations off: 909.1 ms, paused 250.3 msandthe script skips the transition: 710.7 ms, paused 0 ms.
With the CSS, there was no animation. But Chrome still took both snapshots, and still paused rendering. The script way skipped all of that. The new order arrived about 200 ms sooner. Why it matters: when a visitor asks for less motion, don't start the transition at all.
For a cross-document transition, put the opt-in inside a media query:
@media (prefers-reduced-motion: no-preference) {
@view-transition {
navigation: auto;
}
}Line 1 applies the rule only for visitors with no preference. Lines 2 to 4 are the opt-in. In the recording, a visitor asking for less motion got no transition. pagereveal had no viewTransition, and the Mint tea page showed at 330 ms.
Less motion doesn't have to mean none. Chrome's docs point out that reduced motion "doesn't mean the user wants no motion". A quick, gentle fade can still show what changed.
Some people put a note on their door: "Baby sleeping. Please knock softly." A kind courier doesn't ring the loud bell. He knocks gently, or leaves the parcel by the door. He doesn't ring the bell and then whisper sorry.
- In real life: The note on the door
- In JavaScript: prefers-reduced-motion: reduce
- In real life: Ringing the loud bell
- In JavaScript: A big sliding transition
- In real life: Knocking softly
- In JavaScript: A short, gentle fade
- In real life: Leaving the parcel by the door
- In JavaScript: Skipping the transition in the script
- In real life: Ringing anyway, then whispering
- In JavaScript: animation: none, after the snapshots
Does Chrome pause rendering for it?
For each one, ask: does it run while Chrome draws no frames, or while frames keep coming?
- 400 ms of ranking code inside the update callback
- Waiting for
fetch()inside the update callback - 400 ms of ranking code before
startViewTransition() - A
view-transition-nameon all 1,000 cards animation: noneon the pseudo-elements, for less motion- Checking
prefers-reduced-motionand updating the list directly animation-duration: 1son::view-transition-group(*)
Sort each one by what happens on Asha's screen while it runs.
Then pick what to do. Read the table across: what you want, what to do, and why.
| If you want | Do this | Why |
|---|---|---|
| To animate a change on the page. | Do slow work first. Let the update callback only change the DOM. | Chrome pauses rendering while the callback runs. |
| Some cards to move on their own. | Name only those cards, just before the transition. | Each name is a snapshot, a group and five animations. |
| A quick animation. | Keep the quarter second, and keep the main thread free at ready. | Taps reach html while it plays, and it can't start while the main thread is busy. |
| Less motion for those who ask. | Check prefers-reduced-motion and skip startViewTransition(). | animation: none still pays for the snapshots. |
| A transition between pages. | Opt in on both pages, and keep the old page light. | The old page's snapshot comes before the new page's first paint. |
Common mix-ups
- "A view transition runs on the compositor, so it can't freeze the page." No. While Ravi's update callback ran, Chrome showed no new frame for 800 ms, not even for the leaf.
- "A good INP means the transition felt fast." No. Event Timing measured 136 ms. The list changed at 952.2 ms.
- "A view-transition-name is free, so name everything." No. With 1,000 names, the list didn't change within 10 seconds.
- "The browser waits for the update callback as long as it takes." No. Chrome gives up after 4 seconds and skips the animation. The change still happens, later.
- "animation: none skips the work." No. Chrome still took both snapshots and paused rendering for 250.3 ms.
- "A cross-document transition costs nothing until the next page is ready." No. The old page takes its snapshot first. The first paint moved from 316.1 to 442.4 ms.
- "The page stays clickable during the animation." No. A click in the middle of the screen reached html.
Practice exercises
Answer first, then use the hints.
This page gives three paragraphs a name each, then starts a view transition. What does line 4 print, in Chrome?
document.body.innerHTML = '<p id="a">A</p><p id="b">B</p><p id="c">C</p>';
for (const p of document.querySelectorAll("p")) p.style.viewTransitionName = p.id;
const transition = document.startViewTransition(() => document.body.append("Done"));
transition.ready.then(() => console.log(document.getAnimations().length));20. Three paragraphs and the root make four names. Each name gets the group's animation, and a fade and a blend for each of its two pictures.
What does the second line of this page print?
const transition = document.startViewTransition(() => console.log("update"));
transition.finished.then(() => console.log("finished"));
transition.ready.then(() => console.log("ready"));
transition.updateCallbackDone.then(() => console.log("updateCallbackDone"));
console.log("called");update. The page prints called first. Chrome then captures the old state and calls the update callback. updateCallbackDone, ready and finished settle after that, in that order.
In the how many names playground, how long did Chrome pause rendering with 8 cards named?
254.4 ms, a little more than with no card named. The pause also covers the new state, and eight more groups add to that work.
Ravi gets the new order from his server, which takes 2 seconds. For about how long does Asha's screen show no new frame? Then fix it.
document.querySelector("#sort").addEventListener("click", () => {
document.startViewTransition(async () => {
const response = await fetch("order.json"); // the server takes 2 seconds
const ids = await response.json();
list.append(...ids.map((id) => document.getElementById(id)));
});
});document.querySelector("#sort").addEventListener("click", async () => {
const response = await fetch("order.json"); // the page stays alive meanwhile
const ids = await response.json();
document.startViewTransition(() => list.append(...ids.map((id) => document.getElementById(id))));
});About 2 seconds. Rendering stays paused until the callback's promise settles, and the fetch is inside it. Fetch first, while the page still draws frames, then start the transition.
Open the course's copy of Ravi's first page, and run these lines in DevTools' Console. What message do they print?
document.startViewTransition(() => new Promise(() => {}))
.ready.catch((error) => console.log(error.message));Transition was aborted because of timeout in DOM update. DOM update timed out. Chrome joins two strings from its source: the message in dom_view_transition.cc and the reason in view_transition_skip_reason.cc.
A cross-document transition can hold the new page's first frame until one of its elements exists. Which rel value does the <link> for that use?
<link rel="expect" blocking="render" href="#reviews">expect. The new page's first frame waits until the parser has seen the element with that id. Chrome's docs warn to measure first: it delays the first paint.
Check your understanding
For each question, ask: when does Chrome take its pictures, and when does it stop drawing frames?
Question 1 of 8What does the last line of Asha's tap example print?
Read the code, then predict// Asha taps "Most helpful first". From Chrome's trace, in ms after her tap. const callback = { from: 115.9, to: 596.6 }; // Ravi's update callback const noNewFrame = { from: 135.5, to: 935.5 }; // the screen didn't change at all const listChanged = 952.2; // the first frame with the new order in it console.log(`The update callback ran for ${(callback.to - callback.from).toFixed(1)} ms.`); console.log(`No new frame for ${noNewFrame.to - noNewFrame.from} ms.`); console.log(`The list first changed at ${listChanged} ms.`);Choose an answer to see the explanation.
Question 2 of 8In lesson 32.1 the leaf kept swaying through Ravi's ranking code. Why did it stop this time?
Choose an answer to see the explanation.
Question 3 of 8What does the second line of the three-versions example print?
Read the code, then predict// Middle of four taps each. Times in ms after the tap. const taps = [ { version: "no transition", listChanged: 757.2, eventTiming: 768 }, { version: "ranking inside the callback", listChanged: 952.2, eventTiming: 136 }, { version: "ranking before it", listChanged: 925, eventTiming: 560 }, ]; for (const t of taps) { console.log(`${t.version}: ${t.listChanged} ms, Event Timing ${t.eventTiming} ms`); }Choose an answer to see the explanation.
Question 4 of 8Event Timing measured Asha's tap at 136 ms, but the list changed at 952.2 ms. Why so different?
Choose an answer to see the explanation.
Question 5 of 8What does each view-transition-name add to a transition?
Choose an answer to see the explanation.
Question 6 of 8What does a cross-document view transition need?
Choose an answer to see the explanation.
Question 7 of 8Asha taps the middle of the screen while the cards are still gliding. What does her tap reach?
Choose an answer to see the explanation.
Question 8 of 8For a visitor who asks for less motion, which way gets the new order on screen sooner?
Choose an answer to see the explanation.
Key takeaways
- A view transition takes snapshots, pauses rendering while the update callback runs, then animates its pseudo-elements.
- While rendering is paused, nothing moves, not even compositor animations. Do slow work before startViewTransition().
- Event Timing ends at the snapshot's frame, so INP can miss the wait. Measure when the new state appears.
- Chrome gives an update callback 4 seconds. Then it skips the animation, and ready fails with a TimeoutError.
- Every view-transition-name adds a snapshot, a group and five animations. Name only what must move, only while it moves.
- A cross-document transition needs @view-transition on both pages, and delays the next page's first paint.
- Taps reach html while the animation plays. Keep it short, and skip it for prefers-reduced-motion: reduce.
Remember the one-liner.
A view transition freezes the page, photographs it and animates: keep each step short, and skip it for anyone who asks for less motion.
Sources
The facts on this page come from these specs, docs and source files:
- CSS View Transitions Level 1: setting up a view transition, W3C.
- CSS View Transitions Level 1: rendering suppression for view transitions, W3C.
- view_transition.cc: rendering paused after the snapshot's frame, and the 4-second limit, Chromium source.
- layer_tree_host.h: pausing rendering stops main and compositor frames, Chromium source.
- CSS View Transitions Level 1: the view transition pseudo-element tree, W3C.
- CSS View Transitions Level 1: the user agent style sheet, W3C.
- View transitions in Chromium: snapshots as render passes, Chromium source.
- view_transition_style_tracker.cc: one snapshot per captured element, and the memory limit, Chromium source.
- compositor_animations.h: "Animation has no visible change", Chromium source.
- Same-document view transitions for single-page applications, Chrome for Developers.
- Event Timing: setting an entry's duration, W3C.
- CSS View Transitions Level 1: calling the update callback, and the timeout, W3C.
- dom_view_transition.cc and view_transition_skip_reason.cc: the timeout's error, Chromium source.
- CSS View Transitions Level 2: cross-document view transitions, W3C.
- Cross-document view transitions for multi-page applications, Chrome for Developers.
- Misconceptions about view transitions, Chrome for Developers.
- Media Queries Level 5: prefers-reduced-motion, W3C.
- View Transition API, MDN.
Coming next: Measuring smoothness. You'll count dropped and partly presented frames, like the ones in this lesson's traces.