Skip to content

Instantly share code, notes, and snippets.

@kunukn
Last active January 23, 2018 13:41
Show Gist options
  • Select an option

  • Save kunukn/122117964bd33ed5f832206f23a9000c to your computer and use it in GitHub Desktop.

Select an option

Save kunukn/122117964bd33ed5f832206f23a9000c to your computer and use it in GitHub Desktop.
Tween
(() => {
const root = typeof window !== 'undefined' ? window : global;
const rAF = root.requestAnimationFrame
? root.requestAnimationFrame.bind(root)
: callback => root.setTimeout(callback, 16);
const getNow = () => new Date().getTime();
const tween = ({ duration = 300, update, complete } = {}) => {
const play = () => {
if (duration <= 0) return;
const now = getNow();
const elapsedTime = Math.min(duration, now - startTime);
if (elapsedTime >= duration) {
update && update(1);
complete && complete({ startTime, now, elapsedTime });
} else {
const range = elapsedTime / duration;
update && update(range);
rAF(play);
}
};
const startTime = getNow();
play();
};
tween.version = "0.0.2";
root.tween = tween;
})();
// const l = console.log.bind(console);
// tween({ duration: 500, update: l, complete: l });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment