12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- let lastTime = 0
- const prefixes = 'webkit moz ms o'.split(' ')
- let requestAnimationFrame
- let cancelAnimationFrame
- let prefix
- requestAnimationFrame = window.requestAnimationFrame
- cancelAnimationFrame = window.cancelAnimationFrame
- for (let i = 0; i < prefixes.length; i++) {
- if (requestAnimationFrame && cancelAnimationFrame) {
- break
- }
- prefix = prefixes[i]
- requestAnimationFrame = requestAnimationFrame || window[prefix + 'RequestAnimationFrame']
- cancelAnimationFrame = cancelAnimationFrame || window[prefix + 'CancelAnimationFrame'] || window[prefix +
- 'CancelRequestAnimationFrame']
- }
- if (!requestAnimationFrame || !cancelAnimationFrame) {
- requestAnimationFrame = function(callback) {
- const currTime = new Date().getTime()
-
- const timeToCall = Math.max(0, 16 - (currTime - lastTime))
- const id = setTimeout(() => {
- callback(currTime + timeToCall)
- }, timeToCall)
- lastTime = currTime + timeToCall
- return id
- }
- cancelAnimationFrame = function(id) {
- clearTimeout(id)
- }
- }
- export {
- requestAnimationFrame,
- cancelAnimationFrame
- }
|