Skip to content

Instantly share code, notes, and snippets.

@rikschennink
Created April 11, 2016 08:17
Show Gist options
  • Save rikschennink/a6bd4bf02d6e72fdcaf2c73c2ad525d1 to your computer and use it in GitHub Desktop.
Save rikschennink/a6bd4bf02d6e72fdcaf2c73c2ad525d1 to your computer and use it in GitHub Desktop.
Function for doing quick animations
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