Practical Web Animation

A presentation at ScriptConf in October 2019 in Linz, Austria by Lisi Linhart

Slide 1

Slide 1

Practical Web Animation Lisi Linhart

Slide 2

Slide 2

Title Text

Slide 3

Slide 3

Title Text

Slide 4

Slide 4

the user Title Text

Slide 5

Slide 5

the user Title Text the browser

Slide 6

Slide 6

all kinds of experiences Title Text

Slide 7

Slide 7

Slide 8

Slide 8

Animation with the Browser

Slide 9

Slide 9

4 things a browser can animate cheaply

Slide 10

Slide 10

4 things a browser can animate cheaply position translateX(-10px);

Slide 11

Slide 11

4 things a browser can animate cheaply position scale translateX(-10px); scale(0.5);

Slide 12

Slide 12

4 things a browser can animate cheaply position scale rotation translateX(-10px); scale(0.5); rotate(-25deg);

Slide 13

Slide 13

4 things a browser can animate cheaply position scale rotation opacity translateX(-10px); scale(0.5); rotate(-25deg); opacity: 0.5;

Slide 14

Slide 14

order matters transform properties can be chained

Slide 15

Slide 15

codepen.io/danwilson/pen/eEYdgo

Slide 16

Slide 16

this is not performant

Slide 17

Slide 17

use the transform property đź’Ş

Slide 18

Slide 18

Rendering Performance Javascript / Event

Slide 19

Slide 19

Rendering Performance Javascript / Event Style Recalculations width, margin, left, .. Layout

Slide 20

Slide 20

Rendering Performance Javascript / Event Style Recalculations Layout Paint background, box-shadow, outline, ..

Slide 21

Slide 21

Rendering Performance Javascript / Event Style Recalculations Layout Paint Composite transform, opacity ..

Slide 22

Slide 22

Rendering Performance Javascript / Event Style Recalculations Layout 1 frame Paint Composite

Slide 23

Slide 23

translateX(-10px); opacity: 0.5; try to limit yourself to CSS transform & opacity for most animations

Slide 24

Slide 24

promote elements to new layers if animated often & constantly

Slide 25

Slide 25

Creating new layers

Slide 26

Slide 26

Creating new layers 1. will-change property 2. 3D Transform 3. animated 2D transforms 4. being on top of a compositing layers 5. animated CSS filters

Slide 27

Slide 27

Slide 28

Slide 28

🤯 don’t create too many layers remove them if the animation is finished

Slide 29

Slide 29

DevTools

Slide 30

Slide 30

inspect & test your animations in your devtools

Slide 31

Slide 31

Javascript / Event Style Recalculations Layout Paint Composite

Slide 32

Slide 32

Animation for the User

Slide 33

Slide 33

ORIENTATION & TRANSITIONS

Slide 34

Slide 34

Title Text

Slide 35

Slide 35

ORIENTATION & TRANSITIONS 1 help with reorientation in the user 2 explain relationships between different information spaces

Slide 36

Slide 36

ORIENTATION & TRANSITIONS Example: WAAPi

Slide 37

Slide 37

Title Text codepen.io/lisilinhart/pen/vYYLoQg

Slide 38

Slide 38

click button to start transition Title Text codepen.io/lisilinhart/full/pmYBva animate element Out and wait for animation to finish

Slide 39

Slide 39

Title Text codepen.io/lisilinhart/full/pmYBva animate new element in once the animation finished

Slide 40

Slide 40

WAAPI Transitions elements become visible once we click the correct link

Slide 41

Slide 41

WAAPI Transitions call animation function once a link is clicked

Slide 42

Slide 42

WAAPI Transitions get the element that’s currently visible animate it out wait for animation to finish animate new element in

Slide 43

Slide 43

WEB ANIMATIONS API keyframes object or array: stages of the animation timings object: how it’s animated (e.g. speed, delay, ..)

Slide 44

Slide 44

WAAPI KEYFRAMES keyframes object keyframes array

Slide 45

Slide 45

WAAPI TIMINGS OBJECT ORIENTATION & CONTEXT how fast should my animation run, how often should it run, what should happen after it’s finished?

Slide 46

Slide 46

CSS VS WAAPI CSS Animation

Slide 47

Slide 47

CSS VS WAAPI ORIENTATION & CONTEXT CSS Animation Waapi Animation

Slide 48

Slide 48

WAAPI EXAMPLE defining what should be animated

Slide 49

Slide 49

WAAPI EXAMPLE defining timing of animation

Slide 50

Slide 50

WAAPI EXAMPLE calling the animations

Slide 51

Slide 51

WAAPI EXAMPLE doing something else once animation is finished

Slide 52

Slide 52

Title Text codepen.io/lisilinhart/pen/GRRrYLj

Slide 53

Slide 53

WAAPI EXAMPLE create a dynamic, adapting animation via a function

Slide 54

Slide 54

WAAPI EXAMPLE hand some logical parameters to the animation function

Slide 55

Slide 55

Title Text codepen.io/lisilinhart/pen/GRRrYLj

Slide 56

Slide 56

WHY THE WEB ANIMATIONS API? • • • API provided by the browser • • choreographed & chained animations no extra Javascript code like for libraries can render animations over the compositor thread a native & more powerful alternative to CSS animations

Slide 57

Slide 57

JUNE 2019

Slide 58

Slide 58

OCTOBER 2019

Slide 59

Slide 59

WPT.FYI/RESULTS/WEB-ANIMATIONS

Slide 60

Slide 60

Slide 61

Slide 61

ATTENTION & FEEDBACK

Slide 62

Slide 62

FEEDBACK “Feedback indicates causation between two or more events, often used to connect a user’s interaction with the interface’s reaction” Rachel Nabors - Animation at Work

Slide 63

Slide 63

FEEDBACK I recognised that you clicked this button giving a reaction to user input

Slide 64

Slide 64

FEEDBACK I recognised that you clicked this button giving a reaction to user input You entered some information, but it’s not correct cause & effect of the users input

Slide 65

Slide 65

FEEDBACK I recognised that you clicked this button giving a reaction to user input You entered some information, but it’s not correct cause & effect of the users input You submitted the form, but our system is still processing it making system activity visible

Slide 66

Slide 66

codepen.io/davidkpiano/full/WKvPBP/

Slide 67

Slide 67

ATTENTION

Slide 68

Slide 68

ATTENTION To get to the next step, click this prominent item showing possible actions

Slide 69

Slide 69

ATTENTION To get to the next step, click this prominent item showing possible actions We created this explanatory animation in order for you to better understand our process teaching the user about a process, storytelling

Slide 70

Slide 70

ATTENTION To get to the next step, click this prominent item showing possible actions We created this explanatory animation in order for you to better understand our process teaching the user about a process, storytelling You had to read a lot of information, so we added this kitten animation to entertain you adding fun and reinforcing branding

Slide 71

Slide 71

Title Text stripe.com/us/payments

Slide 72

Slide 72

ATTENTION & FEEDBACK Reactive Animation

Slide 73

Slide 73

“A reactive animation is one involving discrete changes, due to events.” Conal Elliott

Slide 74

Slide 74

read here

Slide 75

Slide 75

Slide 76

Slide 76

JS + CSS VARIABLES the final demo

Slide 77

Slide 77

JS + CSS VARIABLES setting a CSS Variable via JS

Slide 78

Slide 78

JS + CSS VARIABLES using the set variables in your CSS

Slide 79

Slide 79

Title Text codepen.io/lisilinhart/pen/YzzqGNX

Slide 80

Slide 80

JS + CSS VARIABLES get the elements for setting the variables on and define a variable for the requestAnimationFrame

Slide 81

Slide 81

JS + CSS VARIABLES define EventListener and calculate X & Y from event values that are pixels

Slide 82

Slide 82

JS + CSS VARIABLES update DOM inside requestAnimationFrame

Slide 83

Slide 83

REQUESTANIMATIONFRAME FRAME FRAME FRAME FRAME we continuously want to update the screen and get as many frames as possible FRAME

Slide 84

Slide 84

REQUESTANIMATIONFRAME FRAME FRAME rAF(update) FRAME rAF(update) FRAME rAF(update) FRAME rAF(update) so we update once before each new frame

Slide 85

Slide 85

REQUESTANIMATIONFRAME mousemove FRAME FRAME rAF(update) FRAME rAF(update) FRAME rAF(update) FRAME rAF(update)

Slide 86

Slide 86

