Improved requestAnimationFrame polyfill

Processes all outstanding frame requests per frame, rather than one at
a time.
This commit is contained in:
Ian Gilman 2013-06-17 11:28:42 -07:00
parent ccbef38297
commit 0112f6a4ea

View File

@ -1675,6 +1675,7 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){
};
} else {
var aAnimQueue = [],
processing = [],
iRequestId = 0,
iIntervalId;
@ -1685,7 +1686,15 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){
if ( !iIntervalId ) {
iIntervalId = setInterval( function() {
if ( aAnimQueue.length ) {
aAnimQueue.shift( )[ 1 ](+new Date());
// Process all of the currently outstanding frame
// requests, but none that get added during the
// processing.
var time = +new Date();
processing = aAnimQueue;
aAnimQueue = [];
while ( processing.length ) {
processing.shift()[ 1 ]( time );
}
} else {
// don't continue the interval, if unnecessary
clearInterval( iIntervalId );
@ -1700,12 +1709,22 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){
// create a mock cancelAnimationFrame function
$.cancelAnimationFrame = function( requestId ) {
// find the request ID and remove it
for ( var i = 0, j = aAnimQueue.length; i < j; i += 1 ) {
var i, j;
for ( i = 0, j = aAnimQueue.length; i < j; i += 1 ) {
if ( aAnimQueue[ i ][ 0 ] === requestId ) {
aAnimQueue.splice( i, 1 );
return;
}
}
// If it's not in the queue, it may be in the set we're currently
// processing.
for ( i = 0, j = processing.length; i < j; i += 1 ) {
if ( processing[ i ][ 0 ] === requestId ) {
processing.splice( i, 1 );
return;
}
}
};
}
})( window );