Merge pull request #25 from openseadragon/requestanimationframe

Requestanimationframe
This commit is contained in:
Chris Thatcher 2013-03-01 06:11:06 -08:00
commit 3d424075b9
4 changed files with 79 additions and 14 deletions

View File

@ -242,9 +242,9 @@ $.extend( $.Button.prototype, $.EventHandler.prototype, {
function scheduleFade( button ) { function scheduleFade( button ) {
window.setTimeout(function(){ $.requestAnimationFrame(function(){
updateFade( button ); updateFade( button );
}, 20 ); });
} }
function updateFade( button ) { function updateFade( button ) {

View File

@ -1018,9 +1018,9 @@ function finishLoadingImage( image, callback, successful, jobid ){
if ( jobid ) { if ( jobid ) {
window.clearTimeout( jobid ); window.clearTimeout( jobid );
} }
window.setTimeout( function() { $.requestAnimationFrame( function() {
callback( image.src, successful ? image : null); callback( image.src, successful ? image : null);
}, 1 ); });
} }

View File

@ -500,7 +500,7 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){
controlsFadeDelay: 2000, //ZOOM/HOME/FULL/SEQUENCE controlsFadeDelay: 2000, //ZOOM/HOME/FULL/SEQUENCE
controlsFadeLength: 1500, //ZOOM/HOME/FULL/SEQUENCE controlsFadeLength: 1500, //ZOOM/HOME/FULL/SEQUENCE
mouseNavEnabled: true, //GENERAL MOUSE INTERACTIVITY mouseNavEnabled: true, //GENERAL MOUSE INTERACTIVITY
//VIEWPORT NAVIGATOR SETTINGS //VIEWPORT NAVIGATOR SETTINGS
showNavigator: true, //promoted to default in 0.9.64 showNavigator: true, //promoted to default in 0.9.64
navigatorElement: null, navigatorElement: null,
@ -1679,6 +1679,71 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){
}; };
// Adding support for HTML5's requestAnimationFrame as suggested by acdha
// implementation taken from matt synders post here:s
// http://mattsnider.com/cross-browser-and-legacy-supported-requestframeanimation/
(function( w ) {
// most browsers have an implementation
w.requestAnimationFrame = w.requestAnimationFrame ||
w.mozRequestAnimationFrame ||
w.webkitRequestAnimationFrame ||
w.msRequestAnimationFrame;
w.cancelAnimationFrame = w.cancelAnimationFrame ||
w.mozCancelAnimationFrame ||
w.webkitCancelAnimationFrame ||
w.msCancelAnimationFrame;
// polyfill, when necessary
if ( w.requestAnimationFrame ) {
//we cant assign window.requestAnimationFrame directly to $.requestAnimationFrame
//without getting Illegal Invocation errors in webkit so call in a
//wrapper
$.requestAnimationFrame = function( callback ){
return w.requestAnimationFrame( callback );
};
$.cancelAnimationFrame = function( requestId ){
return w.cancelAnimationFrame( requestId );
};
} else {
var aAnimQueue = [],
iRequestId = 0,
iIntervalId;
// create a mock requestAnimationFrame function
$.requestAnimationFrame = function( callback ) {
aAnimQueue.push( [ ++iRequestId, callback ] );
if ( !iIntervalId ) {
iIntervalId = setInterval( function() {
if ( aAnimQueue.length ) {
aAnimQueue.shift( )[ 1 ](+new Date());
} else {
// don't continue the interval, if unnecessary
clearInterval( iIntervalId );
iIntervalId = undefined;
}
}, 1000 / 50); // estimating support for 50 frames per second
}
return iRequestId;
};
// 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 ) {
if ( aAnimQueue[ i ][ 0 ] === requestId ) {
aAnimQueue.splice( i, 1 );
return;
}
}
};
}
})( window );
/** /**
* @private * @private
* @inner * @inner

View File

@ -234,9 +234,9 @@ $.Viewer = function( options ) {
); );
} }
window.setTimeout( function(){ $.requestAnimationFrame( function(){
beginControlsAutoHide( _this ); beginControlsAutoHide( _this );
}, 1 ); // initial fade out } ); // initial fade out
}; };
@ -1079,9 +1079,9 @@ function scheduleUpdate( viewer, updateFunc, prevUpdateTime ){
deltaTime; deltaTime;
if ( THIS[ viewer.hash ].animating ) { if ( THIS[ viewer.hash ].animating ) {
return window.setTimeout( function(){ return $.requestAnimationFrame( function(){
updateFunc( viewer ); updateFunc( viewer );
}, 1 ); } );
} }
currentTime = +new Date(); currentTime = +new Date();
@ -1090,17 +1090,17 @@ function scheduleUpdate( viewer, updateFunc, prevUpdateTime ){
targetTime = prevUpdateTime + 1000 / 60; targetTime = prevUpdateTime + 1000 / 60;
deltaTime = Math.max( 1, targetTime - currentTime ); deltaTime = Math.max( 1, targetTime - currentTime );
return window.setTimeout( function(){ return $.requestAnimationFrame( function(){
updateFunc( viewer ); updateFunc( viewer );
}, deltaTime ); } );
} }
//provides a sequence in the fade animation //provides a sequence in the fade animation
function scheduleControlsFade( viewer ) { function scheduleControlsFade( viewer ) {
window.setTimeout( function(){ $.requestAnimationFrame( function(){
updateControlsFade( viewer ); updateControlsFade( viewer );
}, 20); });
} }
@ -1394,7 +1394,7 @@ function endZooming() {
function scheduleZoom( viewer ) { function scheduleZoom( viewer ) {
window.setTimeout( $.delegate( viewer, doZoom ), 10 ); $.requestAnimationFrame( $.delegate( viewer, doZoom ) );
} }