Animation.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the 2-clause BSD license.
  3. * See license.txt in the OpenLayers distribution or repository for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/SingleFile.js
  7. * @requires OpenLayers/Util/vendorPrefix.js
  8. */
  9. /**
  10. * Namespace: OpenLayers.Animation
  11. * A collection of utility functions for executing methods that repaint a
  12. * portion of the browser window. These methods take advantage of the
  13. * browser's scheduled repaints where requestAnimationFrame is available.
  14. */
  15. OpenLayers.Animation = (function(window) {
  16. /**
  17. * Property: isNative
  18. * {Boolean} true if a native requestAnimationFrame function is available
  19. */
  20. var requestAnimationFrame = OpenLayers.Util.vendorPrefix.js(window, "requestAnimationFrame");
  21. var isNative = !!(requestAnimationFrame);
  22. /**
  23. * Function: requestFrame
  24. * Schedule a function to be called at the next available animation frame.
  25. * Uses the native method where available. Where requestAnimationFrame is
  26. * not available, setTimeout will be called with a 16ms delay.
  27. *
  28. * Parameters:
  29. * callback - {Function} The function to be called at the next animation frame.
  30. * element - {DOMElement} Optional element that visually bounds the animation.
  31. */
  32. var requestFrame = (function() {
  33. var request = window[requestAnimationFrame] ||
  34. function(callback, element) {
  35. window.setTimeout(callback, 16);
  36. };
  37. // bind to window to avoid illegal invocation of native function
  38. return function(callback, element) {
  39. request.apply(window, [callback, element]);
  40. };
  41. })();
  42. // private variables for animation loops
  43. var counter = 0;
  44. var loops = {};
  45. /**
  46. * Function: start
  47. * Executes a method with <requestFrame> in series for some
  48. * duration.
  49. *
  50. * Parameters:
  51. * callback - {Function} The function to be called at the next animation frame.
  52. * duration - {Number} Optional duration for the loop. If not provided, the
  53. * animation loop will execute indefinitely.
  54. * element - {DOMElement} Optional element that visually bounds the animation.
  55. *
  56. * Returns:
  57. * {Number} Identifier for the animation loop. Used to stop animations with
  58. * <stop>.
  59. */
  60. function start(callback, duration, element) {
  61. duration = duration > 0 ? duration : Number.POSITIVE_INFINITY;
  62. var id = ++counter;
  63. var start = +new Date;
  64. loops[id] = function() {
  65. if (loops[id] && +new Date - start <= duration) {
  66. callback();
  67. if (loops[id]) {
  68. requestFrame(loops[id], element);
  69. }
  70. } else {
  71. delete loops[id];
  72. }
  73. };
  74. requestFrame(loops[id], element);
  75. return id;
  76. }
  77. /**
  78. * Function: stop
  79. * Terminates an animation loop started with <start>.
  80. *
  81. * Parameters:
  82. * id - {Number} Identifier returned from <start>.
  83. */
  84. function stop(id) {
  85. delete loops[id];
  86. }
  87. return {
  88. isNative: isNative,
  89. requestFrame: requestFrame,
  90. start: start,
  91. stop: stop
  92. };
  93. })(window);