Skip to content

Instantly share code, notes, and snippets.

@colinmeinke
Last active March 1, 2016 21:36
Show Gist options
  • Save colinmeinke/028bc5128ecd5ef97109 to your computer and use it in GitHub Desktop.
Save colinmeinke/028bc5128ecd5ef97109 to your computer and use it in GitHub Desktop.
Generators – good for tweening?
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