-
Notifications
You must be signed in to change notification settings - Fork 31
/
AnimationFrame.js
247 lines (208 loc) · 7.22 KB
/
AnimationFrame.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.AnimationFrame = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
/**
* An even better animation frame.
*
* @copyright Oleg Slobodskoi 2015
* @website https://github.com/kof/animationFrame
* @license MIT
*/
module.exports = require('./lib/animation-frame')
},{"./lib/animation-frame":2}],2:[function(require,module,exports){
'use strict'
var nativeImpl = require('./native')
var now = require('./now')
var performance = require('./performance')
var root = require('./root')
// Weird native implementation doesn't work if context is defined.
var nativeRequest = nativeImpl.request
var nativeCancel = nativeImpl.cancel
/**
* Animation frame constructor.
*
* Options:
* - `useNative` use the native animation frame if possible, defaults to true
* - `frameRate` pass a custom frame rate
*
* @param {Object|Number} options
*/
function AnimationFrame(options) {
if (!(this instanceof AnimationFrame)) return new AnimationFrame(options)
options || (options = {})
// Its a frame rate.
if (typeof options == 'number') options = {frameRate: options}
options.useNative != null || (options.useNative = true)
this.options = options
this.frameRate = options.frameRate || AnimationFrame.FRAME_RATE
this._frameLength = 1000 / this.frameRate
this._isCustomFrameRate = this.frameRate !== AnimationFrame.FRAME_RATE
this._timeoutId = null
this._callbacks = {}
this._lastTickTime = 0
this._tickCounter = 0
}
module.exports = AnimationFrame
/**
* Default frame rate used for shim implementation. Native implementation
* will use the screen frame rate, but js have no way to detect it.
*
* If you know your target device, define it manually.
*
* @type {Number}
* @api public
*/
AnimationFrame.FRAME_RATE = 60
/**
* Replace the globally defined implementation or define it globally.
*
* @param {Object|Number} [options]
* @api public
*/
AnimationFrame.shim = function(options) {
var animationFrame = new AnimationFrame(options)
root.requestAnimationFrame = function(callback) {
return animationFrame.request(callback)
}
root.cancelAnimationFrame = function(id) {
return animationFrame.cancel(id)
}
return animationFrame
}
/**
* Request animation frame.
* We will use the native RAF as soon as we know it does works.
*
* @param {Function} callback
* @return {Number} timeout id or requested animation frame id
* @api public
*/
AnimationFrame.prototype.request = function(callback) {
var self = this
// Alawys inc counter to ensure it never has a conflict with the native counter.
// After the feature test phase we don't know exactly which implementation has been used.
// Therefore on #cancel we do it for both.
++this._tickCounter
if (nativeImpl.supported && this.options.useNative && !this._isCustomFrameRate) {
return nativeRequest(callback)
}
if (!callback) throw new TypeError('Not enough arguments')
if (this._timeoutId == null) {
// Much faster than Math.max
// http://jsperf.com/math-max-vs-comparison/3
// http://jsperf.com/date-now-vs-date-gettime/11
var delay = this._frameLength + this._lastTickTime - now()
if (delay < 0) delay = 0
this._timeoutId = setTimeout(function() {
self._lastTickTime = now()
self._timeoutId = null
++self._tickCounter
var callbacks = self._callbacks
self._callbacks = {}
for (var id in callbacks) {
if (callbacks[id]) {
if (nativeImpl.supported && self.options.useNative) {
nativeRequest(callbacks[id])
} else {
callbacks[id](performance.now())
}
}
}
}, delay)
}
this._callbacks[this._tickCounter] = callback
return this._tickCounter
}
/**
* Cancel animation frame.
*
* @param {Number} timeout id or requested animation frame id
*
* @api public
*/
AnimationFrame.prototype.cancel = function(id) {
if (nativeImpl.supported && this.options.useNative) nativeCancel(id)
delete this._callbacks[id]
}
},{"./native":3,"./now":4,"./performance":6,"./root":7}],3:[function(require,module,exports){
'use strict'
var root = require('./root')
// Test if we are within a foreign domain. Use raf from the top if possible.
try {
// Accessing .name will throw SecurityError within a foreign domain.
root.top.name
root = root.top
} catch(e) {}
exports.request = root.requestAnimationFrame
exports.cancel = root.cancelAnimationFrame || root.cancelRequestAnimationFrame
exports.supported = false
var vendors = ['Webkit', 'Moz', 'ms', 'O']
// Grab the native implementation.
for (var i = 0; i < vendors.length && !exports.request; i++) {
exports.request = root[vendors[i] + 'RequestAnimationFrame']
exports.cancel = root[vendors[i] + 'CancelAnimationFrame'] ||
root[vendors[i] + 'CancelRequestAnimationFrame']
}
// Test if native implementation works.
// There are some issues on ios6
// http://shitwebkitdoes.tumblr.com/post/47186945856/native-requestanimationframe-broken-on-ios-6
// https://gist.github.com/KrofDrakula/5318048
if (exports.request) {
exports.request.call(null, function() {
exports.supported = true
})
}
},{"./root":7}],4:[function(require,module,exports){
'use strict'
/**
* Crossplatform Date.now()
*
* @return {Number} time in ms
* @api private
*/
module.exports = Date.now || function() {
return (new Date).getTime()
}
},{}],5:[function(require,module,exports){
'use strict'
var now = require('./now')
/**
* Replacement for PerformanceTiming.navigationStart for the case when
* performance.now is not implemented.
*
* https://developer.mozilla.org/en-US/docs/Web/API/PerformanceTiming.navigationStart
*
* @type {Number}
* @api private
*/
exports.navigationStart = now()
},{"./now":4}],6:[function(require,module,exports){
'use strict'
var now = require('./now')
var PerformanceTiming = require('./performance-timing')
var root = require('./root')
/**
* Crossplatform performance.now()
*
* https://developer.mozilla.org/en-US/docs/Web/API/Performance.now()
*
* @return {Number} relative time in ms
* @api public
*/
exports.now = function () {
if (root.performance && root.performance.now) return root.performance.now()
return now() - PerformanceTiming.navigationStart
}
},{"./now":4,"./performance-timing":5,"./root":7}],7:[function(require,module,exports){
var root
// Browser window
if (typeof window !== 'undefined') {
root = window
// Web Worker
} else if (typeof self !== 'undefined') {
root = self
// Other environments
} else {
root = this
}
module.exports = root
},{}]},{},[1])(1)
});