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