2019-01-12 11:33:27 +08:00
|
|
|
const availablePrefixs = ['moz', 'ms', 'webkit'];
|
2017-12-07 18:33:33 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
function requestAnimationFramePolyfill() {
|
|
|
|
let lastTime = 0;
|
|
|
|
return function(callback) {
|
|
|
|
const currTime = new Date().getTime();
|
|
|
|
const timeToCall = Math.max(0, 16 - (currTime - lastTime));
|
|
|
|
const id = window.setTimeout(function() {
|
|
|
|
callback(currTime + timeToCall);
|
|
|
|
}, timeToCall);
|
|
|
|
lastTime = currTime + timeToCall;
|
|
|
|
return id;
|
|
|
|
};
|
2017-12-07 18:33:33 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
export default function getRequestAnimationFrame() {
|
2017-12-07 18:33:33 +08:00
|
|
|
if (typeof window === 'undefined') {
|
2019-01-12 11:33:27 +08:00
|
|
|
return () => {};
|
2017-12-07 18:33:33 +08:00
|
|
|
}
|
|
|
|
if (window.requestAnimationFrame) {
|
|
|
|
// https://github.com/vuejs/vue/issues/4465
|
2019-01-12 11:33:27 +08:00
|
|
|
return window.requestAnimationFrame.bind(window);
|
2017-12-07 18:33:33 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const prefix = availablePrefixs.filter(key => `${key}RequestAnimationFrame` in window)[0];
|
2017-12-07 18:33:33 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
return prefix ? window[`${prefix}RequestAnimationFrame`] : requestAnimationFramePolyfill();
|
2017-12-07 18:33:33 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
export function cancelRequestAnimationFrame(id) {
|
2017-12-07 18:33:33 +08:00
|
|
|
if (typeof window === 'undefined') {
|
2019-01-12 11:33:27 +08:00
|
|
|
return null;
|
2017-12-07 18:33:33 +08:00
|
|
|
}
|
|
|
|
if (window.cancelAnimationFrame) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return window.cancelAnimationFrame(id);
|
2017-12-07 18:33:33 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
const prefix = availablePrefixs.filter(
|
|
|
|
key => `${key}CancelAnimationFrame` in window || `${key}CancelRequestAnimationFrame` in window,
|
|
|
|
)[0];
|
2017-12-07 18:33:33 +08:00
|
|
|
|
|
|
|
return prefix
|
2019-01-12 11:33:27 +08:00
|
|
|
? (
|
|
|
|
window[`${prefix}CancelAnimationFrame`] || window[`${prefix}CancelRequestAnimationFrame`]
|
|
|
|
).call(this, id)
|
|
|
|
: clearTimeout(id);
|
2017-12-07 18:33:33 +08:00
|
|
|
}
|