Anna Migas
@szynszyliszys
Make your animations
perform well
Front-end Dev & Designer
at Lunar Logic
Anna Migas
zdjęcie lampy
Loading…
https://www.arpio.pl/
https://dribbble.com/arnaudlrx
zdjęcie krzeseł
http://ancorathemes.com/
Don’t use animations
if they are not helping anyone
CSS vs JavaScript
CSS animations
1. Great for simple animations
2. Browsers optimize for them (GPU acceleration)
3. Native, no need to download anything
4. Need vendor prefixes (@-webkit-keyframes)
5. Scheduling animations together in time is hard
JavaScript animations
1. More control over scheduling & timing
2. Many options with different pros/cons
A. Pure JavaScript
B. Web Animation API (polyfill & vendor prefixes)
C. Frameworks (recommended: Greensock AP)
Ok, so here we go
How a browser paints a website?
Browser makes a get request,
receives HTML in return
1
The HTML is parsed, DOM created
2
Document Object Model
Styles are applied
3
Render Tree
Render Tree
Layout is calculated
4
Layout
Everything is rasterized
and painted to the layers
5
Paint
Compositing
6
Compositing
First render
Any change on a page
1. We change layout
(width, margin-top, left)
2. We change paint
(background-color, shadows, background-image)
3. We change compositing
(transform, opacity)
https://csstriggers.com/
transform, opacity
The two properties to animate nicely
Why?
1. GPU acceleration
2. Layer promotion
Layers creation
What creates new layers?
1. 3D or perspective transforms
2. Animated 2d transforms or opacity
3. Being on top/a child of a compositing layer
4. Accelerated CSS filters
5. In special cases <video>, <canvas>, plugins
@keyframes rotate {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
.btn {
background-color: white;
border: 1px solid blue;
-webkit-animation: rotate 1s infinite;
animation: rotate 1s infinite;
}
@keyframes rotate {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
.btn {
background-color: white;
border: 1px solid blue;
}
.btn::hover {
-webkit-animation: rotate 1s infinite;
animation: rotate 1s infinite;
}
What creates new layers?
1. 3D or perspective transforms
2. Animated 2d transforms or opacity
3. Being on top/a child of a compositing layer
4. Accelerated CSS filters
5. In special cases <video>, <canvas>, plugins
6. will-change property
@keyframes rotate {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
.btn {
background-color: white;
border: 1px solid blue;
will-change: transform;
}
.btn:hover {
-webkit-animation: rotate 1s infinite;
animation: rotate 1s infinite;
}
will-change rules
1. Give a browser a moment to think
.btn {
transition: transform 1s ease-out;
background-color: white;
border: 1px solid blue;
-webkit-transform: rotate(15deg);
transform: rotate(15deg);
}
.btn:hover {
will-change: transform;
}
.btn:active {
-webkit-transform: none;
transform: none;
}
will-change rules
1. Give a browser a moment to think
2. Don’t try to optimize too many elements
“With great power comes great
responsibility.”
— Spiderman’s Uncle
Every layer consumes memory.
Use them wisely.
*,
*::before,
*::after {
will-change: all;
}
DON’T EVER DO THIS.
will-change rules
1. Give a browser a moment to think
2. Don’t try to optimize too many elements
3. Use it in stylesheets only if the change will
happen constantly
4. It is a good idea to remove it once the
animation is finished
var element = document.getElementById(‘element’);
element.addEventListener(‘mouseenter’, hintBrowser);
element.addEventListener('animationEnd', removeHint);
function hintBrowser() {
this.style.willChange = 'transform';
}
function removeHint() {
this.style.willChange = 'auto';
}
will-change rules
1. Give a browser a moment to think
2. Don’t try to optimize too many elements
3. Use in the stylesheets if the change happens
constantly
4. Remove it once the animation is finished
5. Not supported in IE, Edge, Safari (you can use
-webkit-transform: translate3d(0,0,0);)
FLIP technique
First
Last
Invert
Play
FLIP technique
A B
0 200px
-200px 0
FLIP technique
A B
@keyframes slide-from-left-flip {
0% {
-webkit-transform: translateX(-200px);
transform: translateX(-200px);
}
100% {
-webkit-transform: none;
transform: none;
}
}
B A
.element {
position: absolute;
}
.element
B A
.element {
position: absolute;
transform: translateX(-200px);
}
.element
B A
.element {
position: absolute;
transform: translateX(-200px);
transition: transform 1s;
}
.element.active {
transform: none;
}
.element
.active
100ms gap
FLIP technique
1. Can make a difference on less powerful devices
2. Use it for animations that will happen on user
input
3. https://github.com/googlechrome/flipjs
4. https://github.com/szynszyliszys/repaintless
5. https://aerotwist.com/blog/flip-your-animations/
Remember this?
16ms
1 frame
1000ms/60 = 16ms
60fps
1s = 1000ms
Now imagine…
Try to do that in 16ms
requestAnimationFrame()
requestAnimationFrame
requestAnimationFrame
function repeated() {
// show something many times
window.requestAnimationFrame(repeated);
}
window.requestAnimationFrame(repeated);
requestAnimationFrame
1. Needs vendor prefixes
2. You need the polyfill to support older browsers
3. Don’t need to use that if you are using: Web
Animation API, Greensock AP, jQuery 3.0.0+
4. cancelAnimationFrame to end scheduling
5. Way better than setInterval
A B
B A
A
Test your animations
A B
B A
Summary
1. Don’t overuse animations
2. Animate only transform and opacity if possible
3. Use will-change, requestAnimationFrame,
FLIP when applicable
4. Don’t overuse layers
5. Animate elements that are at the top layers
6. Test animations before optimising
Resources
1. https://www.html5rocks.com/en/tutorials/speed/high-performance-
animations
2. http://jankfree.org
3. https://dev.opera.com/articles/css-will-change-property
4. http://creativejs.com/resources/requestanimationframe
5. https://www.udacity.com/course/browser-rendering-optimization--
ud860
6. https://www.html5rocks.com/en/tutorials/speed/layers
7. https://developers.google.com/web/fundamentals/design-and-ui/
animations
Grazie!
Anna Migas
@szynszyliszys
Questions?

Make your animations perform well - Anna Migas - Codemotion Rome 2017