Created
July 7, 2011 05:53
-
-
Save gtzilla/1068957 to your computer and use it in GitHub Desktop.
Just a call queue based around the webkit animation frame
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
/* | |
* SeriesQueue | |
* author: gregory tomlinson | |
* */ | |
(function(window, undefined) { | |
// If a method returns true, the motion queue continues animation | |
// by default, functions return null, meaning empty funcs | |
// will not animate, instead, they will execute once, | |
// incrementing and move to the next item in the Queue | |
// | |
var animation, | |
sQ = function() { | |
var lst=[], idx=0, | |
allow_run=true, paused=false, | |
all_complete_func; | |
return { | |
// func - method to execute (return true to animate) | |
// data - passed to func | |
// callback - executed when final iteration of lst item | |
// scope - Excution context for func (1st arg) | |
add : function( func, data, callback, scope ) { | |
lst.push({ | |
method : func, | |
complete : callback || function() {}, | |
"data" : data, | |
"scope" : scope || this | |
}); | |
}, | |
// Simple Add -- no callback required, scope optional | |
sAdd : function( func, data, scope ) { | |
this.add( func, data, null, scope || window ); | |
}, | |
// Append an object to stack | |
// obj should be of the form | |
// { | |
// method : function(){}, | |
// data : {} || null, | |
// complete : function() {}, | |
// scope : obj // execution scope | |
// } | |
append : function(obj) { | |
if(!obj) { return; } | |
if(!obj.method) { | |
obj.method=function() {} | |
} | |
lst.push(obj); | |
}, | |
// Stops Entire Queue, Queue will reset to one | |
end : function() { | |
allow_run=false; | |
}, | |
// pause a queue, send queue.pause(false); to restart | |
pause : function( bool ) { | |
paused=bool || false; | |
}, | |
// Get access to the Queueu | |
dump : function() { | |
return lst; | |
}, | |
startAt : function(pos) { | |
idx=pos && pos > -1 && pos || 0; | |
allow_run=true; | |
this.run(); | |
}, | |
// optional TimeStamp (typically used w/ requestAnimationFrame | |
run : function(_ts) { | |
var item=lst[idx], result, | |
self=this, | |
ts=_ts || (new Date()).getTime(); | |
if(paused) return; // do not reset a pause | |
if(!item || !allow_run) { | |
allow_run=true; | |
idx=0; // reset counter, prep for next run | |
return; | |
} | |
_incr_item( item.data ); // JS objects are pass by ref | |
result = item.method && item.method.call( item.scope || self, item.data, ts, item.data&&item.data.idx ); | |
if(!result) { | |
item.complete && item.complete.apply(item.scope); | |
if(item.data&&item.data.idx) { delete item.data.idx; } | |
idx+=1; | |
self.run(); | |
} else { | |
// run again! | |
animation(function(curr_time) { | |
self.run(curr_time); | |
}); | |
} | |
} | |
} | |
function _incr_item(data) { | |
if(data && data.idx > -1) { | |
data.idx+=1; | |
} else if(data && !data.idx) { | |
data.idx=0; | |
} | |
return data; | |
} | |
return this; | |
} | |
function set_animation() { | |
animation=(function(){ | |
return window.requestAnimationFrame || | |
window.webkitRequestAnimationFrame || | |
window.mozRequestAnimationFrame || | |
window.oRequestAnimationFrame || | |
window.msRequestAnimationFrame || | |
function(callback){ | |
setTimeout(function() { | |
callback(new Date().getTime()); | |
}, 1000 / 60); | |
}; | |
})(); | |
} | |
set_animation(); | |
window.SeriesQueue=sQ; | |
})(window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment