Workspace
  • Study Book
  • WEB Network HTTP etc
    • Performance Optimization
    • Performance Optimization
    • HTTP/2 & SPDY
    • WebSocket
    • HTTP Header
    • Cross-Origin Resource Sharing
    • JSON, XML, other format
  • Javascript
    • Promise
    • make API call
    • Web API
    • Common JS
    • AJAX
    • Event DOM and delegation
    • ES6 new features
    • special function
    • function API
  • React
    • class component
    • Example
    • Lifting functions/ state up
    • Hot Loader
    • Testing
    • New Features
    • Hook
    • Simple code
    • Life Cycle
  • CSS
    • Horizontal & Vertical Align
    • GPU Animation
    • transform-function
    • LVHA Pseudo-classes
    • Selector
    • How To CSS
  • HTML
  • Redux
  • NodeJS
    • File System
  • express
    • express questions
    • express mongodb
  • Restful API
  • MongoDB
  • Compare
  • Test
    • Jest
  • Deploy development
  • coding question
  • DevOps
  • Webpack
  • GraphQL
Powered by GitBook
On this page

Was this helpful?

  1. CSS

GPU Animation

PreviousHorizontal & Vertical AlignNexttransform-function

Last updated 4 years ago

Was this helpful?

Use transform: translateZ(0) or will-change: transform

  • 3D transforms: translate3d, translateZ and so on;

  • <video>, <canvas> and <iframe> elements;

  • animation of transform and opacity via Element.animate();

  • animation of transform and opacity via СSS transitions and animations;

  • position: fixed;

  • will-change;

  • filter;

PROS

  • The animation is fast and smooth, at 60 frames per second.

  • A properly crafted animation works in a separate thread and is not blocked by heavy JavaScript calculations.

  • 3D transforms are “cheap.”

CONS

  • Additional repainting is required to promote an element to a composite layer. Sometimes this is very slow (i.e. when we get a full-layer repaint, instead of an incremental one).

  • Painted layers have to be transferred to the GPU. Depending on the number and size of these layers, the transfer can be very slow, too. This could lead to an element flickering on low-end and mid-market devices.

  • Every composite layer consumes additional memory. Memory is a precious resource on mobile devices. Excessive memory use could crash the browser.

  • If you don’t consider implicit compositing, then the chances of slow repainting, extra memory usage and browser crashes are very high.

  • We get visual artifacts, such as with text rendering in Safari, and page content will disappear or get distorted in some cases.

USE CSS TRANSITIONS AND ANIMATIONS WHENEVER POSSIBLE

animation of transform and opacity via CSS transitions or animations automatically creates a compositing layer and works on the GPU. We could also animate via JavaScript, but we’d have to add transform: translateZ(0) or will-change: transform, opacity first in order to ensure that the element gets its own compositing layer.

main principles

  • Always negotiate with the client and designer about all animations and effects on the website. It could affect the page’s markup greatly and make for better compositing.

  • Watch out for the number and size of composite layers from the very beginning — especially ones created by implicit compositing. The “Layers” panel in your browser’s development tools is your best friend.

  • Modern browsers make heavy use of compositing not just for animation but to the painting of page elements. For example, position: fixed and the iframe and video elements use compositing.

  • The size of compositing layers is likely be more important than the number of layers. In some cases, the browser will try to reduce the number of composite layers (see the “Layer Squashing” section of “GPU Accelerated Compositing in Chrome”); this prevents so-called “layer explosion” and reduces memory consumption, especially when layers have large intersections. But sometimes, such optimization has a negative impact, such as when a very large texture consumes much more memory than a few small layers. To bypass this optimization, I add a small, unique translateZ() value to each element, such as translateZ(0.0001px), translateZ(0.0002px), etc. The browser will determine that the elements lie on different planes in the 3D space and, thus, skip optimization.

  • You can’t just add transform: translateZ(0) or will-change: transform to any random element to virtually improve animation performance or to get rid of visual artifacts. GPU compositing has many drawbacks and tradeoffs to be considered. When not used sparingly, compositing will decrease overall performance at best, and crash browsers at worst.

CSS animation using the transform property looks much smoother than one using the left and top properties.