Skip to content

Instantly share code, notes, and snippets.

@ezhlobo
Created June 13, 2013 15:50
Show Gist options
  • Save ezhlobo/5774814 to your computer and use it in GitHub Desktop.
Save ezhlobo/5774814 to your computer and use it in GitHub Desktop.
Animate with hata framework
/*
Usage:
hata.animate( stepFunction, params );
Params:
* duration - animation time
* delay - pause between repeating
* type - type of delta incrementing
* delta - increment function
* done - functin after animation
Add new type:
hata.animTypes[ "newType" ] = function( x ) {};
Change default params:
hata.animParams[ paramName ] = 100;
*/
(function( hata ) {
var methods = {
animTypes: {
normslow: function( x ) {
return Math.sin( x * Math.PI / 2 );
},
slowslow: function( x ) {
return ( 1 - Math.cos( x * Math.PI ) ) / 2;
}
},
animParams: {
duration: 200,
delay: 10,
type: "normslow",
delta: function( x ) {
return hata.animTypes[ this.type ]( x );
},
done: function() {}
},
animate: function( step, opts ) {
var timer,
start = new Date,
opts = hata.extend({}, hata.animParams, opts ),
timer = setInterval(function() {
var progress = ( new Date - start ) / opts.duration;
if ( progress > 1 ) {
progress = 1;
}
step( opts.delta( progress ) );
if ( progress === 1 ) {
clearInterval( timer );
opts.done.call( this );
}
}, opts.delay );
}
};
hata.extend( hata, methods );
}( hata ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment