Skip to content

Commit fd437f7

Browse files
committed
Add requestAnimationFrame polyfill util
1 parent 5e2d9e2 commit fd437f7

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/util.js

+29
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,32 @@ export function onlyChild(children) {
1313
export function filterNullChildren(children) {
1414
return children && children.filter(i => i !== null);
1515
}
16+
17+
export const requestAnimationFrame = (() => {
18+
let raf;
19+
20+
if (typeof window !== 'undefined') {
21+
raf = window.requestAnimationFrame;
22+
const prefixes = ['ms', 'moz', 'webkit', 'o'];
23+
for (let i = 0; i < prefixes.length; i++) {
24+
if (raf) break;
25+
raf = window[`${prefixes[i]}RequestAnimationFrame}`];
26+
}
27+
}
28+
29+
if (!raf) {
30+
let timeLast = 0;
31+
raf = (callback) => {
32+
const timeCurrent = new Date().getTime();
33+
34+
/* Dynamically set the delay on a per-tick basis to more closely match 60fps. */
35+
/* Technique by Erik Moller. MIT license: https://gist.github.com/paulirish/1579671. */
36+
const timeDelta = Math.max(0, 16 - (timeCurrent - timeLast));
37+
timeLast = timeCurrent + timeDelta;
38+
39+
return setTimeout(() => { callback(timeCurrent + timeDelta); }, timeDelta);
40+
};
41+
}
42+
43+
return raf;
44+
})();

0 commit comments

Comments
 (0)