Last active
March 1, 2016 21:36
-
-
Save colinmeinke/028bc5128ecd5ef97109 to your computer and use it in GitHub Desktop.
Generators – good for tweening?
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
const state = { | |
position: 10, | |
}; | |
const tween = function* ({ from, to }) { | |
let frames = 60; | |
while ( from !== to ) { | |
from = yield from + ( to - from ) / frames; | |
frames--; | |
} | |
}; | |
const draw = t => { | |
const { done, value } = t.next( state.position ); | |
if ( !done ) { | |
state.position = value; | |
console.log( state.position ); | |
window.requestAnimationFrame(() => { | |
draw( t ); | |
}); | |
} | |
}; | |
const updatePosition = to => { | |
const t = tween({ from: state.position, to }); | |
window.requestAnimationFrame(() => { | |
draw( t ); | |
}); | |
} | |
updatePosition( 500 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment