Skip to content

Commit

Permalink
Util methods for animation loops.
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaub committed Jan 3, 2012
1 parent 485ec03 commit bc8a38c
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/OpenLayers/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

})();

0 comments on commit bc8a38c

Please sign in to comment.