Skip to content

Instantly share code, notes, and snippets.

@tdreyno
Created October 1, 2012 19:38
Show Gist options
  • Save tdreyno/3813974 to your computer and use it in GitHub Desktop.
Save tdreyno/3813974 to your computer and use it in GitHub Desktop.
One-off animation with rAF
function oneOffAnimation(from, to, duration, easing, onUpdate, onComplete) {
var t = new TWEEN.Tween(from)
.to(to, duration)
.easing(easing)
.onUpdate(onUpdate)
.onComplete(function _onComplete() {
t.done = true;
});
var self = this;
function tick(ts) {
if (!ts) {
ts = +(new Date());
}
t.update(ts);
if (t.done) {
onComplete();
} else {
requestAnimationFrame(tick);
}
}
t.start();
requestAnimationFrame(tick);
return t;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment