/
Support.ts
155 lines (138 loc) · 3.8 KB
/
Support.ts
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
export function isBrowser(): boolean {
return (
typeof window !== "undefined" &&
typeof document !== "undefined"
);
}
export function isArraySupported(): boolean {
return !!(
Array.prototype &&
Array.prototype.concat &&
Array.prototype.filter &&
Array.prototype.includes &&
Array.prototype.indexOf &&
Array.prototype.join &&
Array.prototype.map &&
Array.prototype.push &&
Array.prototype.pop &&
Array.prototype.reverse &&
Array.prototype.shift &&
Array.prototype.slice &&
Array.prototype.splice &&
Array.prototype.sort &&
Array.prototype.unshift
);
}
export function isBlobSupported(): boolean {
return (
"Blob" in window &&
"URL" in window
);
}
export function isFunctionSupported(): boolean {
return !!(
Function.prototype &&
Function.prototype.apply &&
Function.prototype.bind
);
}
export function isJSONSupported(): boolean {
return (
"JSON" in window &&
"parse" in JSON &&
"stringify" in JSON
);
}
export function isMapSupported(): boolean {
return "Map" in window;
}
export function isObjectSupported(): boolean {
return !!(
Object.assign &&
Object.keys &&
Object.values
);
}
export function isPromiseSupported(): boolean {
return !!(
"Promise" in window &&
Promise.resolve &&
Promise.reject &&
Promise.prototype &&
Promise.prototype.catch &&
Promise.prototype.then
);
}
export function isSetSupported(): boolean {
return "Set" in window;
}
let isWebGLSupportedCache: boolean = undefined;
export function isWebGLSupportedCached(): boolean {
if (isWebGLSupportedCache === undefined) {
isWebGLSupportedCache = isWebGLSupported();
}
return isWebGLSupportedCache;
}
export function isWebGLSupported(): boolean {
const attributes: WebGLContextAttributes = {
alpha: false,
antialias: false,
depth: true,
failIfMajorPerformanceCaveat: false,
premultipliedAlpha: true,
preserveDrawingBuffer: false,
stencil: true,
};
const canvas = document.createElement("canvas");
const webGL2Context = canvas.getContext("webgl2", attributes);
if (!!webGL2Context) { return true; }
const context =
canvas.getContext("webgl", attributes) ||
<WebGLRenderingContext>canvas
.getContext("experimental-webgl", attributes);
if (!context) { return false; }
const requiredExtensions = ["OES_standard_derivatives"];
const supportedExtensions = context.getSupportedExtensions();
for (const requiredExtension of requiredExtensions) {
if (supportedExtensions.indexOf(requiredExtension) === -1) {
return false;
}
}
return true;
}
/**
* Test whether the current browser supports the full
* functionality of MapillaryJS.
*
* @description The full functionality includes WebGL rendering.
*
* @return {boolean}
*
* @example `var supported = isSupported();`
*/
export function isSupported(): boolean {
return isFallbackSupported() &&
isWebGLSupportedCached();
}
/**
* Test whether the current browser supports the fallback
* functionality of MapillaryJS.
*
* @description The fallback functionality does not include WebGL
* rendering, only 2D canvas rendering.
*
* @return {boolean}
*
* @example `var fallbackSupported = isFallbackSupported();`
*/
export function isFallbackSupported(): boolean {
return isBrowser() &&
isArraySupported() &&
isBlobSupported() &&
isFunctionSupported() &&
isJSONSupported() &&
isMapSupported() &&
isObjectSupported() &&
isPromiseSupported() &&
isSetSupported();
}