2017-02-08 14:44:43 +08:00
|
|
|
const availablePrefixs = ['moz', 'ms', 'webkit'];
|
|
|
|
|
2017-01-26 11:23:54 +08:00
|
|
|
function requestAnimationFramePolyfill() {
|
|
|
|
let lastTime = 0;
|
2017-11-22 12:06:49 +08:00
|
|
|
return function(callback: (n: number) => void) {
|
2017-01-26 11:23:54 +08:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-10-13 16:26:15 +08:00
|
|
|
export default function getRequestAnimationFrame() {
|
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
return () => {};
|
|
|
|
}
|
|
|
|
if (window.requestAnimationFrame) {
|
2017-08-03 10:58:55 +08:00
|
|
|
// https://github.com/vuejs/vue/issues/4465
|
|
|
|
return window.requestAnimationFrame.bind(window);
|
2016-10-13 16:26:15 +08:00
|
|
|
}
|
2017-02-08 14:44:43 +08:00
|
|
|
|
|
|
|
const prefix = availablePrefixs.filter(key => `${key}RequestAnimationFrame` in window)[0];
|
|
|
|
|
2016-10-13 16:26:15 +08:00
|
|
|
return prefix
|
2017-11-22 12:06:49 +08:00
|
|
|
? (window as any)[`${prefix}RequestAnimationFrame`]
|
2017-01-26 11:23:54 +08:00
|
|
|
: requestAnimationFramePolyfill();
|
2016-10-13 16:26:15 +08:00
|
|
|
}
|
2017-02-08 14:44:43 +08:00
|
|
|
|
2017-11-22 12:06:49 +08:00
|
|
|
export function cancelRequestAnimationFrame(id: number) {
|
2017-02-08 14:44:43 +08:00
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (window.cancelAnimationFrame) {
|
|
|
|
return window.cancelAnimationFrame(id);
|
|
|
|
}
|
|
|
|
const prefix = availablePrefixs.filter(key =>
|
2017-03-23 21:15:49 +08:00
|
|
|
`${key}CancelAnimationFrame` in window || `${key}CancelRequestAnimationFrame` in window,
|
2017-02-08 14:44:43 +08:00
|
|
|
)[0];
|
|
|
|
|
|
|
|
return prefix ?
|
2017-11-22 12:06:49 +08:00
|
|
|
(
|
|
|
|
(window as any)[`${prefix}CancelAnimationFrame`] ||
|
|
|
|
(window as any)[`${prefix}CancelRequestAnimationFrame`]
|
|
|
|
).call(this, id) : clearTimeout(id);
|
2017-02-08 14:44:43 +08:00
|
|
|
}
|