Sub topic 12
Performance: what to animate and why
This chapter assumes you can write @keyframes, attach a timeline with animation-timeline, and read a scroll() or view() declaration. It covers which properties are cheap to animate on scroll and which are expensive.
Why scroll-driven motion is sensitive
A time-driven animation that stutters is annoying. A scroll-driven animation that stutters is worse, because the motion is tied to the user's finger or wheel. Any delay between input and visual response is easy to notice.
The browser renders a frame in stages: style, layout, paint, and composite. Animating a property forces the browser to redo that property's stage and every stage after it.
| Property type | Examples | Stages redone each frame |
|---|---|---|
| Layout | `width`, `height`, `top`, `left`, `margin` | Layout, paint, composite |
| Paint | `background-color`, `box-shadow`, `clip-path` | Paint, composite |
| Composite only | `transform`, `opacity` | Composite |
The compositor advantage
Browsers can run a scroll-driven animation of transform or opacity on the compositor. The compositor is a separate thread from the main thread, where JavaScript and layout run. The animation can then keep pace with scrolling while the main thread is busy.
Layout and paint properties must run on the main thread. If a long JavaScript task blocks that thread, the animation falls behind the scroll position.
Same effect, two costs
Both progress bars below look identical. The first changes width, which forces layout on every frame. The second scales the element with a transform.
@keyframes grow-width {
from { width: 0; }
to { width: 100%; }
}
@keyframes grow-scale {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}
.bar {
position: fixed;
inset: 0 0 auto 0;
height: 4px;
background: rebeccapurple;
}
.bar.slow {
animation: grow-width linear both;
animation-timeline: scroll(root block);
}
.bar.fast {
transform-origin: left;
animation: grow-scale linear both;
animation-timeline: scroll(root block);
}Prefer the .fast version. Set transform-origin so the scale grows from the correct edge. Otherwise the bar grows from its center.
Practical rules
- Animate
transformandopacityfirst. Usetranslate,scale, androtatefor movement instead oftoporleft. - Avoid animating
filter: blur()andbox-shadowacross many elements. Both are costly to repaint. - Keep the number of animated elements reasonable. Each element using
view()gets its own timeline, so a page with hundreds of cards does hundreds of times the work. - Use
will-change: transformonly on elements that are animating and only when you measure a gain. Each hint can allocate extra memory for a compositor layer. - Keep the animation simple. Scroll-driven animations have no duration in seconds, so the work is spread over scroll distance.
Common errors
- Animating layout properties for convenience.
widthandheightare easy to write, but they cause layout on every scroll frame. Replace them withscale. - Assuming off-main-thread everywhere. This behavior depends on the browser and on the properties involved. Mixing one layout property into a keyframe block can move the whole animation to the main thread.
- Skipping measurement. Record a trace in the browser's Performance panel while scrolling. Look for long layout or paint entries during the scroll. The Animations panel shows which animations are running.
Exercise
A reading progress bar animates width from 0 to 100% with animation-timeline: scroll(root block). Scrolling stutters when a script runs heavy work. How do you change the animation, and why does that help?
Show answer
Animate transform: scaleX(0) to scaleX(1) with transform-origin: left instead of width. Transform animations can run on the compositor thread, so the bar keeps tracking the scroll position while the main thread is busy. Animating width forces layout and paint on the main thread every frame.