diff --git a/lib/OpenLayers/Util.js b/lib/OpenLayers/Util.js index a907e03023..71a64c06b3 100644 --- a/lib/OpenLayers/Util.js +++ b/lib/OpenLayers/Util.js @@ -1697,3 +1697,40 @@ OpenLayers.Util.getFormattedLonLat = function(coordinate, axis, dmsOption) { return str; }; +OpenLayers.Util.requestAnimationFrame = (function() { + return window.requestAnimationFrame || + window.webkitRequestAnimationFrame || + window.mozRequestAnimationFrame || + window.oRequestAnimationFrame || + window.msRequestAnimationFrame || + function(callback, element) { + window.setTimeout(callback, 16); + }; +})(); + +(function() { + + var counter = 0; + var loops = {}; + var request = OpenLayers.Util.requestAnimationFrame; + + OpenLayers.Util.loopAnimation = function(duration, callback, element) { + var id = ++counter; + var start = +new Date; + loops[id] = function() { + if (loops[id] && +new Date - start <= duration) { + callback(); + request(loops[id], element); + } else { + delete loops[id]; + } + } + request(loops[id], element); + return id; + } + + OpenLayers.Util.stopAnimation = function(id) { + delete loops[id]; + } + +})();