Any change or movement of pixels over time.
60 frames per second
One slow frame can interrupt an otherwise smooth experience.
Central Processing Unit
General purpose processor
Typically 1-8 big cores
Can process anything— network packets, mouse input, etc.
Graphics processing unit
Highly specialized processor
Hundreds of little cores
All shapes can be made of triangles.
GPUs also specialize in drawing lots of triangles (and their pixels) to the screen quickly.
GPUs can move, scale, or rotate triangles (and their pixels) quickly.
GPUs can modify a lot of pixels in the same way quickly.
Browser instructs the CPU and GPU to work together and draw a web page.
Use the GPU to draw to the screen.
Draw into hardware accelerated elements, which draw to screen.
#some-element {
will-change: transform;
}
Causes hardware acceleration.
#some-element {
transform: translate3d(0, 0, 0);
}
Cause hardware acceleration.
#some-element {
transform: translate(0, 0);
}
Don't cause hardware acceleration, unless animated.
#some-element {
transition: transform linear 1s;
}
.initial {
transform: translate(0, 0);
}
.final {
transform: translate(100px, 100px);
}
Causes hardware acceleration during the animation.
#some-element {
transition: opacity linear 1s;
}
.initial {
opacity: 0;
}
.final {
opacity: 1;
}
Causes hardware acceleration during the animation.
#some-element {
transition: filter linear 1s;
}
.initial {
filter: grayscale(0);
}
.final {
filter: grayscale(1);
}
Causes hardware acceleration during the animation.
When an element appears on top of a hardware accelerated element, it also becomes hardware accelerated.
CPU reads and interprets HTML & CSS.
CPU draws the element and its children into an offscreen image (aka "texture").
CPU uploads the element's texture to the GPU.
GPU creates two or more triangles representing the element.
GPU decides how to position, rotate, or scale the element's triangles on the screen.
GPU applies the element's texture to the triangles.
If needed, GPU modifies the pixels on the triangle for special effects like CSS Filters (drop-shadow, grayscale, etc.).
GPU draws the element's triangles on top of the other elements in your page.
Because we can.
header {
transition: height ease-in 1s;
}
.initial {
height: 0;
}
.final {
height: 200px;
}
Every frame, the element's size and contents change.
Every frame, the browser has to do almost every costly step.
body {
transition: transform ease-in 1s;
}
.initial {
transform: translate3d(0, -200px, 0);
}
.final {
transform: translate3d(0, 0, 0);
}
Every frame, the browser can skip many costly steps.
Open Terminal and enter the following:
defaults write com.apple.Safari IncludeInternalDebugMenu 1
Restart Safari and navigate to:
> Debug > Drawing/Compositing Flags > Show Compositing Borders
Design animations and transitions intended for the GPU:
Animate CSS properties intended for the GPU:
Hardware accelerate elements before an animation to remove initial CPU work.
#commonly-animated-element {
/* Force acceleration on current browsers. */
transform: translateZ(0);
/* Force acceleration on future browsers. */
will-change: transform;
transition: transform linear 1s;
}
Don't hardware accelerate everything. It wastes GPU memory.
Max Vujovic | @mvujovic | mvujovic.github.io/MAX2014
Adobe Web Platform Team | @adobeweb | webplatform.adobe.com