Skip to content

Instantly share code, notes, and snippets.

@kunukn
Created January 10, 2018 09:10
Show Gist options
  • Select an option

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

Select an option

Save kunukn/a47dc9425fae832b2027370c9112d1b7 to your computer and use it in GitHub Desktop.
(() => {
const rAF = window.requestAnimationFrame
? window.requestAnimationFrame.bind(window)
: callback => window.setTimeout(callback, 16);
const getNow = () => new Date().getTime();
const tween = ({ duration = 1000 } = {}) => {
const start = ({ 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();
};
return {
start
};
};
tween.version = "0.0.1";
window.simpleTween = tween;
})();
// const log = console.log.bind(console);
// window.simpleTween({ duration: 500 }).start({ update: log, complete: log });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment