Native Web Animation

A presentation at code.talks in October 2018 in Hamburg, Germany by Lisi Linhart

Slide 1

Slide 1

Slide 2

Slide 2

Slide 3

Slide 3

Slide 4

Slide 4

I just want the coffee to be done.

Slide 5

Slide 5

Slowly it’s starting to make a noise, but it’s still taking another minute or two until it’s done. I’m getting really impatient.

Slide 6

Slide 6

I feel more like this. And with animation it isn’t that much different.

Slide 7

Slide 7

Animation should never delay the user in what he’s trying to do. Otherwise, small interactions and other transitions should be so seamless that users don’t even notice that there is animation.

Slide 8

Slide 8

Slide 9

Slide 9

Slide 10

Slide 10

Performance is a key factor in creating good animation. When we understand how to Browser works, we can create animations that perform well.

Slide 11

Slide 11

Slide 12

Slide 12

Slide 13

Slide 13

Slide 14

Slide 14

Slide 15

Slide 15

Slide 16

Slide 16

Here we’re animating the left property, which is really not performant. Instead we should use the CSS Transform property.

Slide 17

Slide 17

Slide 18

Slide 18

Slide 19

Slide 19

One the first step, some change happens, most likely an event is fired

Slide 20

Slide 20

Then we need to recalculate the styles depending on the change

Slide 21

Slide 21

Then the browser will calculate the layout, how much space does every element need?

Slide 22

Slide 22

Everything gets rasterised and put onto layers. Elements that are on different layers can be moved independently

Slide 23

Slide 23

Composite: Browser takes advantage of GPU, layers get glued together to 1 frame that the user sees

Slide 24

Slide 24

All of this has to happen in one frame, if we have long layout calculations, we get less frames per second

Slide 25

Slide 25

Layout is very expensive, because the browser needs to recalculate and go through all the following steps

Slide 26

Slide 26

With Paint the browser already knows how much space the element takes

Slide 27

Slide 27

Compositing is the most performant, because the browser doesn't have to figure out layout or paint.

Slide 28

Slide 28

Try to only animate with composite

Slide 29

Slide 29

Another important part is looking at your devtools and testing your animations.

Slide 30

Slide 30

Depeding on what CSS property you change, different stages in the rendering get triggered, some properties will trigger recalculations of style, while other’s only trigger composite.

Slide 31

Slide 31

Slide 32

Slide 32

Layers have been around for a long time. They originated in Cel animation, which was the core technique for doing traditional 2d animation. To create a cartoon, you would have transparent slates for each character and the different body parts and different layers for the background. By putting them on top of each other, cartoons could be created quicker and more efficiently than before.

Slide 33

Slide 33

Slide 34

Slide 34

Slide 35

Slide 35

Slide 36

Slide 36

Slide 37

Slide 37

Slide 38

Slide 38

Slide 39

Slide 39

Slide 40

Slide 40

There are also gotchas with layers.

Slide 41

Slide 41

don’t promote too many elements

Slide 42

Slide 42

Slide 43

Slide 43

Slide 44

Slide 44

Slide 45

Slide 45

Slide 46

Slide 46

Slide 47

Slide 47

Slide 48

Slide 48

promote elements to new layers if animated often & constantly

Slide 49

Slide 49

🤯 don’t create too many layers

Slide 50

Slide 50

animate elements on top layers

Slide 51

Slide 51

inspect & test your animations in your devtools

Slide 52

Slide 52

How can we animate on the web?

Slide 53

Slide 53

Slide 54

Slide 54

Slide 55

Slide 55

CSS

Slide 56

Slide 56

Slide 57

Slide 57

Slide 58

Slide 58

Slide 59

Slide 59

Slide 60

Slide 60

Slide 61

Slide 61

Loading animations are a great use case for CSS Animation, because you’re Javascript is probably still parsing and you want to take advantage of the GPU to show animations, while your Javascript is busy.

Slide 62

Slide 62

Slide 63

Slide 63

Slide 64

Slide 64

Slide 65

Slide 65

Slide 66

Slide 66

Slide 67

Slide 67

Slide 68

Slide 68

Reactive animation is one of the best examples for Javascript animation, pulling the user into the experience

Slide 69

Slide 69

Slide 70

Slide 70

CSS Variables offer a lot more flexibility than just doing Javascript manipulation on one single object.

Slide 71

Slide 71

Slide 72

Slide 72

Slide 73

Slide 73

CSS Variables are inherited from their parent, for better performance set them at the most specific level performance implications for setting CSS variables on a parent with a vast amount children.

Slide 74

Slide 74

They are in all major browsers and if you need to support IE, you could probably fall back to no animation on IE.

Slide 75

Slide 75

Slide 76

Slide 76

Splittingjs is a great example for using CSS Variables in combination with Javascript. It splits up words & letters by using CSS Variables and then animates them independently

Slide 77

Slide 77

Slide 78

Slide 78

Slide 79

Slide 79

Slide 80

Slide 80

Slide 81

Slide 81

No JS Stalling in CSS & WAAPI

Slide 82

Slide 82

Unfortunately there is not full support for the web animations api yet, but in Safari it’s already in the technology preview. In Edge we’re still waiting for signals

Slide 83

Slide 83

But if we take a look into the platform status of Edge, there are lots of votes on the Web Animations API and it’s under consideration, so there are good chances that it will be implemented in the near future.

Slide 84

Slide 84

Slide 85

Slide 85

Slide 86

Slide 86

Slide 87

Slide 87

Slide 88

Slide 88

Slide 89

Slide 89

How to animate with the WAAPI? keyframes object or array: stages of the animation timings object: how it's animated (e.g. speed, delay, ..)

Slide 90

Slide 90

Slide 91

Slide 91

Slide 92

Slide 92

Slide 93

Slide 93

One tiny library I want to mention is Animate Plus, which uses the web animations api under the hood and provides an easier way to work with staggering animations and making use of callbacks once the animation is finished.

Slide 94

Slide 94

Slide 95

Slide 95

Slide 96

Slide 96

Slide 97

Slide 97

Slide 98

Slide 98

There are a lot more easings, than just the regular ease-in and ease-out.

Slide 99

Slide 99

If we take a look at easings.net, these are easing we can use in CSS & JS

Slide 100

Slide 100

More complex easing can only be done in JS

Slide 101

Slide 101

Slide 102

Slide 102

When I started web animation and tried to find the right easing, I made a lot of use of the greenock ease-visualizer, which gives you direct feedback on how an easing can be denied

Slide 103

Slide 103

You can simple import easings you use a lot as CSS or SASS variables, so you don’t have to define them by hand each time.

Slide 104

Slide 104

So now we talked about a lot of native options on how to do native animation.

Slide 105

Slide 105

My key point is that you don’t always need a huge animation library to include useful animations in your project.

Slide 106

Slide 106

Slide 107

Slide 107

Slide 108

Slide 108

Slide 109

Slide 109