Created
June 13, 2013 15:50
-
-
Save ezhlobo/5774814 to your computer and use it in GitHub Desktop.
Animate with hata framework
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
/* | |
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