Skip to content

Instantly share code, notes, and snippets.

@birkestroem
Last active August 29, 2015 14:06
Show Gist options
  • Save birkestroem/6819db65c404efaf5f4d to your computer and use it in GitHub Desktop.
Save birkestroem/6819db65c404efaf5f4d to your computer and use it in GitHub Desktop.
define(function () {
function Gomo(options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
var duration = options.duration || 500;
var easing = this.easing[options.easing || 'easeInOut'];
var start = null;
var finish = null;
var that = this;
var isRunning = this.isRunning = false;
function step(timestamp) {
if (start === null) {
start = timestamp;
finish = start + duration;
isRunning = true;
}
var elapsed = timestamp - start;
var fraction = timestamp > finish ? 1 : elapsed / duration;
callback(easing(fraction, elapsed, duration), that);
if (fraction < 1) {
that.requestId = requestAnimationFrame(step);
} else {
isRunning = false;
if (options.finish) {
options.finish();
}
}
}
this.requestId = requestAnimationFrame(step);
}
Gomo.prototype.stop = function() {
if (this.requestId) {
cancelAnimationFrame(this.requestId);
}
};
Gomo.prototype.easing = {
// fraction, elapsed, duration
'linear': function(x) { return x; },
'easeInOut': function(x) { return (-Math.cos(x * Math.PI) / 2) + 0.5; },
'easeOutBounce': function (x, t, d) {
if ((t/=d) < (1/2.75)) {
return (7.5625*t*t);
} else if (t < (2/2.75)) {
return (7.5625*(t-=(1.5/2.75))*t + 0.75);
} else if (t < (2.5/2.75)) {
return (7.5625*(t-=(2.25/2.75))*t + 0.9375);
} else {
return (7.5625*(t-=(2.625/2.75))*t + 0.984375);
}
}
};
return Gomo;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment