Algorithms for Animation

Simple formulas to activate your UI

Courtney Hemphill


Partner & Tech Lead
courtney@carbonfive.com

@chemphill
#html5devconf

DesignVDev

Animations are cognitive aids

Affordance, percieved affordance & signifiers

Motion detection

Navigation

Adrian Zumbrunnen
http://www.smashingmagazine.com/2013/10/23/smart-transitions-in-user-experience-design/
Adrian Zumbrunnen
http://www.smashingmagazine.com/2013/10/23/smart-transitions-in-user-experience-design/

Context

Math

Moving pixels

var ball = document.getElementById('ball');					
var start = 0;

var basicAnimation = function (e) {							
	start += 12;	     
	basic.style.left = start + "px";
	if (Math.abs(start) <= 800) { requestAnimationFrame(basicAnimation); }	
}
	
>

The basics of animation: interpolation

valueAtTime = (end - start) * time / duration + start

Breaking it down to [0-1]

valueAtTime = (end - start) * time / duration + start

change = end - start

percent complete = time/duration

Timing


var time, startTime;		

var start = function () {
	startTime = new Date().getTime();
	run();
}

var run = function () {							
	time = new Date().getTime() - startTime;
	div.style.left = 1000 * time / 1000 + "px";

	if(value < 1) requestAnimationFrame(run);
}
	
>
	//valueAtTime = (end - start) * time / duration + start
	div.style.left = 1000-0 * time / 1000 + 0 + "px";

	
>

"Using a term like nonlinear science is like referring to the bulk of zoology as the study of non-elephant animals."

- Stanislaw Ulam

Easing functions

Easing

	valueAtTime = (end - start) * easingfunction([0-1]) + start		

Change in property times (some float) plus beginning value.

Power Functions - EaseIn

var run = function () {							
  time = new Date().getTime() - startTime;  
  div.style.left = 1000 * Math.pow(percentChange, 3) + "px";

  if(time / duration < 1) requestAnimationFrame(run);
}
>

Power Functions - EaseOut


var run = function () {							
  time = new Date().getTime() - startTime;  
  div.style.left=(endX - startX)* (1 - Math.pow(1 - (t / d), 3)) +startX+"px";
  if(time / duration < 1) requestAnimationFrame(run);
}
	
>

Trig! ... sine :)

var run = function () {							
  time = new Date().getTime() - startTime;  
  div.style.left=(endX - startX)* Math.sin( t/d * Math.PI / 2 ) +startX+"px";
  if(time / duration < 1) requestAnimationFrame(run);
}
>

Follow Through

> 1

Elasticity

var s = 1.70158;
var run = function () {							
  time = new Date().getTime() - startTime;  
  k = time / duration;
  div.style.left=(endX - startX)* k * k * ( ( s + 1 ) * k - s ) +startX+"px";
  if(time / duration < 1) requestAnimationFrame(run);
}
>

Bounce

var easeFunc = function(k) {
if ( k < ( 1 / 2.75 ) ) { 
return 7.5625 * k * k;
} else if ( k < ( 2 / 2.75 ) ) { 
return 7.5625 * ( k -= ( 1.5 / 2.75 ) ) * k + 0.75;
} else if ( k < ( 2.5 / 2.75 ) ) {	
return 7.5625 * ( k -= ( 2.25 / 2.75 ) ) * k + 0.9375;
} else { 
return 7.5625 * ( k -= ( 2.625 / 2.75 ) ) * k + 0.984375; }
}
div.style.left=(endX - startX)* easeFunc(t/d) +startX+"px";
>

Birds on a Wire

Tools

  • Software
  • JS Frameworks
  • CSS

Software

  • Adobe Edge Animate
  • Adobe After Effects
  • Flinto
  • Keynote
  • Quartz

JS Frameworks

  • Framer.js
  • Tween.js
  • GSAP (Greensock)
  • jQuery

.box {
-webkit-transform: translate(0, 0);
-webkit-transition: -webkit-transform 500ms;

transform: translate(0, 0);
transition: transform 500ms;
}


.box.move {
-webkit-transform: translate(100px, 100px);
transform: translate(100px, 100px);
}

CSS

  • Animate.css
  • SASS/LESS mixins

Performance

  • CPU vs GPU
  • Transforms (scale, rotation, translation, and skew) and opacity
  • css layer doesn't have to redraw full canvas

Go forth and animate!

References

Courtney Hemphill
@chemphill
courtney@carbonfive.com