Last active
January 25, 2018 18:19
-
-
Save roboshoes/cf7ba9b83aca2396dcabb65f49d42249 to your computer and use it in GitHub Desktop.
Small tweening function for the quick tween.
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
export function tween( time, update ) { | |
const start = Date.now(); | |
var isCanceled = false; | |
var isComplete = false; | |
var chain = []; | |
function loop() { | |
if ( isCanceled ) return; | |
const difference = Date.now() - start; | |
const delta = difference / time; | |
if ( delta >= 1 ) { | |
isComplete = true; | |
update( 1 ); | |
chain.forEach( callback => callback() ); | |
} else { | |
update( delta ); | |
requestAnimationFrame( loop ); | |
} | |
} | |
requestAnimationFrame( loop ); | |
return { | |
cancel() { | |
isCanceled = true; | |
}, | |
after( callback ) { | |
isComplete ? callback() : chain.push( callback ); | |
}, | |
isComplete() { | |
return isComplete; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This isn't amazingly optimized at all. But it get's the job done in many many use cases I encounter a lot. Obviously no replacement for things like Greensock's GSAP and others.
But these 40 lines (many of which are white space) allow you to: