Created
January 10, 2018 09:10
-
-
Save kunukn/a47dc9425fae832b2027370c9112d1b7 to your computer and use it in GitHub Desktop.
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 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