-
Notifications
You must be signed in to change notification settings - Fork 0
/
ng-animator.js
134 lines (110 loc) · 3.19 KB
/
ng-animator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* ngAnimator v1.0.0
* Paradigm of 'central application processor' with requestAnimationFrame technique for AngularJS
* by Vasiliy Os <[email protected]>
*/
;(function (angular) {
'use strict';
angular.module('ngAnimator', []);
/**
* ngAnimator service using requestAnimationFrame API
* for call tasks from pool.
*/
angular.module('ngAnimator')
.factory('ngAnimator', function () {
// requestAnimationFrame polyfill.
// Source: https://gist.github.com/paulirish/1579671.
var vendors = ['ms', 'moz', 'webkit', 'o'],
RAF = window.requestAnimationFrame,
cRAF = window.cancelAnimationFrame;
for (var x = 0; x < vendors.length && !RAF; ++x) {
RAF = window[vendors[x]+'RequestAnimationFrame'];
cRAF = window[vendors[x]+'CancelAnimationFrame'] ||
window[vendors[x]+'CancelRequestAnimationFrame'];
}
// Fallback to setTimeout.
if (!RAF) {
var matMax = Math.max,
setTimeout = window.setTimeout,
lastTime, currTime, timeToCall, tid;
RAF = function(callback) {
currTime = new Date().getTime();
timeToCall = matMax(0, 16 - (currTime - lastTime));
tid = setTimeout(function() {
callback(currTime + timeToCall);
}, timeToCall);
lastTime = currTime + timeToCall;
return tid;
};
}
// Fallback to clearTimeout.
if (!cRAF) {
var clearTimeout = window.clearTimeout;
cRAF = function (tid) {
return clearTimeout(tid);
};
}
// ngAnimator instance definition..
var ngAnimator = {
requestAnimationFrame: RAF,
cancelAnimationFrame: cRAF
};
// Tasks pool definition.
var pool = ngAnimator.pool = [];
// Timeout id for loop stopping.
var _tid;
// Add @task function to calling loop.
ngAnimator.add = function (task) {
pool.unshift(task);
if (!_tid) {
ngAnimator.start();
}
return ngAnimator;
};
// Remove @task function from calling loop.
ngAnimator.remove = function (task) {
var index;
while ((index = pool.indexOf(task)) >= 0) {
pool.splice(index, 1)[0] = null;
}
if (!pool.length) {
ngAnimator.stop();
}
return ngAnimator;
};
// Check for @task function exists in pool and pool is runned.
ngAnimator.isRunned = function (task) {
return !!_tid && pool.indexOf(task) >= 0;
};
// Start animation loop.
ngAnimator.start = function () {
var _pool, len;
ngAnimator.stop();
_tid = RAF(function ngAnimatorLoop () {
_tid = RAF(ngAnimatorLoop);
len = pool.length;
if (!len) {
return;
}
_pool = pool.slice();
for ( ; len--; ) {
_pool[len]();
}
});
return ngAnimator;
};
// Stop started loop.
ngAnimator.stop = function () {
cRAF(_tid);
_tid = null;
return ngAnimator;
};
// Flush all tasks from pool and stop loop.
ngAnimator.flush = function () {
pool.splice(-pool.length);
this.stop();
return ngAnimator;
};
return ngAnimator;
});
} (angular));