diff --git a/src/openseadragon.js b/src/openseadragon.js index ba9e4264..6ed7c3af 100644 --- a/src/openseadragon.js +++ b/src/openseadragon.js @@ -1651,33 +1651,31 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){ }; - // Adding support for HTML5's requestAnimationFrame as suggested by acdha - // implementation taken from matt synders post here:s + // Adding support for HTML5's requestAnimationFrame as suggested by acdha. + // Implementation taken from matt synder's post here: // http://mattsnider.com/cross-browser-and-legacy-supported-requestframeanimation/ (function( w ) { // most browsers have an implementation - w.requestAnimationFrame = w.requestAnimationFrame || + var requestAnimationFrame = w.requestAnimationFrame || w.mozRequestAnimationFrame || w.webkitRequestAnimationFrame || w.msRequestAnimationFrame; - w.cancelAnimationFrame = w.cancelAnimationFrame || + var 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 ); + if ( requestAnimationFrame && cancelAnimationFrame ) { + // We can't assign these window methods directly to $ because they + // expect their "this" to be "window", so we call them in wrappers. + $.requestAnimationFrame = function(){ + return requestAnimationFrame.apply( w, arguments ); }; - $.cancelAnimationFrame = function( requestId ){ - return w.cancelAnimationFrame( requestId ); + $.cancelAnimationFrame = function(){ + return cancelAnimationFrame.apply( w, arguments ); }; } else { var aAnimQueue = [], diff --git a/test/utils.js b/test/utils.js index 3b7d6c49..7489f03b 100644 --- a/test/utils.js +++ b/test/utils.js @@ -62,4 +62,30 @@ }); }); + // ---------- + asyncTest("requestAnimationFrame", function() { + var timeWatcher = Util.timeWatcher(); + + OpenSeadragon.requestAnimationFrame(function() { + ok(true, 'frame fired'); + timeWatcher.done(); + }); + }); + + // ---------- + asyncTest("cancelAnimationFrame", function() { + var frameFired = false; + + setTimeout(function() { + strictEqual(frameFired, false, 'the frame never fired'); + start(); + }, 150); + + var frameId = OpenSeadragon.requestAnimationFrame(function() { + frameFired = true; + }); + + OpenSeadragon.cancelAnimationFrame(frameId); + }); + })();