Gaining Control with the Web Animations API

A presentation at ThunderPlains in October 2019 in Oklahoma City, OK, USA by Dan Wilson

Slide 1

Slide 1

GAINING CONTROL W IT H T H E WE B A NIMATI O NS API THUNDERPLAINS 2019 Dan Wilson | @dancwilson Slides: Soon at https://danielcwilson.com

Slide 2

Slide 2

HOW DO WE ANIMATE ON THE WEB? Straight up setTimeout/setInterval jQuery animate() requestAnimationFrame <canvas> WebGL CSS Transitions and Keyframe Animations SVG Animation Libraries such as GreenSock

Slide 3

Slide 3

ENTER THE WEB ANIMATIONS API W3C Editor’s Dra Unite the various SVG/CSS/JS ways to animate in the DOM

Slide 4

Slide 4

THE WAA? The benefits of SVG/CSS/JS Off main-thread (compositor layer) Dynamic values Timelines and playback control Callbacks

Slide 5

Slide 5

MORE ON THAT COMPOSITOR LAYER… Repainting and reflowing layout Properties like transform can be animated independently Like traditional cel animation

Slide 6

Slide 6

WHAT’S AVAILABLE TODAY? Firefox, Chrome, and Opera have foundation implemented Safari is very close (in Tech Preview or behind a flag) Edge will be Blink soon How Chrome is Implementing the API Polyfill available

Slide 7

Slide 7

CREATE AN ANIMATION Transitioning from one state to another var anim = document.getElementById(‘toAnimate’).animate([ { transform: ‘scale(1)’ }, { transform: ‘scale(.6)’ } ], { duration: 700, //milliseconds iterations: Infinity, //or a number direction: ‘normal’, //’alternate’, ‘reverse’, … fill: ‘forwards’, //’backwards’, ‘both’, ‘none’, ‘auto’ delay: 10, //milliseconds easing: ‘ease-in-out’, //’linear’, ‘ease-in’, … });

Slide 8

Slide 8

CREATE AN ANIMATION Animating multiple frames, multiple properties var anim = document.getElementById(‘toAnimate2’).animate([ { transform: ‘scale(1)’, opacity: 1, offset: 0 }, { transform: ‘scale(.5)’, opacity: .5, offset: .333333 }, { transform: ‘scale(.667)’, opacity: .667, offset: .666667 }, { transform: ‘scale(.6)’, opacity: .6, offset: 1 } ], { duration: 700, iterations: 30, direction: ‘alternate’, fill: ‘forwards’ });

Slide 9

Slide 9

PRETTY MUCH LOOKS LIKE… @keyframes emphasis { 0% { transform: scale(1); opacity: 1; } 33.3333% { transform: scale(.5); opacity: .5; } 66.6667% { transform: scale(.667); opacity: .667; } 100% { transform: scale(.6); opacity: .6; } } #toAnimate2 { animation: emphasis 700ms linear 0s 3 alternate forwards; }

Slide 10

Slide 10

BUT IF IT ALREADY HAS AN EQUIVALENT IN CSS… Keep benefits of CSS, such as compositor layer Variables (vs. Declarative) Finer control Player controls

Slide 11

Slide 11

CONTROLS AND PLAYSTATES var anim = element.animate(/* animation */); console.log(anim.playState); //”running” anim.pause(); //”paused” anim.play(); //”running” anim.cancel(); //”idle”… jump to original state anim.finish(); //”finished”…jump to end state CodePen Demo (Walking Circles)

Slide 12

Slide 12

PLAYER TIMELINE var anim = element.animate(/* animation */); anim.currentTime = 200; Read the current time… or set it to jump Sync multiple animations together Max value is delay + (duration * iterations) CodePen: Sync

Slide 13

Slide 13

PLAYBACKRATE var anim = element.animate(/* animation */); anim.playbackRate = .25; //.25x speed Slow it down or speed it up currentTime will account for playbackRate CodePen: Filling the Gaps 1x

Slide 14

Slide 14

EVENTS Callbacks (or Promises) for onfinish and oncancel Similar to end and cancel events for CSS animations Can be used for basic sequencing, such as with a sing along Or little games (also: CodePen version)

Slide 15

Slide 15

INTERACTING WITH MULTIPLE ANIMATIONS Building blocks for other features Can build a timeline scrubber for multiple animations Demo: Scrubbing

Slide 16

Slide 16

WHAT ARE THE CATCHES? Native browser support (Caniuse.com | Feature Breakdown) Some minor inconsistencies with CSS Some of the more exciting features are yet to come…

Slide 17

Slide 17

WHAT ELSE IS COMING?

Slide 18

Slide 18

GET ALL ANIMATIONS In nightly/canary versions Get references to all animations CodePen: Pause All the Dots

Slide 19

Slide 19

CSS MOTION PATH Animate along a path! In Chrome and Firefox Nightly CodePen Collection | More Demos Demo: offset-path and d

Slide 20

Slide 20

THE COMPOSITE TIMING OPTION CSS properties that take multiple values Subsequent animations on the same property override composite allows for adding values Firefox Nightly, Chrome Canary, and Safari Tech Preview Talk of how to get into CSS Demo: Transforms | Demo: Bending

Slide 21

Slide 21

CSS! CSS animations can be WAAPI-ified document.getAnimations() will also get CSS animations and transitions Access to player controls Firefox Nightly, Chrome Canary, and Safari Tech Preview Demo: HyperCSS

Slide 22

Slide 22

CSS… HOUDINI! Houdini allows us to use CSS Variables in new ways Individualize the parts of a property e.g. animate scale separately from rotate within a transform Chrome Canary Demo: CSS Variables

Slide 23

Slide 23

AND MORE… SetKeyframes Grouping & Sequencing Even timelines that are not related to time…

Slide 24

Slide 24

WHEN TO USE WAAPI OVER CSS Randomized Values (Confetti) Modifying keyframes (Custom Springs) Class Toggling-itis (already tying into JS events) Sequencing Integrating with JS frameworks

Slide 25

Slide 25

SO HAVE WE FINALLY DONE IT? Did we solve all our animation needs?

Slide 26

Slide 26

WANT TO KNOW MORE? MDN docs from Rachel Nabors slack.animationatwork.com uianimationnewsletter.com from Val Head Shop Talk Show: Episodes 216 and 203 Web Animations API series

Slide 27

Slide 27

THANK YOU VERY MUCH! Slides: Soon at https://danielcwilson.com CodePen: @DanWilson Twitter: @DanCWilson Yes… I was inconsistent with my usernames. I’ve learned my lesson for the future.