REQUESTANIMATIONFRAME mousemove FRAME mousemove FRAME rAF(update) FRAME rAF(update) FRAME rAF(update) FRAME rAF(update)

Slide 87

Slide 87

REQUESTANIMATIONFRAME mousemove mousemove FRAME mousemove FRAME rAF(update) mousemove FRAME rAF(update) mousemove FRAME rAF(update) we update the next frame only with the latest value FRAME rAF(update)

Slide 88

Slide 88

JS + CSS VARIABLES set CSS Variables and reset requestAnimationFrame when done

Slide 89

Slide 89

JS + CSS VARIABLES

Slide 90

Slide 90

JS + CSS VARIABLES rotate panel according to the variables

Slide 91

Slide 91

MATRIX 3D ????

Slide 92

Slide 92

I got you MATRIX 3D ????

Slide 93

Slide 93

meyerweb.com/eric/tools/matrix/

Slide 94

Slide 94

MATRIX 3D The matrix3d() function is an alternative to the three dimensional transform functions rotate3d(), rotateX(), rotateY(), rotateZ(), translate3d(), translateZ(), scale3d(), scaleZ(), and perspective().

Slide 95

Slide 95

MATRIX 3D doesn’t need perspective on parent needs perspective on parent

Slide 96

Slide 96

MATRIX 3D doesn’t need perspective on parent without perspective

Slide 97

Slide 97

Title Text codepen.io/lisilinhart/pen/YzzqGNX

Slide 98

Slide 98

JS + CSS VARIABLES • easily debuggable • no excessive DOM manipulation • DOM node independent • great if you animate multiple child elements • great for reactive & physics animation • Transform: Individual Properties

Slide 99

Slide 99

Timing Animation Principles Easing

Slide 100

Slide 100

TIMING valhead.com/2016/05/05/how-fast-should-your-ui-animations-be

Slide 101

Slide 101

TIMING slow transitions are less distracting fast animations are more likely to attract attention valhead.com/2016/05/05/how-fast-should-your-ui-animations-be/

Slide 102

Slide 102

EASINGS.NET

Slide 103

Slide 103

EASINGS.NET

Slide 104

Slide 104

EASING easeOut feels more reactive and instantaneous

Slide 105

Slide 105

EASING easeOut feels more reactive and instantaneous needs more time, because it’s a more complex curve

Slide 106

Slide 106

EASING when an element is moving into the screen it should start quick and slow down towards the center

Slide 107

Slide 107

EASING when an element is moving out of the screen it should start slow and speed up towards the end

Slide 108

Slide 108

Opportunities for Animation

Slide 109

Slide 109

make idle time more useful and interesting Opportunities for Animation

Slide 110

Slide 110

make idle time more useful and interesting guide the user in a process Opportunities for Animation

Slide 111

Slide 111

make idle time more useful and interesting guide the user in a process Opportunities for Animation explain relationships in the information space

Slide 112

Slide 112

make idle time more useful and interesting guide the user in a process Opportunities for Animation get the users attention explain relationships in the information space

Slide 113

Slide 113

Animation Resources

Slide 114

Slide 114

Animation Resources Animation At Work - A Book Apart Book by Rachel Nabors

Slide 115

Slide 115

Animation Resources Animation At Work - A Book Apart Twitch Channel by David Khourshid & Stephen Shaw keyframe.rs Book by Rachel Nabors

Slide 116

Slide 116

Animation Resources Animation At Work - A Book Apart Twitch Channel by David Khourshid & Stephen Shaw keyframe.rs Book by Rachel Nabors animationatwork.slack.com Slack Channel to get feedback & inspiration

Slide 117

Slide 117

Animation Resources Animation At Work - A Book Apart Twitch Channel by David Khourshid & Stephen Shaw basics to know about animation keyframe.rs Book by Rachel Nabors animationatwork.slack.com developers.google.com/web/ fundamentals/design-and-ux/animations/ Slack Channel to get feedback & inspiration

Slide 118

Slide 118

“Users should only notice your animation if you need to attract their attention in that moment. Otherwise, micro-interactions and other transitions should be so seamless, users don’t even notice that there is animation” Heather Daggett - Animation At Work

Slide 119

Slide 119

Animate for the user, don’t create obstacles,

Slide 120

Slide 120

Lecturing @ LISILINHART.INFO Twitter @LISI_LINHART Blog