Created
October 28, 2019 07:15
-
-
Save DimaCrafter/9892916662b0f55a99f3af6f86c34ee4 to your computer and use it in GitHub Desktop.
[JS] Universal animation function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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