PG Day Us: Animations for Web & Hybrid

79
Animations for Web & Hybrid @alexblom Isle of Code

Transcript of PG Day Us: Animations for Web & Hybrid

Page 1: PG Day Us: Animations for Web & Hybrid

Animations for Web &

Hybrid@alexblom

Isle of Code

Page 2: PG Day Us: Animations for Web & Hybrid

whoami

Page 3: PG Day Us: Animations for Web & Hybrid

Isle of Code

• Toronto + Houston based development;

• Focused on Ember/Vue/React/Cordova;

• Creators of ember-cordova & corber;

Page 4: PG Day Us: Animations for Web & Hybrid

corber.io

• Extension of ember-cordova;

• Supports Ember, Vue, React, Glimmer;

• CLI for framework -> hybrid builds;

• Adds livereload, unified builds, splash/icons, etc;

Page 5: PG Day Us: Animations for Web & Hybrid

corber demo

Page 6: PG Day Us: Animations for Web & Hybrid

Contents

• What makes a good animation?;

• Profiling / performance;

• 3 examples;

• Additional options (promoting layers, JS

animation API);

Page 7: PG Day Us: Animations for Web & Hybrid

What makes a good

animation?

Page 8: PG Day Us: Animations for Web & Hybrid

FPS Target

• 60fps is considered best;

• 1000ms / 60fps = ~16.6ms per frame;

• 60fps is not always achievable!;

• It is better to deliver a consistent frame rate

than a variable one. Consider targeting 30fps;

Page 9: PG Day Us: Animations for Web & Hybrid

Animation Options

• Declarative: CSS;

• Generally speaking most performant;

• Browser knows what is happening up front -

can save you from yourself;

• Can become complex to write;

Page 10: PG Day Us: Animations for Web & Hybrid

Animation Options

• Imperative: JS;

• Tell the browser how. Easier to spell out in

code (e.g. bounce);

• Run on the main thread (not great);

• requestAnimationFrame & webAnimationsAPI

can fix performance issues - to be discussed

later;

Page 11: PG Day Us: Animations for Web & Hybrid

Profiling / Performance

Page 12: PG Day Us: Animations for Web & Hybrid

From 0 to rendered

Page 13: PG Day Us: Animations for Web & Hybrid

How our content is rendered

• 1: DOM Tree constructed;

• 2: Layout (recursively from the top left);

• highly interdependent;

• expensive;

Page 14: PG Day Us: Animations for Web & Hybrid

Layout Styles

width overflow/overflow-y

min-width font-weight

height font-size

min-height text-align

padding line-height

display vertical-align

border white-space

border-width clear

position bottom/top/left/right

float

Page 15: PG Day Us: Animations for Web & Hybrid

Reflow

Page 16: PG Day Us: Animations for Web & Hybrid

What causes Reflow?

• Resizing the browser window;

• using JavaScript methods involving computed

styles;

• adding or removing elements from the DOM; and

• changing an element's classes.

• https://developers.google.com/speed/articles/reflo

w

Page 17: PG Day Us: Animations for Web & Hybrid

How our content is rendered

• 1: DOM Tree constructed;

• 2: Layout (recursively from the top left);

• 3: Paint;

Page 18: PG Day Us: Animations for Web & Hybrid

Paint Styles

border-style outline

border-radius outline-color

background outline-width

background-size outline-style

background-image color

background-repeat visibility

background-position text-decoration

box-shadow

Page 19: PG Day Us: Animations for Web & Hybrid

Paint Styles

• opacity is a special case;

• If the element has its own layer, this is handled

automagically by lowering the alpha-mask value;

Page 20: PG Day Us: Animations for Web & Hybrid

How our content is rendered

• 1: DOM Tree constructed;

• 2: Layout (recursively from the top left);

• 3: Paint;

• 4: Composite/Layering (default is a single layer);

Page 21: PG Day Us: Animations for Web & Hybrid

Layers

• Default is a single layer;

• Composed layer more efficient to animate - GPU;

• Pushing every node to it’s own layer is the worst case;

• The most performant is hardware accelerated

properties on composite layers (e.g.

transform/translateX);

• Layers are expensive (width * height * 3);

Page 22: PG Day Us: Animations for Web & Hybrid

Promoting a Layer

• will-change property, e.g.;

• will-change: auto;

• will-change: scroll;

• will-change: transform;

Page 23: PG Day Us: Animations for Web & Hybrid

Prior Layers

Page 24: PG Day Us: Animations for Web & Hybrid

Add will-change;

Page 25: PG Day Us: Animations for Web & Hybrid

Layers

Page 26: PG Day Us: Animations for Web & Hybrid

Layout Thrashing

Page 27: PG Day Us: Animations for Web & Hybrid

Layout Thrashing

Page 28: PG Day Us: Animations for Web & Hybrid

Profiler Tool

Page 29: PG Day Us: Animations for Web & Hybrid

Profiler Tool

Page 30: PG Day Us: Animations for Web & Hybrid

Profiler Tool

Page 31: PG Day Us: Animations for Web & Hybrid

Profiler Tool

Page 32: PG Day Us: Animations for Web & Hybrid

Profiler Tool

Page 33: PG Day Us: Animations for Web & Hybrid

Our Scene

https://www.sitepoint.com/playing-with-fire-organic-css3-animation/

Page 34: PG Day Us: Animations for Web & Hybrid

Animation 1 -

The worst case

Page 35: PG Day Us: Animations for Web & Hybrid

Animation 1

Page 36: PG Day Us: Animations for Web & Hybrid

Animation 1

