Skip to content

Instantly share code, notes, and snippets.

@DimaCrafter
Created October 28, 2019 07:15
Show Gist options
  • Save DimaCrafter/9892916662b0f55a99f3af6f86c34ee4 to your computer and use it in GitHub Desktop.
Save DimaCrafter/9892916662b0f55a99f3af6f86c34ee4 to your computer and use it in GitHub Desktop.
[JS] Universal animation function
// https://gist.github.com/gre/1650294
const easeOutCubic = t => (--t) * t * t + 1;
function animate (obj, prop, from, to, duration, ease) {
if (!obj) return;
const delta = to - from;
const start = Date.now();
const end = start + duration;
const interval = setInterval(() => {
const timeDelta = Date.now() - start;
if (timeDelta >= duration) return clearInterval(interval);
obj[prop] = from + delta * ease(timeDelta / duration);
}, 10);
}
// Example:
let object = {
set intVal (value) { console.log(value); }
};
animate(object, 'intVal', 400, -400, 500, easeOutCubic);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment