-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
browser.js
executable file
·59 lines (48 loc) · 1.64 KB
/
browser.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
// @flow strict
import window from './window';
import type { Cancelable } from '../types/cancelable';
const now = window.performance && window.performance.now ?
window.performance.now.bind(window.performance) :
Date.now.bind(Date);
const raf = window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame;
const cancel = window.cancelAnimationFrame ||
window.mozCancelAnimationFrame ||
window.webkitCancelAnimationFrame ||
window.msCancelAnimationFrame;
let linkEl;
/**
* @private
*/
const exported = {
/**
* Provides a function that outputs milliseconds: either performance.now()
* or a fallback to Date.now()
*/
now,
frame(fn: () => void): Cancelable {
const frame = raf(fn);
return { cancel: () => cancel(frame) };
},
getImageData(img: CanvasImageSource): ImageData {
const canvas = window.document.createElement('canvas');
const context = canvas.getContext('2d');
if (!context) {
throw new Error('failed to create canvas 2d context');
}
canvas.width = img.width;
canvas.height = img.height;
context.drawImage(img, 0, 0, img.width, img.height);
return context.getImageData(0, 0, img.width, img.height);
},
resolveURL(path: string) {
if (!linkEl) linkEl = window.document.createElement('a');
linkEl.href = path;
return linkEl.href;
},
hardwareConcurrency: window.navigator.hardwareConcurrency || 4,
get devicePixelRatio() { return window.devicePixelRatio; }
};
export default exported;