Page 37: PG Day Us: Animations for Web & Hybrid
Page 38: PG Day Us: Animations for Web & Hybrid
Page 39: PG Day Us: Animations for Web & Hybrid
Page 40: PG Day Us: Animations for Web & Hybrid

Some of our problems

• Big FPS dip;

• Each animation is triggering a reflow;

• We are not taking advantage of hardware-

accelerated properties;

• We are redrawing at a fixed (but not) rate;

• Will keep running in an unfocused tab;

Page 41: PG Day Us: Animations for Web & Hybrid

We are redrawing at a fixed

rate

• Sort of:

• interval timing is not guaranteed - if our thread

is busy it will take longer;

• When a tab is inactive the interval will still fire

every ~1s;

Page 42: PG Day Us: Animations for Web & Hybrid

CSS3 Animations

(@keyframe)

Page 43: PG Day Us: Animations for Web & Hybrid

https://www.sitepoint.com/playing-with-fire-organic-css3-animation/

Page 44: PG Day Us: Animations for Web & Hybrid

CSS3 Animation

• animation-duration;

• animation-delay;

• animation-timing;

• ease, linear, ease-in, ease-out, ease-in-out,

cubic-bezier;

Page 45: PG Day Us: Animations for Web & Hybrid

Why don’t we try them

in this case?

Page 46: PG Day Us: Animations for Web & Hybrid

But CSS animations

are hard!

Page 47: PG Day Us: Animations for Web & Hybrid

Animation API

Page 48: PG Day Us: Animations for Web & Hybrid
Page 49: PG Day Us: Animations for Web & Hybrid

Animation 2 -

Hardware Accelerated

Page 50: PG Day Us: Animations for Web & Hybrid

http://slides.com/ehayman/deck-1#/6

Page 51: PG Day Us: Animations for Web & Hybrid

http://slides.com/ehayman/deck-1#/6

Page 52: PG Day Us: Animations for Web & Hybrid

Animation 2

Page 53: PG Day Us: Animations for Web & Hybrid
Page 54: PG Day Us: Animations for Web & Hybrid

Timeline

Page 55: PG Day Us: Animations for Web & Hybrid
Page 56: PG Day Us: Animations for Web & Hybrid

Profile

• 6248ms to 5420ms on scripting;

• 10588ms to 4818ms rendering;

• 267ms to 1488ms painting;

Page 57: PG Day Us: Animations for Web & Hybrid

Some of our problems

• Big FPS dip (sort of);

• Each animation is triggering a reflow;

• We are not taking advantage of hardware-

accelerated properties;

• Will keep running in an unfocused tab;

• We are redrawing at a fixed rate;

Page 58: PG Day Us: Animations for Web & Hybrid

Animation Frames

Page 59: PG Day Us: Animations for Web & Hybrid

animationFrame

• Supported for major browsers;

• Only draws when:

• the animation will be visible;

• the browser is ready;

• there are no other frames waiting to be drawn;

• Eliminates unnecessary draws & overloaded

callbacks;

Page 60: PG Day Us: Animations for Web & Hybrid
Page 61: PG Day Us: Animations for Web & Hybrid

animationFrame

Page 62: PG Day Us: Animations for Web & Hybrid

animationFrame

Page 63: PG Day Us: Animations for Web & Hybrid
Page 64: PG Day Us: Animations for Web & Hybrid
Page 65: PG Day Us: Animations for Web & Hybrid

Profile

• 5420ms to 2605ms on scripting;

• 4818ms to 5295ms rendering;

• 1488 to1557ms painting;

Page 66: PG Day Us: Animations for Web & Hybrid

Some of our problems

• Big FPS dip;

• Each animation is triggering a reflow;

• We are not taking advantage of hardware-

accelerated properties;

• This will keep running in an unfocused tab;

• We are redrawing at a fixed rate;

Page 67: PG Day Us: Animations for Web & Hybrid

Still could be better

• Immediate reaction may be to tear down unused

elements;

• Don’t

Page 68: PG Day Us: Animations for Web & Hybrid
Page 69: PG Day Us: Animations for Web & Hybrid

Demo

Page 70: PG Day Us: Animations for Web & Hybrid
Page 71: PG Day Us: Animations for Web & Hybrid

Profile

• 2605ms to 1068ms on scripting;

• 5295ms to 2100ms rendering;

• 1557 to 702ms painting;

Page 72: PG Day Us: Animations for Web & Hybrid

Intersection Observer API

Page 73: PG Day Us: Animations for Web & Hybrid

Intersection Observer API

• “The Intersection Observer API provides a way to

asynchronously observe changes in the

intersection of a target element with an ancestor

element or with a top-level document’s.”

• https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API

Page 74: PG Day Us: Animations for Web & Hybrid

Intersection Observer API

Page 75: PG Day Us: Animations for Web & Hybrid

Intersection Observer API

• callback function receives a list of intersections

detected;

Page 76: PG Day Us: Animations for Web & Hybrid

Passive Event Listener

Page 77: PG Day Us: Animations for Web & Hybrid

Passive Event Listener

• Allows you to indicate preventDefault will not be

used - e.g. eliminates block on scroll

Page 78: PG Day Us: Animations for Web & Hybrid

My best practices

• Avoiding JS Animation API until better Safari support;

• Pages with smaller animations (e.g. button bounce):

• CSS/Hardware accelerated animations are fine;

• Will generally promote frequent/transitional

animations to their own layer;

• Pages with lots of animations:

• Just use animationFrame;

Page 79: PG Day Us: Animations for Web & Hybrid

Animations for Web &

Hybrid@alexblom

Isle of Code