-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
290 lines (264 loc) · 8.12 KB
/
index.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Constants & Variables
// =============================================================================
const cvs = document ? document.createElement('canvas') : null;
const ctx = cvs && cvs.getContext ? cvs.getContext('2d') : null;
const defaults = {
max : null,
min : 1,
sizes: [],
step : 1024,
// Callbacks
onError : Function.prototype,
onSuccess: Function.prototype
};
const testSizes = {
area: [
// Chrome 70 (Mac, Win)
// Chrome 68 (Android 4.4)
// Edge 17 (Win)
// Safari 7-12 (Mac)
16384,
// Chrome 68 (Android 7.1-9)
14188,
// Chrome 68 (Android 5),
11402,
// Chrome 68 (Android 6)
10836,
// Firefox 63 (Mac, Win)
11180,
// IE 9-11 (Win)
8192,
// IE Mobile (Windows Phone 8.x)
// Safari (iOS 9 - 12)
4096,
// Failed
defaults.min
],
height: [
// Safari 7-12 (Mac)
// Safari (iOS 9-12)
8388607,
// Chrome 70 (Mac, Win)
// Chrome 68 (Android 4.4-9)
// Firefox 63 (Mac, Win)
32767,
// IE11
// Edge 17 (Win)
16384,
// IE 9-10 (Win)
8192,
// IE Mobile (Windows Phone 8.x)
4096,
// Failed
defaults.min
],
width: [
// Safari 7-12 (Mac)
// Safari (iOS 9-12)
4194303,
// Chrome 70 (Mac, Win)
// Chrome 68 (Android 4.4-9)
// Firefox 63 (Mac, Win)
32767,
// IE11
// Edge 17 (Win)
16384,
// IE 9-10 (Win)
8192,
// IE Mobile (Windows Phone 8.x)
4096,
// Failed
defaults.min
]
};
// Functions (Private)
// =============================================================================
/**
* Tests ability to read pixel data from a canvas at a specified dimension.
*
* @param {number} width
* @param {number} height
* @returns {boolean}
*/
function canvasTest(width, height) {
// Define test rectangle dimensions and coordinates
const w = 1;
const h = 1;
const x = width - w; // Right edge
const y = height - h; // Bottom edge
try {
// Set sized canvas dimensions and draw test rectangle
cvs.width = width;
cvs.height = height;
ctx.fillRect(x, y, w, h);
// Verify test rectangle image data (Pass = 255, Fail = 0)
return Boolean(ctx.getImageData(x, y, w, h).data[3]);
}
catch(e){
return false;
}
}
/**
* Tests ability to read pixel data from canvas elements of various dimensions
* by decreasing canvas height and/or width until a test succeeds.
*
* @param {object} settings
* @param {number[][]} settings.sizes
* @param {function} settings.onError
* @param {function} settings.onSuccess
*/
function canvasTestLoop(settings) {
const sizes = settings.sizes.shift();
const width = sizes[0];
const height = sizes[1];
const testPass = canvasTest(width, height);
if (testPass) {
settings.onSuccess(width, height);
}
else {
settings.onError(width, height);
if (settings.sizes.length) {
setTimeout(function(){
canvasTestLoop(settings);
}, 0);
}
}
}
/**
* Creates a 2d array of canvas dimensions either from the default testSizes
* object or the width/height/min/step values provided.
*
* @param {object} settings
* @param {number} settings.width
* @param {number} settings.height
* @param {number} settings.min
* @param {number} settings.step
* @param {number[][]} settings.sizes
* @returns {number[][]}
*/
function createSizesArray(settings) {
const isArea = settings.width === settings.height;
const isWidth = settings.height === 1;
const isHeight = settings.width === 1;
const sizes = [];
// Use settings.sizes
if (!settings.width || !settings.height) {
settings.sizes.forEach(testSize => {
const width = isArea || isWidth ? testSize : 1;
const height = isArea || isHeight ? testSize : 1;
sizes.push([width, height]);
});
}
// Generate sizes from width, height, and step
else {
const testMin = settings.min || defaults.min;
const testStep = settings.step || defaults.step;
let testSize = Math.max(settings.width, settings.height);
while (testSize > testMin) {
const width = isArea || isWidth ? testSize : 1;
const height = isArea || isHeight ? testSize : 1;
sizes.push([width, height]);
testSize -= testStep;
}
sizes.push([testMin, testMin]);
}
return sizes;
}
// Methods
// =============================================================================
const canvasSize = {
/**
* Determines maximum area of an HTML canvas element. When `max` is
* unspecified, an optimized test will be performed using known maximum
* values from a variety of browsers and platforms.
*
* @param {object} [options]
* @param {number} [options.max]
* @param {number} [options.min=1]
* @param {number} [options.step=1024]
* @param {function} [options.onError]
* @param {function} [options.onSuccess]
*/
maxArea(options = {}) {
const sizes = createSizesArray({
width : options.max,
height: options.max,
min : options.min,
step : options.step,
sizes : [...testSizes.area]
});
const settings = Object.assign({}, defaults, options, { sizes });
canvasTestLoop(settings);
},
/**
* Determines maximum height of an HTML canvas element. When `max` is
* unspecified, an optimized test will be performed using known maximum
* values from a variety of browsers and platforms.
*
* @param {object} [options]
* @param {number} [options.max]
* @param {number} [options.min=1]
* @param {number} [options.step=1024]
* @param {function} [options.onError]
* @param {function} [options.onSuccess]
*/
maxHeight(options = {}) {
const sizes = createSizesArray({
width : 1,
height: options.max,
min : options.min,
step : options.step,
sizes : [...testSizes.height]
});
const settings = Object.assign({}, defaults, options, { sizes });
canvasTestLoop(settings);
},
/**
* Determines maximum width of an HTML canvas element. When `max` is
* unspecified, an optimized test will be performed using known maximum
* values from a variety of browsers and platforms.
*
* @param {object} [options]
* @param {number} [options.max]
* @param {number} [options.min=1]
* @param {number} [options.step=1024]
* @param {function} [options.onError]
* @param {function} [options.onSuccess]
*/
maxWidth(options = {}) {
const sizes = createSizesArray({
width : options.max,
height: 1,
min : options.min,
step : options.step,
sizes : [...testSizes.width]
});
const settings = Object.assign({}, defaults, options, { sizes });
canvasTestLoop(settings);
},
/**
* Tests ability to read pixel data from canvas of specified dimension(s).
*
* @param {object} [options]
* @param {number} [options.width]
* @param {number} [options.height]
* @param {number[][]} [options.sizes]
* @param {function} [options.onError]
* @param {function} [options.onSuccess]
* @returns {boolean} Returns boolean when width/heigt is set (not sizes)
*/
test(options = {}) {
const settings = Object.assign({}, defaults, options);
if (settings.sizes.length) {
settings.sizes = [...options.sizes];
canvasTestLoop(settings);
}
else {
const testPass = canvasTest(settings.width, settings.height);
return testPass;
}
}
};
// Exports
// =============================================================================
export default canvasSize;