Super smooth animations
Milano Frontend Meetup - 24 Aprile 2018
Expectation
Reality
Render cycle
JavaScript
Style
Layout
Paint
Composite
Our logic
JavaScript
Style
Layout
Paint
Composite
JavaScript
Style
Layout
Paint
Composite
JavaScript
Style
Layout
Paint
Composite
JavaScript
Style
Layout
Paint
Composite
Animate compositor-only properties
Left
Translate
Create layers
will-change
❌
* {
will-change: transform;
}
Avoid forced synchronous layout
❌
elements.forEach(el => {
el.querySelector('img').style.width
= `${el.offsetWidth}px`;
})
🤓
elements.forEach((el, i) => {
ws[i] = el.offsetWidth;
})
elements.forEach((el, i) => {
el.querySelector('img').style.width
= `${ws[i]}px`;
})
rAF
requestAnimationFrame()
rIC
requestIdleCallback()
Response <100ms
Animation <16ms
Idle <50ms
Load <1000ms
First
const first =
el.getBoundingClientRect();
Last
el.classList.toggle('end');
const last =
el.getBoundingClientRect();
Invert
const invert =
first.height / last.height;
Play
el.animate([
{ transform: `scaleY(${invert})` },
{ transform: 'scaleY(1)' }
], 300);
The best animation is the invisible one
Thank you 🙏
Super smooth animations
Milano Frontend Meetup - 24 Aprile 2018