Last active
January 23, 2018 13:41
-
-
Save kunukn/122117964bd33ed5f832206f23a9000c to your computer and use it in GitHub Desktop.
Tween
This file contains hidden or 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
| (() => { | |
| 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