Created
April 11, 2016 08:17
-
-
Save rikschennink/a6bd4bf02d6e72fdcaf2c73c2ad525d1 to your computer and use it in GitHub Desktop.
Function for doing quick animations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var animate = (function(){ | |
'use strict'; | |
var PI = Math.PI; | |
var PI_HALF = Math.PI/2; | |
var deltas = { | |
linear:function(p) {return p;}, | |
easeInSine:function(p) {return Math.cos(p * PI_HALF);}, | |
easeOutSine:function(p) {return Math.sin(p * PI_HALF);}, | |
easeInOutSine:function(p) {return Math.cos(p * PI) - 1;} | |
}; | |
return function (options, step, complete) { | |
var start = 0; | |
var delta = deltas[options.ease || 'linear']; | |
var duration = options.duration; | |
function draw(now) { | |
if (!start) { | |
start = now; | |
} | |
var t = now - start; | |
var progress = t / duration; | |
if (progress > 1) {progress = 1;} | |
step(delta(progress)); | |
if (progress < 1) { | |
window.requestAnimationFrame(draw); | |
} | |
else if (complete) { | |
complete(); | |
} | |
} | |
window.requestAnimationFrame(draw); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment