alice in web animations api land

120
@RachelNabors .com

Upload: rachel-nabors

Post on 14-Apr-2017

2.193 views

Category:

Design


0 download

TRANSCRIPT

Page 1: Alice in Web Animations API Land

@RachelNabors .com

Page 2: Alice in Web Animations API Land

light

ning

desig

nsys

tem

.com

/des

ign/

mo0

on

Page 3: Alice in Web Animations API Land

It’s t

rue.

rach

elth

egre

at.co

m

Page 4: Alice in Web Animations API Land

surgery.

Page 5: Alice in Web Animations API Land
Page 6: Alice in Web Animations API Land

Some years later…

Page 7: Alice in Web Animations API Land
Page 8: Alice in Web Animations API Land
Page 9: Alice in Web Animations API Land
Page 10: Alice in Web Animations API Land

rach

elna

bors

.com

/alic

e-in

-vid

eola

nd/

book

Page 11: Alice in Web Animations API Land
Page 12: Alice in Web Animations API Land
Page 13: Alice in Web Animations API Land

HistoryGOSSIP

OMG!!!1!

Page 14: Alice in Web Animations API Land

Birth of SMIL, 1999

SMIL

Page 15: Alice in Web Animations API Land

Birth of CSS Anima5ons and Transi5ons, 2009

CSS Transi-ons

CSS Anima-ons

Page 16: Alice in Web Animations API Land

CSS Transi-ons

CSS Anima-ons SMIL

Seriously?

Internet Explorer’s Reac5on

Page 17: Alice in Web Animations API Land

One API to Rule Them All

CSS Transi-ons

CSS Anima-ons SMIL

?

Page 18: Alice in Web Animations API Land

Some years later…

Page 19: Alice in Web Animations API Land

?Web Anima-ons

API

CSS Transi-ons

CSS Anima-ons SMIL

Page 20: Alice in Web Animations API Land

CSS Transi-ons

CSS Anima-ons SMIL

?Web Anima-ons

API

la mort de SMIL, 2015

Page 21: Alice in Web Animations API Land

Fire

fox

Dev

elop

er E

di0o

n

Page 22: Alice in Web Animations API Land
Page 23: Alice in Web Animations API Land
Page 24: Alice in Web Animations API Land

with

@RachelNabors

& Alice

Page 25: Alice in Web Animations API Land

Core Concepts

Page 26: Alice in Web Animations API Land

The Timing & Anima5on Models

The When The What

Page 27: Alice in Web Animations API Land

Timing

Page 28: Alice in Web Animations API Land

the Cheshire Cat’s 5meline

there

0 seconds

not all there

8 seconds4 seconds2 61 3 5 7

Page 29: Alice in Web Animations API Land

“Time may be shi2ed backwards and forwards, scaled, reversed,

paused, and repeated.”

–Web Anima0ons API spec

Page 30: Alice in Web Animations API Land

Stateless Animation

Frame Rate Independent

Direc8on Agnos8c

Seek-able

Page 32: Alice in Web Animations API Land

Animation

Page 33: Alice in Web Animations API Land

0 seconds 8 seconds4 seconds2 61 3 5 7

there not all there

the Cheshire Cat’s 5meline

Keyframe Effect

Page 34: Alice in Web Animations API Land

Do you even remember me?

KeyframeEffect

Anima-on

Page 35: Alice in Web Animations API Land

Timing Anima-on

Page 36: Alice in Web Animations API Land

Web Anima-ons

API

CSS Transi-ons

CSS Anima-ons SMIL

Web Anima5ons API underlies both CSS Anima5ons and Transi5ons

Page 37: Alice in Web Animations API Land

CSS Animations & Transitions

WEB ANIMATIONS API

Page 38: Alice in Web Animations API Land

I’ve a CSS AnimaCons

& TransiCons course… rachelnabors.com/

css-animaCons-course

Page 39: Alice in Web Animations API Land

KeyframeEffect

Anima-on

Page 40: Alice in Web Animations API Land

KeyframeEffect Constructor

Page 41: Alice in Web Animations API Land

Interac5on Development

Tradi5onal Anima5on

tweenskeyframe keyframe

key keyin-betweens

Page 42: Alice in Web Animations API Land

0 seconds 8 seconds4 seconds2 61 3 5 7

there not all there

Keyframe Effect

Page 44: Alice in Web Animations API Land

#rabbit { transition: transform 3s;

}

#rabbit.interacted { transform: translateY(100%);}

Page 45: Alice in Web Animations API Land

cdpn

.io/e

JyW

zm

Page 46: Alice in Web Animations API Land

new KeyframeEffect(

);

whiteRabbit, [ { transform: 'translateY(0%)' }, { transform: 'translateY(100%)' } ], { duration: 3000, fill: 'forwards' }

var rabbitDownKeyframes =

Page 47: Alice in Web Animations API Land

cdpn

.io/e

JyW

zm

Page 48: Alice in Web Animations API Land

Familiar Keyframe timing options

duration = transition/animation-duration

delay = transition/animation-delay

fill = animation-fill-mode

iterations = animation-iteration-count

direction = animation-direction

easing = transition/animation-timing-function; Defaults to linear.

Page 49: Alice in Web Animations API Land

Shiny Keyframe timing options

endDelay Milliseconds to delay aDer the end of an anima8on.

iterationStart When the itera8on the anima8on should start.

composite, iteration-composite, spacing

Page 50: Alice in Web Animations API Land

Animation Constructor

Page 51: Alice in Web Animations API Land

var rabbitDownKeyframes = new KeyframeEffect( whiteRabbit, [ { transform: 'translateY(0%)' }, { transform: 'translateY(100%)' } ], { duration: 3000, fill: 'forwards' } );

Page 52: Alice in Web Animations API Land

KeyframeEffect

Anima-on

Page 53: Alice in Web Animations API Land

new Animation(rabbitDownKeyframes, document.timeline);

var rabbitDownAnimation = new Animation(rabbitDownKeyframes, document.timeline);new Animation(rabbitDownKeyframes, document.timeline);

Page 54: Alice in Web Animations API Land

Animation Methods

Animation.pause()

Animation.play()

Animation.reverse()

Animation.finish()

Animation.cancel()

Page 55: Alice in Web Animations API Land

whiteRabbit.removeEventListener( "click", downHeGoes);

function downHeGoes() {

}

whiteRabbit.addEventListener("click", downHeGoes);

rabbitDownAnimation.play();

Page 56: Alice in Web Animations API Land

cdpn

.io/e

JyW

zm

Page 57: Alice in Web Animations API Land

Fortunately, we have a shortcut.

Page 58: Alice in Web Animations API Land

animate( ) Method

Page 59: Alice in Web Animations API Land

#rabbit { transition: transform 3s;

}

#rabbit.interacted { transform: translateY(100%);

}

Page 60: Alice in Web Animations API Land

#rabbit.interacted { animation: downHeGoes 3s forwards;

}

@keyframes downHeGoes { 0% { transform: translateY(0%);

} 100% { transform: translateY(100%);

} }

Page 61: Alice in Web Animations API Land

cdpn

.io/Q

yOqq

W

Page 62: Alice in Web Animations API Land

#alice { animation: aliceTumbling infinite 3s linear; }

@keyframes aliceTumbling { 0% { color: #000; transform: rotate(0) …; } 30% { color: #431236; } 100% { color: #000; transform: rotate(360deg) …; } }

Page 63: Alice in Web Animations API Land

element.animate( keyframes, timingOptions );

Page 64: Alice in Web Animations API Land

var aliceKeyframes = [ { transform: 'rotate(0) …', color: '#000' }, { color: '#431236',

}, { transform: 'rotate(360deg) …', color: '#000' } ];

offset: 0.3

Page 65: Alice in Web Animations API Land

var aliceTiming = { duration: 3000, iterations: Infinity }

Page 66: Alice in Web Animations API Land

element.animate( keyframes, timingOptions );

Page 67: Alice in Web Animations API Land

document.getElementById("alice") .animate( aliceTumbling, aliceTiming )

Page 69: Alice in Web Animations API Land

Timelines & Callbacks

Page 70: Alice in Web Animations API Land

Anima-on

Page 71: Alice in Web Animations API Land

Animation Attributes

onfinish promise

oncancel promise

ready promise

playState read-only, use methods

playbackRate more on you later…

effect points to…

Page 72: Alice in Web Animations API Land

KeyframeEffect

Anima-on

Page 73: Alice in Web Animations API Land

Animation Attributes

currentTime loca8on on 8meline

finished callback

Page 74: Alice in Web Animations API Land
Page 75: Alice in Web Animations API Land
Page 76: Alice in Web Animations API Land

goo.

gl/E

k7T5

h

Page 77: Alice in Web Animations API Land
Page 78: Alice in Web Animations API Land

var aliceChange = document.getElementById('alice').animate( [ { transform: 'scale(.5) …' }, { transform: 'scale(2) …' } ], aliceTimingOptions);

aliceChange.pause();

Page 79: Alice in Web Animations API Land

goo.

gl/E

k7T5

h

Page 80: Alice in Web Animations API Land

var aliceChange = document.getElementById('alice').animate( [ { transform: 'scale(.5) …' }, { transform: 'scale(2) …' } ], aliceTimingOptions);

aliceChange.pause();

aliceChange.currentTime = aliceChange.effect.timing.duration / 2;

Page 81: Alice in Web Animations API Land

Setting the Controls

Page 82: Alice in Web Animations API Land
Page 83: Alice in Web Animations API Land

var shrinkAlice = function() { aliceChange.playbackRate = -1; aliceChange.play();

// bottle’s animation drinking.play(); }

drinking.addEventListener("mousedown", shrinkAlice);

var shrinkAlice = function() { aliceChange.playbackRate = -1; aliceChange.play();

// bottle’s animation drinking.play(); }

drinking.addEventListener("mousedown", shrinkAlice);

var shrinkAlice = function() { aliceChange.playbackRate = -1; aliceChange.play();

// bottle’s animation drinking.play(); }

drinking.addEventListener("mousedown", shrinkAlice);

var shrinkAlice = function() { aliceChange.playbackRate = -1; aliceChange.play();

// bottle’s animation drinking.play(); }

drinking.addEventListener("mousedown", shrinkAlice);

var shrinkAlice = function() { aliceChange.playbackRate = -1; aliceChange.play();

// bottle’s animation drinking.play(); }

drinking.addEventListener("mousedown", shrinkAlice);

var shrinkAlice = function() {

aliceChange.reverse();

// bottle’s animation drinking.play(); }

drinking.addEventListener("mousedown", shrinkAlice);

Page 84: Alice in Web Animations API Land

goo.

gl/E

k7T5

h

Page 85: Alice in Web Animations API Land

var growAlice = function() { aliceChange.playbackRate = 1; aliceChange.play();

// play cake’s animation nommingCake.play(); }

cake.addEventListener("mousedown", growAlice);

Page 86: Alice in Web Animations API Land

var stopPlayingAlice = function() { aliceChange.pause(); nommingCake.pause(); drinking.pause(); };

bottle.addEventListener("mouseup", stopPlayingAlice); cake.addEventListener("mouseup", stopPlayingAlice);

var stopPlayingAlice = function() { aliceChange.pause(); nommingCake.pause(); drinking.pause(); };

bottle.addEventListener("mouseup", stopPlayingAlice); cake.addEventListener("mouseup", stopPlayingAlice);

var stopPlayingAlice = function() { aliceChange.pause(); nommingCake.pause(); drinking.pause(); };

bottle.addEventListener("mouseup", stopPlayingAlice); cake.addEventListener("mouseup", stopPlayingAlice);

Page 87: Alice in Web Animations API Land

Game Over: Two Endings

Page 88: Alice in Web Animations API Land
Page 89: Alice in Web Animations API Land
Page 90: Alice in Web Animations API Land

cdpn

.io/b

EPdQ

r

Page 91: Alice in Web Animations API Land

cdpn

.io/E

PJdJ

x

Page 92: Alice in Web Animations API Land
Page 93: Alice in Web Animations API Land
Page 94: Alice in Web Animations API Land

cdpn

.io/P

NYG

ZQ

Page 95: Alice in Web Animations API Land

// When the cake or runs out... nommingCake.onfinish = endGame; drinking.onfinish = endGame;

// ...or Alice reaches the end of her animation aliceChange.onfinish = endGame;

// When the cake or runs out... animation.onfinish = endGame; drinking.onfinish = endGame;

// ...or Alice reaches the end of her animation aliceChange.onfinish = endGame;

// When the cake or runs out... nommingCake.onfinish = endGame; drinking.onfinish = endGame;

// ...or Alice reaches the end of her animation aliceChange.onfinish = endGame;

Page 96: Alice in Web Animations API Land

var endGame = function() {

var alicePlayhead = aliceChange.currentTime;

var aliceTimeline = aliceChange.effect.activeDuration;

var aliceHeight = alicePlayhead/aliceTimeline;

}

var endGame = function() {

var alicePlayhead = aliceChange.currentTime;

var aliceTimeline = aliceChange.effect.activeDuration;

var aliceHeight = alicePlayhead/aliceTimeline;

}

var endGame = function() {

var alicePlayhead = aliceChange.currentTime;

var aliceTimeline = aliceChange.effect.activeDuration;

var aliceHeight = alicePlayhead/aliceTimeline;

}

var endGame = function() {

var alicePlayhead = aliceChange.currentTime;

var aliceTimeline = aliceChange.effect.activeDuration;

var aliceHeight = alicePlayhead/aliceTimeline;

}

Page 97: Alice in Web Animations API Land

var aliceHeight = alicePlayhead/aliceTimeline;

if (aliceHeight <= .333){ // Alice got smaller! … } else if (aliceHeight >= .666) { // Alice got bigger! … } else { // Alice didn't change significantly … }

var aliceHeight = alicePlayhead/aliceTimeline;

if (aliceHeight <= .333){ // Alice got smaller! … } else if (aliceHeight >= .666) { // Alice got bigger! … } else { // Alice didn't change significantly … }

Page 98: Alice in Web Animations API Land

var endGame = function() { stopPlayingAlice();

var alicePlayhead = aliceChange.currentTime; var aliceTimeline = aliceChange.effect.activeDuration;

var aliceHeight = alicePlayhead/aliceTimeline;

if (aliceHeight <= .333){ // Alice got smaller! … } else if (aliceHeight >= .666) { // Alice got bigger! … } else { // Alice didn't change significantly … } }

var endGame = function() { stopPlayingAlice();

var alicePlayhead = aliceChange.currentTime; var aliceTimeline = aliceChange.effect.activeDuration;

var aliceHeight = alicePlayhead/aliceTimeline;

if (aliceHeight <= .333){ // Alice got smaller! … } else if (aliceHeight >= .666) { // Alice got bigger! … } else { // Alice didn't change significantly … } }

Page 99: Alice in Web Animations API Land

Bonus: Randomizing Animation

Page 100: Alice in Web Animations API Land

cdpn

.io/E

PJdJ

x

Page 101: Alice in Web Animations API Land

var getRandomMsRange = function(min, max) { return Math.random() * (max - min) + min; }

tears.forEach(function(tear) { tear.animate( tearsFalling, { delay: getRandomMsRange(-1000, 1000), duration: getRandomMsRange(2000, 6000), iterations: Infinity, easing: "cubic-bezier(0.6, 0.04, 0.98, 0.335)" }); });

var getRandomMsRange = function(min, max) { return Math.random() * (max - min) + min; }

tears.forEach(function(tear) { tear.animate( tearsFalling, { delay: getRandomMsRange(-1000, 1000), duration: getRandomMsRange(2000, 6000), iterations: Infinity, easing: "cubic-bezier(0.6, 0.04, 0.98, 0.335)" }); });

var getRandomMsRange = function(min, max) { return Math.random() * (max - min) + min; }

tears.forEach(function(tear) { tear.animate( tearsFalling, { delay: getRandomMsRange(-1000, 1000), duration: getRandomMsRange(2000, 6000), iterations: Infinity, easing: "cubic-bezier(0.6, 0.04, 0.98, 0.335)" }); });

var getRandomMsRange = function(min, max) { return Math.random() * (max - min) + min; }

tears.forEach(function(tear) { tear.animate( tearsFalling, { delay: getRandomMsRange(-1000, 1000), duration: getRandomMsRange(2000, 6000), iterations: Infinity, easing: "cubic-bezier(0.6, 0.04, 0.98, 0.335)" }); });

var getRandomMsRange = function(min, max) { return Math.random() * (max - min) + min; }

tears.forEach(function(tear) { tear.animate( tearsFalling, { delay: getRandomMsRange(-1000, 1000), duration: getRandomMsRange(2000, 6000), iterations: Infinity, easing: "cubic-bezier(0.6, 0.04, 0.98, 0.335)" }); });

Page 102: Alice in Web Animations API Land

Playback Rate

Page 103: Alice in Web Animations API Land

The Red Queen’s Race

Page 104: Alice in Web Animations API Land

cdpn

.io/G

ZpRE

z

Page 105: Alice in Web Animations API Land

var redQueen_alice = redQueen_alice_sprite.animate( spriteFrames, { easing: 'steps(6, end)', direction: "reverse", duration: 600, iterations: Infinity, playbackRate: 1 });

var redQueen_alice = redQueen_alice_sprite.animate( spriteFrames, { easing: 'steps(6, end)', direction: "reverse", duration: 600, iterations: Infinity, playbackRate: 1 });

Page 106: Alice in Web Animations API Land

setInterval( function() { if (redQueen_alice.playbackRate > .4) { redQueen_alice.playbackRate *= .9; } }, 3000);

setInterval( function() { if (redQueen_alice.playbackRate > .4) { redQueen_alice.playbackRate *= .9; } }, 3000);

setInterval( function() { if (redQueen_alice.playbackRate > .4) { redQueen_alice.playbackRate *= .9; } }, 3000);

Page 107: Alice in Web Animations API Land

var goFaster = function() { redQueen_alice.playbackRate *= 1.1; }

document.addEventListener("click", goFaster); document.addEventListener("touchstart", goFaster);

Page 108: Alice in Web Animations API Land

Running to Stay in Place

Page 109: Alice in Web Animations API Land

cdpn

.io/P

NGG

aV

Page 110: Alice in Web Animations API Land

What’s Next

Page 111: Alice in Web Animations API Land

gith

ub.co

m/w

eb-a

nim

a0on

s/w

eb-

anim

a0on

s-js

Page 112: Alice in Web Animations API Land

Support for the Web Anima5ons API

Let’s do this.

I guess.

Page 113: Alice in Web Animations API Land
Page 114: Alice in Web Animations API Land

Spiri

t.js

Page 115: Alice in Web Animations API Land

Chro

me

Cana

ry

Page 116: Alice in Web Animations API Land

Grou

ping

and

Seq

uenc

ing

goo.

gl/

1zH

64t

Page 117: Alice in Web Animations API Land

Timing Anima-on

Page 118: Alice in Web Animations API Land

Web Anima-ons

API

CSS Transi-ons

CSS Anima-ons

Web Anima5ons API opens the door to future anima5on specs

?

Page 119: Alice in Web Animations API Land

Ace Folks

Alex Miller Opal Essence

Chris Mills Brian Birtles

Page 120: Alice in Web Animations API Land

@RachelNabors .com

RachelNabors.com/waapi WebAnimaConWeekly.com

slack.AnimaConAtWork.com

Available for all your animaCon needs.