By Paul Lewis. See also his article on HTML 5 Rocks:
https://www.html5rocks.com/en/tutorials/speed/high-performance-animations/
Even more links:
- https://eu.udacity.com/course/browser-rendering-optimization–ud860
- https://developers.google.com/web/fundamentals/performance/rendering/
- https://aerotwist.com/blog/flip-your-animations/
- https://developers.google.com/web/updates/2017/03/performant-expand-and-collapse
- https://aerotwist.com/blog/the-anatomy-of-a-frame/
Takeaway of this video:
The magic goal: 16ms
For 60HZ monitors aim for 16ms delay or less. (60fps = ~16ms per frame).
Delays above that will make the animation look janky
Note: for the 120HZ monitors that are getting more common these days the magic number would of course be 8 ms.
A bit of ‘theory’:
What goes into making a frame:
– JS
– Styles
– Layout
– Paint
– Composite
Layout and Paint are typically the most expensive operations here but also the ‘easiest’ to take out of the flow
Layout is triggered by a lot of css properties. See this overview for all of them, but to name a few:
- width/height
- padding/margin
- position (top/bottom/left/right)
- border/border-color/border-width
- display
- float/ clear
- overflow
- font-size/font-weight/font-family/line-height
- text-align/vertical-align
The most common css properties to trigger Paint are:
- color
- visibility
- text-decoration
- border-radius/ border-style
- box-shadow
- background/ background-image/ background-repeat/ background-position
- outline/outline-color/outline-style/outline-width
Layers
If you have two elements A and B on the page, and you want to move element B, you will have to scrub out B and paint it again on its new position.
However, if A and B are on different layers, you only have to move around the layer of element B
Now there are some things a browser can animate cheaply (because GPU’s are optimized for this type of stuff):
- Transformations: Translate, Scale, Rotate, Skew, Matrix transformations
- Opacity
Create a new Layer and transform it
Use this CSS to create a new layer:
#box {
will-change: transform
}
Warning: too many layers will cause your composite layer to grow, so you will have to find a balance there.