@loaders.gl/video 4.0.0-alpha.4 → 4.0.0-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/dist/bundle.d.ts +2 -0
  2. package/dist/bundle.d.ts.map +1 -0
  3. package/dist/bundle.js +2 -2
  4. package/dist/dist.min.js +2147 -0
  5. package/dist/es5/bundle.js +6 -0
  6. package/dist/es5/bundle.js.map +1 -0
  7. package/dist/es5/gif-builder.js +200 -0
  8. package/dist/es5/gif-builder.js.map +1 -0
  9. package/dist/es5/index.js +21 -0
  10. package/dist/es5/index.js.map +1 -0
  11. package/dist/es5/lib/gifshot/gifshot-loader.js +67 -0
  12. package/dist/es5/lib/gifshot/gifshot-loader.js.map +1 -0
  13. package/dist/es5/lib/gifshot/gifshot.js +1924 -0
  14. package/dist/es5/lib/gifshot/gifshot.js.map +1 -0
  15. package/dist/es5/lib/parsers/parse-video.js +31 -0
  16. package/dist/es5/lib/parsers/parse-video.js.map +1 -0
  17. package/dist/es5/lib/utils/assert.js +12 -0
  18. package/dist/es5/lib/utils/assert.js.map +1 -0
  19. package/dist/es5/video-loader.js +28 -0
  20. package/dist/es5/video-loader.js.map +1 -0
  21. package/dist/esm/bundle.js +4 -0
  22. package/dist/esm/bundle.js.map +1 -0
  23. package/dist/esm/gif-builder.js +106 -0
  24. package/dist/esm/gif-builder.js.map +1 -0
  25. package/dist/esm/index.js +3 -0
  26. package/dist/esm/index.js.map +1 -0
  27. package/{src → dist/esm}/lib/gifshot/gifshot-loader.js +4 -8
  28. package/dist/esm/lib/gifshot/gifshot-loader.js.map +1 -0
  29. package/dist/esm/lib/gifshot/gifshot.js +1915 -0
  30. package/dist/esm/lib/gifshot/gifshot.js.map +1 -0
  31. package/dist/esm/lib/parsers/parse-video.js +7 -0
  32. package/dist/esm/lib/parsers/parse-video.js.map +1 -0
  33. package/{src → dist/esm}/lib/utils/assert.js +1 -0
  34. package/dist/esm/lib/utils/assert.js.map +1 -0
  35. package/dist/esm/video-loader.js +19 -0
  36. package/dist/esm/video-loader.js.map +1 -0
  37. package/dist/gif-builder.d.ts +53 -0
  38. package/dist/gif-builder.d.ts.map +1 -0
  39. package/dist/gif-builder.js +136 -114
  40. package/dist/index.d.ts +3 -0
  41. package/dist/index.d.ts.map +1 -0
  42. package/dist/index.js +10 -3
  43. package/dist/lib/gifshot/gifshot-loader.d.ts +2 -0
  44. package/dist/lib/gifshot/gifshot-loader.d.ts.map +1 -0
  45. package/dist/lib/gifshot/gifshot-loader.js +18 -15
  46. package/dist/lib/gifshot/gifshot.d.ts +72 -0
  47. package/dist/lib/gifshot/gifshot.d.ts.map +1 -0
  48. package/dist/lib/gifshot/gifshot.js +2439 -0
  49. package/dist/lib/parsers/parse-video.d.ts +2 -0
  50. package/dist/lib/parsers/parse-video.d.ts.map +1 -0
  51. package/dist/lib/parsers/parse-video.js +12 -6
  52. package/dist/lib/utils/assert.d.ts +2 -0
  53. package/dist/lib/utils/assert.d.ts.map +1 -0
  54. package/dist/lib/utils/assert.js +8 -5
  55. package/dist/video-loader.d.ts +17 -0
  56. package/dist/video-loader.d.ts.map +1 -0
  57. package/dist/video-loader.js +22 -14
  58. package/package.json +8 -8
  59. package/src/{gif-builder.js → gif-builder.ts} +6 -6
  60. package/src/lib/gifshot/gifshot.ts +2416 -0
  61. package/dist/bundle.js.map +0 -1
  62. package/dist/gif-builder.js.map +0 -1
  63. package/dist/index.js.map +0 -1
  64. package/dist/lib/gifshot/gifshot-loader.js.map +0 -1
  65. package/dist/lib/parsers/parse-video.js.map +0 -1
  66. package/dist/lib/utils/assert.js.map +0 -1
  67. package/dist/lib/utils/globals.js +0 -16
  68. package/dist/lib/utils/globals.js.map +0 -1
  69. package/dist/libs/gifshot.js +0 -2826
  70. package/dist/video-loader.js.map +0 -1
  71. package/src/lib/utils/globals.js +0 -25
  72. package/src/libs/gifshot.js +0 -2826
@@ -0,0 +1,2147 @@
1
+ (() => {
2
+ var __defProp = Object.defineProperty;
3
+ var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
4
+ var __esm = (fn, res) => function __init() {
5
+ return fn && (res = (0, fn[Object.keys(fn)[0]])(fn = 0)), res;
6
+ };
7
+ var __commonJS = (cb, mod) => function __require() {
8
+ return mod || (0, cb[Object.keys(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
9
+ };
10
+ var __export = (target, all) => {
11
+ __markAsModule(target);
12
+ for (var name in all)
13
+ __defProp(target, name, { get: all[name], enumerable: true });
14
+ };
15
+
16
+ // src/lib/parsers/parse-video.ts
17
+ async function parseVideo(arrayBuffer) {
18
+ const blob2 = new Blob([arrayBuffer]);
19
+ const video = document.createElement("video");
20
+ video.src = URL.createObjectURL(blob2);
21
+ return video;
22
+ }
23
+ var init_parse_video = __esm({
24
+ "src/lib/parsers/parse-video.ts"() {
25
+ }
26
+ });
27
+
28
+ // src/video-loader.ts
29
+ var VERSION, EXTENSIONS, MIME_TYPES, DEFAULT_LOADER_OPTIONS, VideoLoader;
30
+ var init_video_loader = __esm({
31
+ "src/video-loader.ts"() {
32
+ init_parse_video();
33
+ VERSION = typeof __VERSION__ !== "undefined" ? __VERSION__ : "latest";
34
+ EXTENSIONS = ["mp4"];
35
+ MIME_TYPES = ["video/mp4"];
36
+ DEFAULT_LOADER_OPTIONS = {
37
+ video: {}
38
+ };
39
+ VideoLoader = {
40
+ name: "Video",
41
+ id: "video",
42
+ module: "video",
43
+ version: VERSION,
44
+ extensions: EXTENSIONS,
45
+ mimeTypes: MIME_TYPES,
46
+ parse: parseVideo,
47
+ options: DEFAULT_LOADER_OPTIONS
48
+ };
49
+ }
50
+ });
51
+
52
+ // src/lib/utils/assert.ts
53
+ function assert(condition, message) {
54
+ if (!condition) {
55
+ throw new Error(message);
56
+ }
57
+ }
58
+ var init_assert = __esm({
59
+ "src/lib/utils/assert.ts"() {
60
+ }
61
+ });
62
+
63
+ // src/lib/gifshot/gifshot.ts
64
+ function isSupported() {
65
+ return error.isValid();
66
+ }
67
+ function isWebCamGIFSupported() {
68
+ return error.isValid();
69
+ }
70
+ function isSupported$1() {
71
+ const options = {
72
+ getUserMedia: true
73
+ };
74
+ return error.isValid(options);
75
+ }
76
+ function isExistingVideoGIFSupported(codecs) {
77
+ let hasValidCodec = false;
78
+ if (utils.isArray(codecs) && codecs.length) {
79
+ utils.each(codecs, function(indece, currentCodec) {
80
+ if (utils.isSupported.videoCodecs[currentCodec]) {
81
+ hasValidCodec = true;
82
+ }
83
+ });
84
+ if (!hasValidCodec) {
85
+ return false;
86
+ }
87
+ } else if (utils.isString(codecs) && codecs.length) {
88
+ if (!utils.isSupported.videoCodecs[codecs]) {
89
+ return false;
90
+ }
91
+ }
92
+ return error.isValid({
93
+ getUserMedia: true
94
+ });
95
+ }
96
+ function NeuQuant() {
97
+ const netsize = 256;
98
+ const prime1 = 499;
99
+ const prime2 = 491;
100
+ const prime3 = 487;
101
+ const prime4 = 503;
102
+ const minpicturebytes = 3 * prime4;
103
+ const maxnetpos = netsize - 1;
104
+ const netbiasshift = 4;
105
+ const ncycles = 100;
106
+ const intbiasshift = 16;
107
+ const intbias = 1 << intbiasshift;
108
+ const gammashift = 10;
109
+ const gamma = 1 << gammashift;
110
+ const betashift = 10;
111
+ const beta = intbias >> betashift;
112
+ const betagamma = intbias << gammashift - betashift;
113
+ const initrad = netsize >> 3;
114
+ const radiusbiasshift = 6;
115
+ const radiusbias = 1 << radiusbiasshift;
116
+ const initradius = initrad * radiusbias;
117
+ const radiusdec = 30;
118
+ const alphabiasshift = 10;
119
+ const initalpha = 1 << alphabiasshift;
120
+ let alphadec;
121
+ const radbiasshift = 8;
122
+ const radbias = 1 << radbiasshift;
123
+ const alpharadbshift = alphabiasshift + radbiasshift;
124
+ const alpharadbias = 1 << alpharadbshift;
125
+ let thepicture;
126
+ let lengthcount;
127
+ let samplefac;
128
+ let network;
129
+ const netindex = [];
130
+ const bias = [];
131
+ const freq = [];
132
+ const radpower = [];
133
+ function NeuQuantConstructor(thepic, len, sample) {
134
+ let i;
135
+ let p;
136
+ thepicture = thepic;
137
+ lengthcount = len;
138
+ samplefac = sample;
139
+ network = new Array(netsize);
140
+ for (i = 0; i < netsize; i++) {
141
+ network[i] = new Array(4);
142
+ p = network[i];
143
+ p[0] = p[1] = p[2] = (i << netbiasshift + 8) / netsize | 0;
144
+ freq[i] = intbias / netsize | 0;
145
+ bias[i] = 0;
146
+ }
147
+ }
148
+ function colorMap() {
149
+ const map2 = [];
150
+ const index = new Array(netsize);
151
+ for (let i = 0; i < netsize; i++) {
152
+ index[network[i][3]] = i;
153
+ }
154
+ let k = 0;
155
+ for (let l = 0; l < netsize; l++) {
156
+ const j = index[l];
157
+ map2[k++] = network[j][0];
158
+ map2[k++] = network[j][1];
159
+ map2[k++] = network[j][2];
160
+ }
161
+ return map2;
162
+ }
163
+ function inxbuild() {
164
+ let i;
165
+ let j;
166
+ let smallpos;
167
+ let smallval;
168
+ let p;
169
+ let q;
170
+ let previouscol;
171
+ let startpos;
172
+ previouscol = 0;
173
+ startpos = 0;
174
+ for (i = 0; i < netsize; i++) {
175
+ p = network[i];
176
+ smallpos = i;
177
+ smallval = p[1];
178
+ for (j = i + 1; j < netsize; j++) {
179
+ q = network[j];
180
+ if (q[1] < smallval) {
181
+ smallpos = j;
182
+ smallval = q[1];
183
+ }
184
+ }
185
+ q = network[smallpos];
186
+ if (i != smallpos) {
187
+ j = q[0];
188
+ q[0] = p[0];
189
+ p[0] = j;
190
+ j = q[1];
191
+ q[1] = p[1];
192
+ p[1] = j;
193
+ j = q[2];
194
+ q[2] = p[2];
195
+ p[2] = j;
196
+ j = q[3];
197
+ q[3] = p[3];
198
+ p[3] = j;
199
+ }
200
+ if (smallval != previouscol) {
201
+ netindex[previouscol] = startpos + i >> 1;
202
+ for (j = previouscol + 1; j < smallval; j++) {
203
+ netindex[j] = i;
204
+ }
205
+ previouscol = smallval;
206
+ startpos = i;
207
+ }
208
+ }
209
+ netindex[previouscol] = startpos + maxnetpos >> 1;
210
+ for (j = previouscol + 1; j < 256; j++) {
211
+ netindex[j] = maxnetpos;
212
+ }
213
+ }
214
+ function learn() {
215
+ let i;
216
+ let j;
217
+ let b;
218
+ let g;
219
+ let r;
220
+ let radius;
221
+ let rad;
222
+ let alpha;
223
+ let step;
224
+ let delta;
225
+ let samplepixels;
226
+ let p;
227
+ let pix;
228
+ let lim;
229
+ if (lengthcount < minpicturebytes) {
230
+ samplefac = 1;
231
+ }
232
+ alphadec = 30 + (samplefac - 1) / 3;
233
+ p = thepicture;
234
+ pix = 0;
235
+ lim = lengthcount;
236
+ samplepixels = lengthcount / (3 * samplefac);
237
+ delta = samplepixels / ncycles | 0;
238
+ alpha = initalpha;
239
+ radius = initradius;
240
+ rad = radius >> radiusbiasshift;
241
+ if (rad <= 1) {
242
+ rad = 0;
243
+ }
244
+ for (i = 0; i < rad; i++) {
245
+ radpower[i] = alpha * ((rad * rad - i * i) * radbias / (rad * rad));
246
+ }
247
+ if (lengthcount < minpicturebytes) {
248
+ step = 3;
249
+ } else if (lengthcount % prime1 !== 0) {
250
+ step = 3 * prime1;
251
+ } else if (lengthcount % prime2 !== 0) {
252
+ step = 3 * prime2;
253
+ } else if (lengthcount % prime3 !== 0) {
254
+ step = 3 * prime3;
255
+ } else {
256
+ step = 3 * prime4;
257
+ }
258
+ i = 0;
259
+ while (i < samplepixels) {
260
+ b = (p[pix + 0] & 255) << netbiasshift;
261
+ g = (p[pix + 1] & 255) << netbiasshift;
262
+ r = (p[pix + 2] & 255) << netbiasshift;
263
+ j = contest(b, g, r);
264
+ altersingle(alpha, j, b, g, r);
265
+ if (rad !== 0) {
266
+ alterneigh(rad, j, b, g, r);
267
+ }
268
+ pix += step;
269
+ if (pix >= lim) {
270
+ pix -= lengthcount;
271
+ }
272
+ i++;
273
+ if (delta === 0) {
274
+ delta = 1;
275
+ }
276
+ if (i % delta === 0) {
277
+ alpha -= alpha / alphadec;
278
+ radius -= radius / radiusdec;
279
+ rad = radius >> radiusbiasshift;
280
+ if (rad <= 1) {
281
+ rad = 0;
282
+ }
283
+ for (j = 0; j < rad; j++) {
284
+ radpower[j] = alpha * ((rad * rad - j * j) * radbias / (rad * rad));
285
+ }
286
+ }
287
+ }
288
+ }
289
+ function map(b, g, r) {
290
+ let i;
291
+ let j;
292
+ let dist;
293
+ let a;
294
+ let bestd;
295
+ let p;
296
+ let best;
297
+ bestd = 1e3;
298
+ best = -1;
299
+ i = netindex[g];
300
+ j = i - 1;
301
+ while (i < netsize || j >= 0) {
302
+ if (i < netsize) {
303
+ p = network[i];
304
+ dist = p[1] - g;
305
+ if (dist >= bestd) {
306
+ i = netsize;
307
+ } else {
308
+ i++;
309
+ if (dist < 0) {
310
+ dist = -dist;
311
+ }
312
+ a = p[0] - b;
313
+ if (a < 0) {
314
+ a = -a;
315
+ }
316
+ dist += a;
317
+ if (dist < bestd) {
318
+ a = p[2] - r;
319
+ if (a < 0) {
320
+ a = -a;
321
+ }
322
+ dist += a;
323
+ if (dist < bestd) {
324
+ bestd = dist;
325
+ best = p[3];
326
+ }
327
+ }
328
+ }
329
+ }
330
+ if (j >= 0) {
331
+ p = network[j];
332
+ dist = g - p[1];
333
+ if (dist >= bestd) {
334
+ j = -1;
335
+ } else {
336
+ j--;
337
+ if (dist < 0) {
338
+ dist = -dist;
339
+ }
340
+ a = p[0] - b;
341
+ if (a < 0) {
342
+ a = -a;
343
+ }
344
+ dist += a;
345
+ if (dist < bestd) {
346
+ a = p[2] - r;
347
+ if (a < 0) {
348
+ a = -a;
349
+ }
350
+ dist += a;
351
+ if (dist < bestd) {
352
+ bestd = dist;
353
+ best = p[3];
354
+ }
355
+ }
356
+ }
357
+ }
358
+ }
359
+ return best;
360
+ }
361
+ function process() {
362
+ learn();
363
+ unbiasnet();
364
+ inxbuild();
365
+ return colorMap();
366
+ }
367
+ function unbiasnet() {
368
+ let i;
369
+ let j;
370
+ for (i = 0; i < netsize; i++) {
371
+ network[i][0] >>= netbiasshift;
372
+ network[i][1] >>= netbiasshift;
373
+ network[i][2] >>= netbiasshift;
374
+ network[i][3] = i;
375
+ }
376
+ }
377
+ function alterneigh(rad, i, b, g, r) {
378
+ let j;
379
+ let k;
380
+ let lo;
381
+ let hi;
382
+ let a;
383
+ let m;
384
+ let p;
385
+ lo = i - rad;
386
+ if (lo < -1) {
387
+ lo = -1;
388
+ }
389
+ hi = i + rad;
390
+ if (hi > netsize) {
391
+ hi = netsize;
392
+ }
393
+ j = i + 1;
394
+ k = i - 1;
395
+ m = 1;
396
+ while (j < hi || k > lo) {
397
+ a = radpower[m++];
398
+ if (j < hi) {
399
+ p = network[j++];
400
+ try {
401
+ p[0] -= a * (p[0] - b) / alpharadbias | 0;
402
+ p[1] -= a * (p[1] - g) / alpharadbias | 0;
403
+ p[2] -= a * (p[2] - r) / alpharadbias | 0;
404
+ } catch (e) {
405
+ }
406
+ }
407
+ if (k > lo) {
408
+ p = network[k--];
409
+ try {
410
+ p[0] -= a * (p[0] - b) / alpharadbias | 0;
411
+ p[1] -= a * (p[1] - g) / alpharadbias | 0;
412
+ p[2] -= a * (p[2] - r) / alpharadbias | 0;
413
+ } catch (e) {
414
+ }
415
+ }
416
+ }
417
+ }
418
+ function altersingle(alpha, i, b, g, r) {
419
+ const n = network[i];
420
+ const alphaMult = alpha / initalpha;
421
+ n[0] -= alphaMult * (n[0] - b) | 0;
422
+ n[1] -= alphaMult * (n[1] - g) | 0;
423
+ n[2] -= alphaMult * (n[2] - r) | 0;
424
+ }
425
+ function contest(b, g, r) {
426
+ let i;
427
+ let dist;
428
+ let a;
429
+ let biasdist;
430
+ let betafreq;
431
+ let bestpos;
432
+ let bestbiaspos;
433
+ let bestd;
434
+ let bestbiasd;
435
+ let n;
436
+ bestd = ~(1 << 31);
437
+ bestbiasd = bestd;
438
+ bestpos = -1;
439
+ bestbiaspos = bestpos;
440
+ for (i = 0; i < netsize; i++) {
441
+ n = network[i];
442
+ dist = n[0] - b;
443
+ if (dist < 0) {
444
+ dist = -dist;
445
+ }
446
+ a = n[1] - g;
447
+ if (a < 0) {
448
+ a = -a;
449
+ }
450
+ dist += a;
451
+ a = n[2] - r;
452
+ if (a < 0) {
453
+ a = -a;
454
+ }
455
+ dist += a;
456
+ if (dist < bestd) {
457
+ bestd = dist;
458
+ bestpos = i;
459
+ }
460
+ biasdist = dist - (bias[i] >> intbiasshift - netbiasshift);
461
+ if (biasdist < bestbiasd) {
462
+ bestbiasd = biasdist;
463
+ bestbiaspos = i;
464
+ }
465
+ betafreq = freq[i] >> betashift;
466
+ freq[i] -= betafreq;
467
+ bias[i] += betafreq << gammashift;
468
+ }
469
+ freq[bestpos] += beta;
470
+ bias[bestpos] -= betagamma;
471
+ return bestbiaspos;
472
+ }
473
+ NeuQuantConstructor.apply(this, arguments);
474
+ const exports = {};
475
+ exports.map = map;
476
+ exports.process = process;
477
+ return exports;
478
+ }
479
+ function workerCode() {
480
+ const self = this;
481
+ try {
482
+ globalThis.onmessage = function(ev) {
483
+ const data = ev.data || {};
484
+ let response;
485
+ if (data.gifshot) {
486
+ response = workerMethods.run(data);
487
+ postMessage(response);
488
+ }
489
+ };
490
+ } catch (e) {
491
+ }
492
+ var workerMethods = {
493
+ dataToRGB: function dataToRGB(data, width, height) {
494
+ const length = width * height * 4;
495
+ let i = 0;
496
+ const rgb = [];
497
+ while (i < length) {
498
+ rgb.push(data[i++]);
499
+ rgb.push(data[i++]);
500
+ rgb.push(data[i++]);
501
+ i++;
502
+ }
503
+ return rgb;
504
+ },
505
+ componentizedPaletteToArray: function componentizedPaletteToArray(paletteRGB) {
506
+ paletteRGB = paletteRGB || [];
507
+ const paletteArray = [];
508
+ for (let i = 0; i < paletteRGB.length; i += 3) {
509
+ const r = paletteRGB[i];
510
+ const g = paletteRGB[i + 1];
511
+ const b = paletteRGB[i + 2];
512
+ paletteArray.push(r << 16 | g << 8 | b);
513
+ }
514
+ return paletteArray;
515
+ },
516
+ processFrameWithQuantizer: function processFrameWithQuantizer(imageData, width, height, sampleInterval) {
517
+ const rgbComponents = this.dataToRGB(imageData, width, height);
518
+ const nq = new NeuQuant(rgbComponents, rgbComponents.length, sampleInterval);
519
+ const paletteRGB = nq.process();
520
+ const paletteArray = new Uint32Array(this.componentizedPaletteToArray(paletteRGB));
521
+ const numberPixels = width * height;
522
+ const indexedPixels = new Uint8Array(numberPixels);
523
+ let k = 0;
524
+ for (let i = 0; i < numberPixels; i++) {
525
+ const r = rgbComponents[k++];
526
+ const g = rgbComponents[k++];
527
+ const b = rgbComponents[k++];
528
+ indexedPixels[i] = nq.map(r, g, b);
529
+ }
530
+ return {
531
+ pixels: indexedPixels,
532
+ palette: paletteArray
533
+ };
534
+ },
535
+ run: function run(frame) {
536
+ frame = frame || {};
537
+ const _frame = frame;
538
+ const height = _frame.height;
539
+ const palette = _frame.palette;
540
+ const sampleInterval = _frame.sampleInterval;
541
+ const width = _frame.width;
542
+ const imageData = frame.data;
543
+ return this.processFrameWithQuantizer(imageData, width, height, sampleInterval);
544
+ }
545
+ };
546
+ return workerMethods;
547
+ }
548
+ function gifWriter(buf, width, height, gopts) {
549
+ let p = 0;
550
+ gopts = gopts === void 0 ? {} : gopts;
551
+ const loop_count = gopts.loop === void 0 ? null : gopts.loop;
552
+ const global_palette = gopts.palette === void 0 ? null : gopts.palette;
553
+ if (width <= 0 || height <= 0 || width > 65535 || height > 65535)
554
+ throw "Width/Height invalid.";
555
+ function check_palette_and_num_colors(palette) {
556
+ const num_colors = palette.length;
557
+ if (num_colors < 2 || num_colors > 256 || num_colors & num_colors - 1)
558
+ throw "Invalid code/color length, must be power of 2 and 2 .. 256.";
559
+ return num_colors;
560
+ }
561
+ buf[p++] = 71;
562
+ buf[p++] = 73;
563
+ buf[p++] = 70;
564
+ buf[p++] = 56;
565
+ buf[p++] = 57;
566
+ buf[p++] = 97;
567
+ const gp_num_colors_pow2 = 0;
568
+ const background = 0;
569
+ buf[p++] = width & 255;
570
+ buf[p++] = width >> 8 & 255;
571
+ buf[p++] = height & 255;
572
+ buf[p++] = height >> 8 & 255;
573
+ buf[p++] = (global_palette !== null ? 128 : 0) | gp_num_colors_pow2;
574
+ buf[p++] = background;
575
+ buf[p++] = 0;
576
+ if (loop_count !== null) {
577
+ if (loop_count < 0 || loop_count > 65535)
578
+ throw "Loop count invalid.";
579
+ buf[p++] = 33;
580
+ buf[p++] = 255;
581
+ buf[p++] = 11;
582
+ buf[p++] = 78;
583
+ buf[p++] = 69;
584
+ buf[p++] = 84;
585
+ buf[p++] = 83;
586
+ buf[p++] = 67;
587
+ buf[p++] = 65;
588
+ buf[p++] = 80;
589
+ buf[p++] = 69;
590
+ buf[p++] = 50;
591
+ buf[p++] = 46;
592
+ buf[p++] = 48;
593
+ buf[p++] = 3;
594
+ buf[p++] = 1;
595
+ buf[p++] = loop_count & 255;
596
+ buf[p++] = loop_count >> 8 & 255;
597
+ buf[p++] = 0;
598
+ }
599
+ let ended = false;
600
+ this.addFrame = function(x, y, w, h, indexed_pixels, opts) {
601
+ if (ended === true) {
602
+ --p;
603
+ ended = false;
604
+ }
605
+ opts = opts === void 0 ? {} : opts;
606
+ if (x < 0 || y < 0 || x > 65535 || y > 65535)
607
+ throw "x/y invalid.";
608
+ if (w <= 0 || h <= 0 || w > 65535 || h > 65535)
609
+ throw "Width/Height invalid.";
610
+ if (indexed_pixels.length < w * h)
611
+ throw "Not enough pixels for the frame size.";
612
+ let using_local_palette = true;
613
+ let palette = opts.palette;
614
+ if (palette === void 0 || palette === null) {
615
+ using_local_palette = false;
616
+ palette = global_palette;
617
+ }
618
+ if (palette === void 0 || palette === null)
619
+ throw "Must supply either a local or global palette.";
620
+ let num_colors = check_palette_and_num_colors(palette);
621
+ let min_code_size = 0;
622
+ while (num_colors >>= 1) {
623
+ ++min_code_size;
624
+ }
625
+ num_colors = 1 << min_code_size;
626
+ const delay = opts.delay === void 0 ? 0 : opts.delay;
627
+ const disposal = opts.disposal === void 0 ? 0 : opts.disposal;
628
+ if (disposal < 0 || disposal > 3)
629
+ throw "Disposal out of range.";
630
+ let use_transparency = false;
631
+ let transparent_index = 0;
632
+ if (opts.transparent !== void 0 && opts.transparent !== null) {
633
+ use_transparency = true;
634
+ transparent_index = opts.transparent;
635
+ if (transparent_index < 0 || transparent_index >= num_colors)
636
+ throw "Transparent color index.";
637
+ }
638
+ if (disposal !== 0 || use_transparency || delay !== 0) {
639
+ buf[p++] = 33;
640
+ buf[p++] = 249;
641
+ buf[p++] = 4;
642
+ buf[p++] = disposal << 2 | (use_transparency === true ? 1 : 0);
643
+ buf[p++] = delay & 255;
644
+ buf[p++] = delay >> 8 & 255;
645
+ buf[p++] = transparent_index;
646
+ buf[p++] = 0;
647
+ }
648
+ buf[p++] = 44;
649
+ buf[p++] = x & 255;
650
+ buf[p++] = x >> 8 & 255;
651
+ buf[p++] = y & 255;
652
+ buf[p++] = y >> 8 & 255;
653
+ buf[p++] = w & 255;
654
+ buf[p++] = w >> 8 & 255;
655
+ buf[p++] = h & 255;
656
+ buf[p++] = h >> 8 & 255;
657
+ buf[p++] = using_local_palette === true ? 128 | min_code_size - 1 : 0;
658
+ if (using_local_palette === true) {
659
+ for (let i = 0, il = palette.length; i < il; ++i) {
660
+ const rgb = palette[i];
661
+ buf[p++] = rgb >> 16 & 255;
662
+ buf[p++] = rgb >> 8 & 255;
663
+ buf[p++] = rgb & 255;
664
+ }
665
+ }
666
+ p = GifWriterOutputLZWCodeStream(buf, p, min_code_size < 2 ? 2 : min_code_size, indexed_pixels);
667
+ };
668
+ this.end = function() {
669
+ if (ended === false) {
670
+ buf[p++] = 59;
671
+ ended = true;
672
+ }
673
+ return p;
674
+ };
675
+ function GifWriterOutputLZWCodeStream(buf2, p2, min_code_size, index_stream) {
676
+ buf2[p2++] = min_code_size;
677
+ let cur_subblock = p2++;
678
+ const clear_code = 1 << min_code_size;
679
+ const code_mask = clear_code - 1;
680
+ const eoi_code = clear_code + 1;
681
+ let next_code = eoi_code + 1;
682
+ let cur_code_size = min_code_size + 1;
683
+ let cur_shift = 0;
684
+ let cur = 0;
685
+ function emit_bytes_to_buffer(bit_block_size) {
686
+ while (cur_shift >= bit_block_size) {
687
+ buf2[p2++] = cur & 255;
688
+ cur >>= 8;
689
+ cur_shift -= 8;
690
+ if (p2 === cur_subblock + 256) {
691
+ buf2[cur_subblock] = 255;
692
+ cur_subblock = p2++;
693
+ }
694
+ }
695
+ }
696
+ function emit_code(c) {
697
+ cur |= c << cur_shift;
698
+ cur_shift += cur_code_size;
699
+ emit_bytes_to_buffer(8);
700
+ }
701
+ let ib_code = index_stream[0] & code_mask;
702
+ let code_table = {};
703
+ emit_code(clear_code);
704
+ for (let i = 1, il = index_stream.length; i < il; ++i) {
705
+ const k = index_stream[i] & code_mask;
706
+ const cur_key = ib_code << 8 | k;
707
+ const cur_code = code_table[cur_key];
708
+ if (cur_code === void 0) {
709
+ cur |= ib_code << cur_shift;
710
+ cur_shift += cur_code_size;
711
+ while (cur_shift >= 8) {
712
+ buf2[p2++] = cur & 255;
713
+ cur >>= 8;
714
+ cur_shift -= 8;
715
+ if (p2 === cur_subblock + 256) {
716
+ buf2[cur_subblock] = 255;
717
+ cur_subblock = p2++;
718
+ }
719
+ }
720
+ if (next_code === 4096) {
721
+ emit_code(clear_code);
722
+ next_code = eoi_code + 1;
723
+ cur_code_size = min_code_size + 1;
724
+ code_table = {};
725
+ } else {
726
+ if (next_code >= 1 << cur_code_size)
727
+ ++cur_code_size;
728
+ code_table[cur_key] = next_code++;
729
+ }
730
+ ib_code = k;
731
+ } else {
732
+ ib_code = cur_code;
733
+ }
734
+ }
735
+ emit_code(ib_code);
736
+ emit_code(eoi_code);
737
+ emit_bytes_to_buffer(1);
738
+ if (cur_subblock + 1 === p2) {
739
+ buf2[cur_subblock] = 0;
740
+ } else {
741
+ buf2[cur_subblock] = p2 - cur_subblock - 1;
742
+ buf2[p2++] = 0;
743
+ }
744
+ return p2;
745
+ }
746
+ }
747
+ function getBase64GIF2(animatedGifInstance, callback) {
748
+ animatedGifInstance.getBase64GIF(function(image) {
749
+ callback({
750
+ error: false,
751
+ errorCode: "",
752
+ errorMsg: "",
753
+ image
754
+ });
755
+ });
756
+ }
757
+ function existingImages() {
758
+ const obj = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
759
+ const self = this;
760
+ const callback = obj.callback;
761
+ const images = obj.images;
762
+ const options = obj.options;
763
+ let imagesLength = obj.imagesLength;
764
+ const skipObj = {
765
+ getUserMedia: true,
766
+ "globalThis.URL": true
767
+ };
768
+ const errorObj = error.validate(skipObj);
769
+ const loadedImages = [];
770
+ let loadedImagesLength = 0;
771
+ let tempImage = void 0;
772
+ let ag = void 0;
773
+ if (errorObj.error) {
774
+ return callback(errorObj);
775
+ }
776
+ ag = new AnimatedGIF(options);
777
+ utils.each(images, function(index, image) {
778
+ const currentImage = image;
779
+ if (utils.isElement(currentImage)) {
780
+ if (options.crossOrigin) {
781
+ currentImage.crossOrigin = options.crossOrigin;
782
+ }
783
+ loadedImages[index] = currentImage;
784
+ loadedImagesLength += 1;
785
+ if (loadedImagesLength === imagesLength) {
786
+ addLoadedImagesToGif();
787
+ }
788
+ } else if (utils.isString(currentImage)) {
789
+ tempImage = new Image();
790
+ if (options.crossOrigin) {
791
+ tempImage.crossOrigin = options.crossOrigin;
792
+ }
793
+ (function(tempImage2) {
794
+ if (image.text) {
795
+ tempImage2.text = image.text;
796
+ }
797
+ tempImage2.onerror = function(e) {
798
+ let obj2 = void 0;
799
+ --imagesLength;
800
+ if (imagesLength === 0) {
801
+ obj2 = {};
802
+ obj2.error = "None of the requested images was capable of being retrieved";
803
+ return callback(obj2);
804
+ }
805
+ };
806
+ tempImage2.onload = function(e) {
807
+ if (image.text) {
808
+ loadedImages[index] = {
809
+ img: tempImage2,
810
+ text: tempImage2.text
811
+ };
812
+ } else {
813
+ loadedImages[index] = tempImage2;
814
+ }
815
+ loadedImagesLength += 1;
816
+ if (loadedImagesLength === imagesLength) {
817
+ addLoadedImagesToGif();
818
+ }
819
+ utils.removeElement(tempImage2);
820
+ };
821
+ tempImage2.src = currentImage;
822
+ })(tempImage);
823
+ utils.setCSSAttr(tempImage, {
824
+ position: "fixed",
825
+ opacity: "0"
826
+ });
827
+ document.body.appendChild(tempImage);
828
+ }
829
+ });
830
+ function addLoadedImagesToGif() {
831
+ utils.each(loadedImages, function(index, loadedImage) {
832
+ if (loadedImage) {
833
+ if (loadedImage.text) {
834
+ ag.addFrame(loadedImage.img, options, loadedImage.text);
835
+ } else {
836
+ ag.addFrame(loadedImage, options);
837
+ }
838
+ }
839
+ });
840
+ getBase64GIF2(ag, callback);
841
+ }
842
+ }
843
+ function stopVideoStreaming2(options) {
844
+ options = utils.isObject(options) ? options : {};
845
+ videoStream.stopVideoStreaming(options);
846
+ }
847
+ function createAndGetGIF(obj, callback) {
848
+ const options = obj.options || {};
849
+ const images = options.images;
850
+ const video = options.video;
851
+ const gifWidth = Number(options.gifWidth);
852
+ const gifHeight = Number(options.gifHeight);
853
+ const numFrames = Number(options.numFrames);
854
+ const cameraStream = obj.cameraStream;
855
+ const videoElement = obj.videoElement;
856
+ const videoWidth = obj.videoWidth;
857
+ const videoHeight = obj.videoHeight;
858
+ const cropDimensions = screenShot.getCropDimensions({
859
+ videoWidth,
860
+ videoHeight,
861
+ gifHeight,
862
+ gifWidth
863
+ });
864
+ const completeCallback = callback;
865
+ options.crop = cropDimensions;
866
+ options.videoElement = videoElement;
867
+ options.videoWidth = videoWidth;
868
+ options.videoHeight = videoHeight;
869
+ options.cameraStream = cameraStream;
870
+ if (!utils.isElement(videoElement)) {
871
+ return;
872
+ }
873
+ videoElement.width = gifWidth + cropDimensions.width;
874
+ videoElement.height = gifHeight + cropDimensions.height;
875
+ if (!options.webcamVideoElement) {
876
+ utils.setCSSAttr(videoElement, {
877
+ position: "fixed",
878
+ opacity: "0"
879
+ });
880
+ document.body.appendChild(videoElement);
881
+ }
882
+ videoElement.play();
883
+ screenShot.getGIF(options, function(obj2) {
884
+ if ((!images || !images.length) && (!video || !video.length)) {
885
+ stopVideoStreaming2(obj2);
886
+ }
887
+ completeCallback(obj2);
888
+ });
889
+ }
890
+ function existingVideo() {
891
+ const obj = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
892
+ const callback = obj.callback;
893
+ let existingVideo2 = obj.existingVideo;
894
+ const options = obj.options;
895
+ const skipObj = {
896
+ getUserMedia: true,
897
+ "globalThis.URL": true
898
+ };
899
+ const errorObj = error.validate(skipObj);
900
+ const loadedImages = 0;
901
+ let videoType = void 0;
902
+ let videoSrc = void 0;
903
+ const tempImage = void 0;
904
+ const ag = void 0;
905
+ if (errorObj.error) {
906
+ return callback(errorObj);
907
+ }
908
+ if (utils.isElement(existingVideo2) && existingVideo2.src) {
909
+ videoSrc = existingVideo2.src;
910
+ videoType = utils.getExtension(videoSrc);
911
+ if (!utils.isSupported.videoCodecs[videoType]) {
912
+ return callback(error.messages.videoCodecs);
913
+ }
914
+ } else if (utils.isArray(existingVideo2)) {
915
+ utils.each(existingVideo2, function(iterator, videoSrc2) {
916
+ if (videoSrc2 instanceof Blob) {
917
+ videoType = videoSrc2.type.substr(videoSrc2.type.lastIndexOf("/") + 1, videoSrc2.length);
918
+ } else {
919
+ videoType = videoSrc2.substr(videoSrc2.lastIndexOf(".") + 1, videoSrc2.length);
920
+ }
921
+ if (utils.isSupported.videoCodecs[videoType]) {
922
+ existingVideo2 = videoSrc2;
923
+ return false;
924
+ }
925
+ });
926
+ }
927
+ videoStream.startStreaming({
928
+ completed: function completed(obj2) {
929
+ obj2.options = options || {};
930
+ createAndGetGIF(obj2, callback);
931
+ },
932
+ existingVideo: existingVideo2,
933
+ crossOrigin: options.crossOrigin,
934
+ options
935
+ });
936
+ }
937
+ function existingWebcam() {
938
+ const obj = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
939
+ const callback = obj.callback;
940
+ const lastCameraStream = obj.lastCameraStream;
941
+ const options = obj.options;
942
+ const webcamVideoElement = obj.webcamVideoElement;
943
+ if (!isWebCamGIFSupported()) {
944
+ return callback(error.validate());
945
+ }
946
+ if (options.savedRenderingContexts.length) {
947
+ screenShot.getGIF(options, function(obj2) {
948
+ callback(obj2);
949
+ });
950
+ return;
951
+ }
952
+ videoStream.startVideoStreaming(function() {
953
+ const obj2 = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
954
+ obj2.options = options || {};
955
+ createAndGetGIF(obj2, callback);
956
+ }, {
957
+ lastCameraStream,
958
+ callback,
959
+ webcamVideoElement,
960
+ crossOrigin: options.crossOrigin
961
+ });
962
+ }
963
+ function createGIF(userOptions, callback) {
964
+ callback = utils.isFunction(userOptions) ? userOptions : callback;
965
+ userOptions = utils.isObject(userOptions) ? userOptions : {};
966
+ if (!utils.isFunction(callback)) {
967
+ return;
968
+ }
969
+ let options = utils.normalizeOptions(defaultOptions, userOptions) || {};
970
+ const lastCameraStream = userOptions.cameraStream;
971
+ const images = options.images;
972
+ const imagesLength = images ? images.length : 0;
973
+ const video = options.video;
974
+ const webcamVideoElement = options.webcamVideoElement;
975
+ options = utils.normalizeOptions(options, {
976
+ gifWidth: Math.floor(options.gifWidth),
977
+ gifHeight: Math.floor(options.gifHeight)
978
+ });
979
+ if (imagesLength) {
980
+ existingImages({
981
+ images,
982
+ imagesLength,
983
+ callback,
984
+ options
985
+ });
986
+ } else if (video) {
987
+ existingVideo({
988
+ existingVideo: video,
989
+ callback,
990
+ options
991
+ });
992
+ } else {
993
+ existingWebcam({
994
+ lastCameraStream,
995
+ callback,
996
+ webcamVideoElement,
997
+ options
998
+ });
999
+ }
1000
+ }
1001
+ function takeSnapShot(userOptions, callback) {
1002
+ callback = utils.isFunction(userOptions) ? userOptions : callback;
1003
+ userOptions = utils.isObject(userOptions) ? userOptions : {};
1004
+ if (!utils.isFunction(callback)) {
1005
+ return;
1006
+ }
1007
+ const mergedOptions = utils.normalizeOptions(defaultOptions, userOptions);
1008
+ const options = utils.normalizeOptions(mergedOptions, {
1009
+ interval: 0.1,
1010
+ numFrames: 1,
1011
+ gifWidth: Math.floor(mergedOptions.gifWidth),
1012
+ gifHeight: Math.floor(mergedOptions.gifHeight)
1013
+ });
1014
+ createGIF(options, callback);
1015
+ }
1016
+ var utils, utils$2, error, error$2, noop2, defaultOptions, defaultOptions$2, noop$2, AnimatedGIF, noop$3, screenShot, videoStream, API, gifshot_default;
1017
+ var init_gifshot = __esm({
1018
+ "src/lib/gifshot/gifshot.ts"() {
1019
+ utils = {
1020
+ URL: globalThis.URL || globalThis.webkitURL || globalThis.mozURL || globalThis.msURL,
1021
+ getUserMedia: function() {
1022
+ if (!globalThis.navigator)
1023
+ return globalThis.navigator;
1024
+ const getUserMedia = globalThis.navigator.getUserMedia || globalThis.navigator.webkitGetUserMedia || globalThis.navigator.mozGetUserMedia || globalThis.navigator.msGetUserMedia;
1025
+ return getUserMedia ? getUserMedia.bind(globalThis.navigator) : getUserMedia;
1026
+ }(),
1027
+ requestAnimFrame: globalThis.requestAnimationFrame || globalThis.webkitRequestAnimationFrame || globalThis.mozRequestAnimationFrame || globalThis.oRequestAnimationFrame || globalThis.msRequestAnimationFrame,
1028
+ requestTimeout: function requestTimeout(callback, delay) {
1029
+ callback = callback || utils.noop;
1030
+ delay = delay || 0;
1031
+ if (!utils.requestAnimFrame) {
1032
+ return setTimeout(callback, delay);
1033
+ }
1034
+ const start = new Date().getTime();
1035
+ const handle = new Object();
1036
+ const requestAnimFrame = utils.requestAnimFrame;
1037
+ const loop = function loop2() {
1038
+ const current = new Date().getTime();
1039
+ const delta = current - start;
1040
+ delta >= delay ? callback.call() : handle.value = requestAnimFrame(loop2);
1041
+ };
1042
+ handle.value = requestAnimFrame(loop);
1043
+ return handle;
1044
+ },
1045
+ Blob: globalThis.Blob || globalThis.BlobBuilder || globalThis.WebKitBlobBuilder || globalThis.MozBlobBuilder || globalThis.MSBlobBuilder,
1046
+ btoa: function() {
1047
+ const btoa = globalThis.btoa || function(input) {
1048
+ let output = "";
1049
+ let i = 0;
1050
+ const l = input.length;
1051
+ const key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
1052
+ let chr1 = void 0;
1053
+ let chr2 = void 0;
1054
+ let chr3 = void 0;
1055
+ let enc1 = void 0;
1056
+ let enc2 = void 0;
1057
+ let enc3 = void 0;
1058
+ let enc4 = void 0;
1059
+ while (i < l) {
1060
+ chr1 = input.charCodeAt(i++);
1061
+ chr2 = input.charCodeAt(i++);
1062
+ chr3 = input.charCodeAt(i++);
1063
+ enc1 = chr1 >> 2;
1064
+ enc2 = (chr1 & 3) << 4 | chr2 >> 4;
1065
+ enc3 = (chr2 & 15) << 2 | chr3 >> 6;
1066
+ enc4 = chr3 & 63;
1067
+ if (isNaN(chr2)) {
1068
+ enc3 = enc4 = 64;
1069
+ } else if (isNaN(chr3)) {
1070
+ enc4 = 64;
1071
+ }
1072
+ output = output + key.charAt(enc1) + key.charAt(enc2) + key.charAt(enc3) + key.charAt(enc4);
1073
+ }
1074
+ return output;
1075
+ };
1076
+ return btoa ? btoa.bind(globalThis) : utils.noop;
1077
+ }(),
1078
+ isObject: function isObject(obj) {
1079
+ return obj && Object.prototype.toString.call(obj) === "[object Object]";
1080
+ },
1081
+ isEmptyObject: function isEmptyObject(obj) {
1082
+ return utils.isObject(obj) && !Object.keys(obj).length;
1083
+ },
1084
+ isArray: function isArray(arr) {
1085
+ return arr && Array.isArray(arr);
1086
+ },
1087
+ isFunction: function isFunction(func) {
1088
+ return func && typeof func === "function";
1089
+ },
1090
+ isElement: function isElement(elem) {
1091
+ return elem && elem.nodeType === 1;
1092
+ },
1093
+ isString: function isString(value) {
1094
+ return typeof value === "string" || Object.prototype.toString.call(value) === "[object String]";
1095
+ },
1096
+ isSupported: {
1097
+ canvas: function canvas() {
1098
+ const el = document.createElement("canvas");
1099
+ return el && el.getContext && el.getContext("2d");
1100
+ },
1101
+ webworkers: function webworkers() {
1102
+ return globalThis.Worker;
1103
+ },
1104
+ blob: function blob() {
1105
+ return utils.Blob;
1106
+ },
1107
+ Uint8Array: function Uint8Array2() {
1108
+ return globalThis.Uint8Array;
1109
+ },
1110
+ Uint32Array: function Uint32Array2() {
1111
+ return globalThis.Uint32Array;
1112
+ },
1113
+ videoCodecs: function() {
1114
+ const testEl = document.createElement("video");
1115
+ const supportObj = {
1116
+ mp4: false,
1117
+ h264: false,
1118
+ ogv: false,
1119
+ ogg: false,
1120
+ webm: false
1121
+ };
1122
+ try {
1123
+ if (testEl && testEl.canPlayType) {
1124
+ supportObj.mp4 = testEl.canPlayType('video/mp4; codecs="mp4v.20.8"') !== "";
1125
+ supportObj.h264 = (testEl.canPlayType('video/mp4; codecs="avc1.42E01E"') || testEl.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"')) !== "";
1126
+ supportObj.ogv = testEl.canPlayType('video/ogg; codecs="theora"') !== "";
1127
+ supportObj.ogg = testEl.canPlayType('video/ogg; codecs="theora"') !== "";
1128
+ supportObj.webm = testEl.canPlayType('video/webm; codecs="vp8, vorbis"') !== -1;
1129
+ }
1130
+ } catch (e) {
1131
+ }
1132
+ return supportObj;
1133
+ }()
1134
+ },
1135
+ noop: function noop() {
1136
+ },
1137
+ each: function each(collection, callback) {
1138
+ let x = void 0;
1139
+ let len = void 0;
1140
+ if (utils.isArray(collection)) {
1141
+ x = -1;
1142
+ len = collection.length;
1143
+ while (++x < len) {
1144
+ if (callback(x, collection[x]) === false) {
1145
+ break;
1146
+ }
1147
+ }
1148
+ } else if (utils.isObject(collection)) {
1149
+ for (x in collection) {
1150
+ if (collection.hasOwnProperty(x)) {
1151
+ if (callback(x, collection[x]) === false) {
1152
+ break;
1153
+ }
1154
+ }
1155
+ }
1156
+ }
1157
+ },
1158
+ normalizeOptions: function normalizeOptions(defaultOptions2, userOptions) {
1159
+ if (!utils.isObject(defaultOptions2) || !utils.isObject(userOptions) || !Object.keys) {
1160
+ return;
1161
+ }
1162
+ const newObj = {};
1163
+ utils.each(defaultOptions2, function(key, val) {
1164
+ newObj[key] = defaultOptions2[key];
1165
+ });
1166
+ utils.each(userOptions, function(key, val) {
1167
+ const currentUserOption = userOptions[key];
1168
+ if (!utils.isObject(currentUserOption)) {
1169
+ newObj[key] = currentUserOption;
1170
+ } else if (!defaultOptions2[key]) {
1171
+ newObj[key] = currentUserOption;
1172
+ } else {
1173
+ newObj[key] = utils.normalizeOptions(defaultOptions2[key], currentUserOption);
1174
+ }
1175
+ });
1176
+ return newObj;
1177
+ },
1178
+ setCSSAttr: function setCSSAttr(elem, attr, val) {
1179
+ if (!utils.isElement(elem)) {
1180
+ return;
1181
+ }
1182
+ if (utils.isString(attr) && utils.isString(val)) {
1183
+ elem.style[attr] = val;
1184
+ } else if (utils.isObject(attr)) {
1185
+ utils.each(attr, function(key, val2) {
1186
+ elem.style[key] = val2;
1187
+ });
1188
+ }
1189
+ },
1190
+ removeElement: function removeElement(node) {
1191
+ if (!utils.isElement(node)) {
1192
+ return;
1193
+ }
1194
+ if (node.parentNode) {
1195
+ node.parentNode.removeChild(node);
1196
+ }
1197
+ },
1198
+ createWebWorker: function createWebWorker(content) {
1199
+ if (!utils.isString(content)) {
1200
+ return {};
1201
+ }
1202
+ try {
1203
+ const blob2 = new utils.Blob([content], {
1204
+ type: "text/javascript"
1205
+ });
1206
+ const objectUrl = utils.URL.createObjectURL(blob2);
1207
+ const worker = new Worker(objectUrl);
1208
+ return {
1209
+ objectUrl,
1210
+ worker
1211
+ };
1212
+ } catch (e) {
1213
+ return `${e}`;
1214
+ }
1215
+ },
1216
+ getExtension: function getExtension(src) {
1217
+ return src.substr(src.lastIndexOf(".") + 1, src.length);
1218
+ },
1219
+ getFontSize: function getFontSize() {
1220
+ const options = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
1221
+ if (!document.body || options.resizeFont === false) {
1222
+ return options.fontSize;
1223
+ }
1224
+ const text = options.text;
1225
+ const containerWidth = options.gifWidth;
1226
+ let fontSize = parseInt(options.fontSize, 10);
1227
+ const minFontSize = parseInt(options.minFontSize, 10);
1228
+ const div = document.createElement("div");
1229
+ const span = document.createElement("span");
1230
+ div.setAttribute("width", containerWidth);
1231
+ div.appendChild(span);
1232
+ span.innerHTML = text;
1233
+ span.style.fontSize = `${fontSize}px`;
1234
+ span.style.textIndent = "-9999px";
1235
+ span.style.visibility = "hidden";
1236
+ document.body.appendChild(span);
1237
+ while (span.offsetWidth > containerWidth && fontSize >= minFontSize) {
1238
+ span.style.fontSize = `${--fontSize}px`;
1239
+ }
1240
+ document.body.removeChild(span);
1241
+ return `${fontSize}px`;
1242
+ },
1243
+ webWorkerError: false
1244
+ };
1245
+ utils$2 = Object.freeze({
1246
+ default: utils
1247
+ });
1248
+ error = {
1249
+ validate: function validate(skipObj) {
1250
+ skipObj = utils.isObject(skipObj) ? skipObj : {};
1251
+ let errorObj = {};
1252
+ utils.each(error.validators, function(indece, currentValidator) {
1253
+ const errorCode = currentValidator.errorCode;
1254
+ if (!skipObj[errorCode] && !currentValidator.condition) {
1255
+ errorObj = currentValidator;
1256
+ errorObj.error = true;
1257
+ return false;
1258
+ }
1259
+ });
1260
+ delete errorObj.condition;
1261
+ return errorObj;
1262
+ },
1263
+ isValid: function isValid(skipObj) {
1264
+ const errorObj = error.validate(skipObj);
1265
+ const isValid2 = errorObj.error !== true;
1266
+ return isValid2;
1267
+ },
1268
+ validators: [
1269
+ {
1270
+ condition: utils.isFunction(utils.getUserMedia),
1271
+ errorCode: "getUserMedia",
1272
+ errorMsg: "The getUserMedia API is not supported in your browser"
1273
+ },
1274
+ {
1275
+ condition: utils.isSupported.canvas(),
1276
+ errorCode: "canvas",
1277
+ errorMsg: "Canvas elements are not supported in your browser"
1278
+ },
1279
+ {
1280
+ condition: utils.isSupported.webworkers(),
1281
+ errorCode: "webworkers",
1282
+ errorMsg: "The Web Workers API is not supported in your browser"
1283
+ },
1284
+ {
1285
+ condition: utils.isFunction(utils.URL),
1286
+ errorCode: "globalThis.URL",
1287
+ errorMsg: "The globalThis.URL API is not supported in your browser"
1288
+ },
1289
+ {
1290
+ condition: utils.isSupported.blob(),
1291
+ errorCode: "globalThis.Blob",
1292
+ errorMsg: "The globalThis.Blob File API is not supported in your browser"
1293
+ },
1294
+ {
1295
+ condition: utils.isSupported.Uint8Array(),
1296
+ errorCode: "globalThis.Uint8Array",
1297
+ errorMsg: "The globalThis.Uint8Array function constructor is not supported in your browser"
1298
+ },
1299
+ {
1300
+ condition: utils.isSupported.Uint32Array(),
1301
+ errorCode: "globalThis.Uint32Array",
1302
+ errorMsg: "The globalThis.Uint32Array function constructor is not supported in your browser"
1303
+ }
1304
+ ],
1305
+ messages: {
1306
+ videoCodecs: {
1307
+ errorCode: "videocodec",
1308
+ errorMsg: "The video codec you are trying to use is not supported in your browser"
1309
+ }
1310
+ }
1311
+ };
1312
+ error$2 = Object.freeze({
1313
+ default: error
1314
+ });
1315
+ noop2 = function noop3() {
1316
+ };
1317
+ defaultOptions = {
1318
+ sampleInterval: 10,
1319
+ numWorkers: 2,
1320
+ filter: "",
1321
+ gifWidth: 200,
1322
+ gifHeight: 200,
1323
+ interval: 0.1,
1324
+ numFrames: 10,
1325
+ frameDuration: 1,
1326
+ keepCameraOn: false,
1327
+ images: [],
1328
+ video: null,
1329
+ webcamVideoElement: null,
1330
+ cameraStream: null,
1331
+ text: "",
1332
+ fontWeight: "normal",
1333
+ fontSize: "16px",
1334
+ minFontSize: "10px",
1335
+ resizeFont: false,
1336
+ fontFamily: "sans-serif",
1337
+ fontColor: "#ffffff",
1338
+ textAlign: "center",
1339
+ textBaseline: "bottom",
1340
+ textXCoordinate: null,
1341
+ textYCoordinate: null,
1342
+ progressCallback: noop2,
1343
+ completeCallback: noop2,
1344
+ saveRenderingContexts: false,
1345
+ savedRenderingContexts: [],
1346
+ crossOrigin: "Anonymous"
1347
+ };
1348
+ defaultOptions$2 = Object.freeze({
1349
+ default: defaultOptions
1350
+ });
1351
+ noop$2 = function noop4() {
1352
+ };
1353
+ AnimatedGIF = function AnimatedGIF2(options) {
1354
+ this.canvas = null;
1355
+ this.ctx = null;
1356
+ this.repeat = 0;
1357
+ this.frames = [];
1358
+ this.numRenderedFrames = 0;
1359
+ this.onRenderCompleteCallback = noop$2;
1360
+ this.onRenderProgressCallback = noop$2;
1361
+ this.workers = [];
1362
+ this.availableWorkers = [];
1363
+ this.generatingGIF = false;
1364
+ this.options = options;
1365
+ this.initializeWebWorkers(options);
1366
+ };
1367
+ AnimatedGIF.prototype = {
1368
+ workerMethods: workerCode(),
1369
+ initializeWebWorkers: function initializeWebWorkers(options) {
1370
+ const self = this;
1371
+ const processFrameWorkerCode = `${NeuQuant.toString()}(${workerCode.toString()}());`;
1372
+ let webWorkerObj = void 0;
1373
+ let objectUrl = void 0;
1374
+ let webWorker = void 0;
1375
+ let numWorkers = void 0;
1376
+ let x = -1;
1377
+ let workerError = "";
1378
+ numWorkers = options.numWorkers;
1379
+ while (++x < numWorkers) {
1380
+ webWorkerObj = utils.createWebWorker(processFrameWorkerCode);
1381
+ if (utils.isObject(webWorkerObj)) {
1382
+ objectUrl = webWorkerObj.objectUrl;
1383
+ webWorker = webWorkerObj.worker;
1384
+ self.workers.push({
1385
+ worker: webWorker,
1386
+ objectUrl
1387
+ });
1388
+ self.availableWorkers.push(webWorker);
1389
+ } else {
1390
+ workerError = webWorkerObj;
1391
+ utils.webWorkerError = Boolean(webWorkerObj);
1392
+ }
1393
+ }
1394
+ this.workerError = workerError;
1395
+ this.canvas = document.createElement("canvas");
1396
+ this.canvas.width = options.gifWidth;
1397
+ this.canvas.height = options.gifHeight;
1398
+ this.ctx = this.canvas.getContext("2d");
1399
+ this.frames = [];
1400
+ },
1401
+ getWorker: function getWorker() {
1402
+ return this.availableWorkers.pop();
1403
+ },
1404
+ freeWorker: function freeWorker(worker) {
1405
+ this.availableWorkers.push(worker);
1406
+ },
1407
+ byteMap: function() {
1408
+ const byteMap = [];
1409
+ for (let i = 0; i < 256; i++) {
1410
+ byteMap[i] = String.fromCharCode(i);
1411
+ }
1412
+ return byteMap;
1413
+ }(),
1414
+ bufferToString: function bufferToString(buffer) {
1415
+ const numberValues = buffer.length;
1416
+ let str = "";
1417
+ let x = -1;
1418
+ while (++x < numberValues) {
1419
+ str += this.byteMap[buffer[x]];
1420
+ }
1421
+ return str;
1422
+ },
1423
+ onFrameFinished: function onFrameFinished(progressCallback) {
1424
+ const self = this;
1425
+ const frames = self.frames;
1426
+ const options = self.options;
1427
+ const hasExistingImages = Boolean((options.images || []).length);
1428
+ const allDone = frames.every(function(frame) {
1429
+ return !frame.beingProcessed && frame.done;
1430
+ });
1431
+ self.numRenderedFrames++;
1432
+ if (hasExistingImages) {
1433
+ progressCallback(self.numRenderedFrames / frames.length);
1434
+ }
1435
+ self.onRenderProgressCallback(self.numRenderedFrames * 0.75 / frames.length);
1436
+ if (allDone) {
1437
+ if (!self.generatingGIF) {
1438
+ self.generateGIF(frames, self.onRenderCompleteCallback);
1439
+ }
1440
+ } else {
1441
+ utils.requestTimeout(function() {
1442
+ self.processNextFrame();
1443
+ }, 1);
1444
+ }
1445
+ },
1446
+ processFrame: function processFrame(position) {
1447
+ const AnimatedGifContext = this;
1448
+ const options = this.options;
1449
+ const _options = this.options;
1450
+ const progressCallback = _options.progressCallback;
1451
+ const sampleInterval = _options.sampleInterval;
1452
+ const frames = this.frames;
1453
+ let frame = void 0;
1454
+ let worker = void 0;
1455
+ const done = function done2() {
1456
+ const ev = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
1457
+ const data = ev.data;
1458
+ delete frame.data;
1459
+ frame.pixels = Array.prototype.slice.call(data.pixels);
1460
+ frame.palette = Array.prototype.slice.call(data.palette);
1461
+ frame.done = true;
1462
+ frame.beingProcessed = false;
1463
+ AnimatedGifContext.freeWorker(worker);
1464
+ AnimatedGifContext.onFrameFinished(progressCallback);
1465
+ };
1466
+ frame = frames[position];
1467
+ if (frame.beingProcessed || frame.done) {
1468
+ this.onFrameFinished();
1469
+ return;
1470
+ }
1471
+ frame.sampleInterval = sampleInterval;
1472
+ frame.beingProcessed = true;
1473
+ frame.gifshot = true;
1474
+ worker = this.getWorker();
1475
+ if (worker) {
1476
+ worker.onmessage = done;
1477
+ worker.postMessage(frame);
1478
+ } else {
1479
+ done({
1480
+ data: AnimatedGifContext.workerMethods.run(frame)
1481
+ });
1482
+ }
1483
+ },
1484
+ startRendering: function startRendering(completeCallback) {
1485
+ this.onRenderCompleteCallback = completeCallback;
1486
+ for (let i = 0; i < this.options.numWorkers && i < this.frames.length; i++) {
1487
+ this.processFrame(i);
1488
+ }
1489
+ },
1490
+ processNextFrame: function processNextFrame() {
1491
+ let position = -1;
1492
+ for (let i = 0; i < this.frames.length; i++) {
1493
+ const frame = this.frames[i];
1494
+ if (!frame.done && !frame.beingProcessed) {
1495
+ position = i;
1496
+ break;
1497
+ }
1498
+ }
1499
+ if (position >= 0) {
1500
+ this.processFrame(position);
1501
+ }
1502
+ },
1503
+ generateGIF: function generateGIF(frames, callback) {
1504
+ const buffer = [];
1505
+ const gifOptions = {
1506
+ loop: this.repeat
1507
+ };
1508
+ const options = this.options;
1509
+ const interval = options.interval;
1510
+ const frameDuration = options.frameDuration;
1511
+ const existingImages2 = options.images;
1512
+ const hasExistingImages = Boolean(existingImages2.length);
1513
+ const height = options.gifHeight;
1514
+ const width = options.gifWidth;
1515
+ const gifWriter$$1 = new gifWriter(buffer, width, height, gifOptions);
1516
+ const onRenderProgressCallback = this.onRenderProgressCallback;
1517
+ const delay = hasExistingImages ? interval * 100 : 0;
1518
+ let bufferToString2 = void 0;
1519
+ let gif = void 0;
1520
+ this.generatingGIF = true;
1521
+ utils.each(frames, function(iterator, frame) {
1522
+ const framePalette = frame.palette;
1523
+ onRenderProgressCallback(0.75 + 0.25 * frame.position * 1 / frames.length);
1524
+ for (let i = 0; i < frameDuration; i++) {
1525
+ gifWriter$$1.addFrame(0, 0, width, height, frame.pixels, {
1526
+ palette: framePalette,
1527
+ delay
1528
+ });
1529
+ }
1530
+ });
1531
+ gifWriter$$1.end();
1532
+ onRenderProgressCallback(1);
1533
+ this.frames = [];
1534
+ this.generatingGIF = false;
1535
+ if (utils.isFunction(callback)) {
1536
+ bufferToString2 = this.bufferToString(buffer);
1537
+ gif = `data:image/gif;base64,${utils.btoa(bufferToString2)}`;
1538
+ callback(gif);
1539
+ }
1540
+ },
1541
+ setRepeat: function setRepeat(r) {
1542
+ this.repeat = r;
1543
+ },
1544
+ addFrame: function addFrame(element, gifshotOptions) {
1545
+ gifshotOptions = utils.isObject(gifshotOptions) ? gifshotOptions : {};
1546
+ const self = this;
1547
+ const ctx = self.ctx;
1548
+ const options = self.options;
1549
+ const width = options.gifWidth;
1550
+ const height = options.gifHeight;
1551
+ const fontSize = utils.getFontSize(gifshotOptions);
1552
+ const _gifshotOptions = gifshotOptions;
1553
+ const filter = _gifshotOptions.filter;
1554
+ const fontColor = _gifshotOptions.fontColor;
1555
+ const fontFamily = _gifshotOptions.fontFamily;
1556
+ const fontWeight = _gifshotOptions.fontWeight;
1557
+ const gifHeight = _gifshotOptions.gifHeight;
1558
+ const gifWidth = _gifshotOptions.gifWidth;
1559
+ const text = _gifshotOptions.text;
1560
+ const textAlign = _gifshotOptions.textAlign;
1561
+ const textBaseline = _gifshotOptions.textBaseline;
1562
+ const textXCoordinate = gifshotOptions.textXCoordinate ? gifshotOptions.textXCoordinate : textAlign === "left" ? 1 : textAlign === "right" ? width : width / 2;
1563
+ const textYCoordinate = gifshotOptions.textYCoordinate ? gifshotOptions.textYCoordinate : textBaseline === "top" ? 1 : textBaseline === "center" ? height / 2 : height;
1564
+ const font = `${fontWeight} ${fontSize} ${fontFamily}`;
1565
+ let imageData = void 0;
1566
+ try {
1567
+ ctx.filter = filter;
1568
+ ctx.drawImage(element, 0, 0, width, height);
1569
+ if (text) {
1570
+ ctx.font = font;
1571
+ ctx.fillStyle = fontColor;
1572
+ ctx.textAlign = textAlign;
1573
+ ctx.textBaseline = textBaseline;
1574
+ ctx.fillText(text, textXCoordinate, textYCoordinate);
1575
+ }
1576
+ imageData = ctx.getImageData(0, 0, width, height);
1577
+ self.addFrameImageData(imageData);
1578
+ } catch (e) {
1579
+ return `${e}`;
1580
+ }
1581
+ },
1582
+ addFrameImageData: function addFrameImageData() {
1583
+ const imageData = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
1584
+ const frames = this.frames;
1585
+ const imageDataArray = imageData.data;
1586
+ this.frames.push({
1587
+ data: imageDataArray,
1588
+ width: imageData.width,
1589
+ height: imageData.height,
1590
+ palette: null,
1591
+ dithering: null,
1592
+ done: false,
1593
+ beingProcessed: false,
1594
+ position: frames.length
1595
+ });
1596
+ },
1597
+ onRenderProgress: function onRenderProgress(callback) {
1598
+ this.onRenderProgressCallback = callback;
1599
+ },
1600
+ isRendering: function isRendering() {
1601
+ return this.generatingGIF;
1602
+ },
1603
+ getBase64GIF: function getBase64GIF(completeCallback) {
1604
+ const self = this;
1605
+ const onRenderComplete = function onRenderComplete2(gif) {
1606
+ self.destroyWorkers();
1607
+ utils.requestTimeout(function() {
1608
+ completeCallback(gif);
1609
+ }, 0);
1610
+ };
1611
+ self.startRendering(onRenderComplete);
1612
+ },
1613
+ destroyWorkers: function destroyWorkers() {
1614
+ if (this.workerError) {
1615
+ return;
1616
+ }
1617
+ const workers = this.workers;
1618
+ utils.each(workers, function(iterator, workerObj) {
1619
+ const worker = workerObj.worker;
1620
+ const objectUrl = workerObj.objectUrl;
1621
+ worker.terminate();
1622
+ utils.URL.revokeObjectURL(objectUrl);
1623
+ });
1624
+ }
1625
+ };
1626
+ noop$3 = function noop5() {
1627
+ };
1628
+ screenShot = {
1629
+ getGIF: function getGIF() {
1630
+ const options = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
1631
+ let callback = arguments[1];
1632
+ callback = utils.isFunction(callback) ? callback : noop$3;
1633
+ const canvas2 = document.createElement("canvas");
1634
+ let context = void 0;
1635
+ const existingImages2 = options.images;
1636
+ const hasExistingImages = Boolean(existingImages2.length);
1637
+ const cameraStream = options.cameraStream;
1638
+ const crop = options.crop;
1639
+ const filter = options.filter;
1640
+ const fontColor = options.fontColor;
1641
+ const fontFamily = options.fontFamily;
1642
+ const fontWeight = options.fontWeight;
1643
+ const keepCameraOn = options.keepCameraOn;
1644
+ const numWorkers = options.numWorkers;
1645
+ const progressCallback = options.progressCallback;
1646
+ const saveRenderingContexts = options.saveRenderingContexts;
1647
+ const savedRenderingContexts = options.savedRenderingContexts;
1648
+ const text = options.text;
1649
+ const textAlign = options.textAlign;
1650
+ const textBaseline = options.textBaseline;
1651
+ const videoElement = options.videoElement;
1652
+ const videoHeight = options.videoHeight;
1653
+ const videoWidth = options.videoWidth;
1654
+ const webcamVideoElement = options.webcamVideoElement;
1655
+ const gifWidth = Number(options.gifWidth);
1656
+ const gifHeight = Number(options.gifHeight);
1657
+ let interval = Number(options.interval);
1658
+ const sampleInterval = Number(options.sampleInterval);
1659
+ const waitBetweenFrames = hasExistingImages ? 0 : interval * 1e3;
1660
+ const renderingContextsToSave = [];
1661
+ let numFrames = savedRenderingContexts.length ? savedRenderingContexts.length : options.numFrames;
1662
+ let pendingFrames = numFrames;
1663
+ const ag = new AnimatedGIF(options);
1664
+ const fontSize = utils.getFontSize(options);
1665
+ const textXCoordinate = options.textXCoordinate ? options.textXCoordinate : textAlign === "left" ? 1 : textAlign === "right" ? gifWidth : gifWidth / 2;
1666
+ const textYCoordinate = options.textYCoordinate ? options.textYCoordinate : textBaseline === "top" ? 1 : textBaseline === "center" ? gifHeight / 2 : gifHeight;
1667
+ const font = `${fontWeight} ${fontSize} ${fontFamily}`;
1668
+ let sourceX = crop ? Math.floor(crop.scaledWidth / 2) : 0;
1669
+ let sourceWidth = crop ? videoWidth - crop.scaledWidth : 0;
1670
+ let sourceY = crop ? Math.floor(crop.scaledHeight / 2) : 0;
1671
+ let sourceHeight = crop ? videoHeight - crop.scaledHeight : 0;
1672
+ const captureFrames = function captureSingleFrame() {
1673
+ const framesLeft = pendingFrames - 1;
1674
+ if (savedRenderingContexts.length) {
1675
+ context.putImageData(savedRenderingContexts[numFrames - pendingFrames], 0, 0);
1676
+ finishCapture();
1677
+ } else {
1678
+ drawVideo();
1679
+ }
1680
+ function drawVideo() {
1681
+ try {
1682
+ if (sourceWidth > videoWidth) {
1683
+ sourceWidth = videoWidth;
1684
+ }
1685
+ if (sourceHeight > videoHeight) {
1686
+ sourceHeight = videoHeight;
1687
+ }
1688
+ if (sourceX < 0) {
1689
+ sourceX = 0;
1690
+ }
1691
+ if (sourceY < 0) {
1692
+ sourceY = 0;
1693
+ }
1694
+ context.filter = filter;
1695
+ context.drawImage(videoElement, sourceX, sourceY, sourceWidth, sourceHeight, 0, 0, gifWidth, gifHeight);
1696
+ finishCapture();
1697
+ } catch (e) {
1698
+ if (e.name === "NS_ERROR_NOT_AVAILABLE") {
1699
+ utils.requestTimeout(drawVideo, 100);
1700
+ } else {
1701
+ throw e;
1702
+ }
1703
+ }
1704
+ }
1705
+ function finishCapture() {
1706
+ let imageData = void 0;
1707
+ if (saveRenderingContexts) {
1708
+ renderingContextsToSave.push(context.getImageData(0, 0, gifWidth, gifHeight));
1709
+ }
1710
+ if (text) {
1711
+ context.font = font;
1712
+ context.fillStyle = fontColor;
1713
+ context.textAlign = textAlign;
1714
+ context.textBaseline = textBaseline;
1715
+ context.fillText(text, textXCoordinate, textYCoordinate);
1716
+ }
1717
+ imageData = context.getImageData(0, 0, gifWidth, gifHeight);
1718
+ ag.addFrameImageData(imageData);
1719
+ pendingFrames = framesLeft;
1720
+ progressCallback((numFrames - pendingFrames) / numFrames);
1721
+ if (framesLeft > 0) {
1722
+ utils.requestTimeout(captureSingleFrame, waitBetweenFrames);
1723
+ }
1724
+ if (!pendingFrames) {
1725
+ ag.getBase64GIF(function(image) {
1726
+ callback({
1727
+ error: false,
1728
+ errorCode: "",
1729
+ errorMsg: "",
1730
+ image,
1731
+ cameraStream,
1732
+ videoElement,
1733
+ webcamVideoElement,
1734
+ savedRenderingContexts: renderingContextsToSave,
1735
+ keepCameraOn
1736
+ });
1737
+ });
1738
+ }
1739
+ }
1740
+ };
1741
+ numFrames = numFrames !== void 0 ? numFrames : 10;
1742
+ interval = interval !== void 0 ? interval : 0.1;
1743
+ canvas2.width = gifWidth;
1744
+ canvas2.height = gifHeight;
1745
+ context = canvas2.getContext("2d");
1746
+ (function capture() {
1747
+ if (!savedRenderingContexts.length && videoElement.currentTime === 0) {
1748
+ utils.requestTimeout(capture, 100);
1749
+ return;
1750
+ }
1751
+ captureFrames();
1752
+ })();
1753
+ },
1754
+ getCropDimensions: function getCropDimensions() {
1755
+ const obj = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
1756
+ const width = obj.videoWidth;
1757
+ const height = obj.videoHeight;
1758
+ const gifWidth = obj.gifWidth;
1759
+ const gifHeight = obj.gifHeight;
1760
+ const result = {
1761
+ width: 0,
1762
+ height: 0,
1763
+ scaledWidth: 0,
1764
+ scaledHeight: 0
1765
+ };
1766
+ if (width > height) {
1767
+ result.width = Math.round(width * (gifHeight / height)) - gifWidth;
1768
+ result.scaledWidth = Math.round(result.width * (height / gifHeight));
1769
+ } else {
1770
+ result.height = Math.round(height * (gifWidth / width)) - gifHeight;
1771
+ result.scaledHeight = Math.round(result.height * (width / gifWidth));
1772
+ }
1773
+ return result;
1774
+ }
1775
+ };
1776
+ videoStream = {
1777
+ loadedData: false,
1778
+ defaultVideoDimensions: {
1779
+ width: 640,
1780
+ height: 480
1781
+ },
1782
+ findVideoSize: function findVideoSizeMethod(obj) {
1783
+ findVideoSizeMethod.attempts = findVideoSizeMethod.attempts || 0;
1784
+ const cameraStream = obj.cameraStream;
1785
+ const completedCallback = obj.completedCallback;
1786
+ const videoElement = obj.videoElement;
1787
+ if (!videoElement) {
1788
+ return;
1789
+ }
1790
+ if (videoElement.videoWidth > 0 && videoElement.videoHeight > 0) {
1791
+ videoElement.removeEventListener("loadeddata", videoStream.findVideoSize);
1792
+ completedCallback({
1793
+ videoElement,
1794
+ cameraStream,
1795
+ videoWidth: videoElement.videoWidth,
1796
+ videoHeight: videoElement.videoHeight
1797
+ });
1798
+ } else if (findVideoSizeMethod.attempts < 10) {
1799
+ findVideoSizeMethod.attempts += 1;
1800
+ utils.requestTimeout(function() {
1801
+ videoStream.findVideoSize(obj);
1802
+ }, 400);
1803
+ } else {
1804
+ completedCallback({
1805
+ videoElement,
1806
+ cameraStream,
1807
+ videoWidth: videoStream.defaultVideoDimensions.width,
1808
+ videoHeight: videoStream.defaultVideoDimensions.height
1809
+ });
1810
+ }
1811
+ },
1812
+ onStreamingTimeout: function onStreamingTimeout(callback) {
1813
+ if (utils.isFunction(callback)) {
1814
+ callback({
1815
+ error: true,
1816
+ errorCode: "getUserMedia",
1817
+ errorMsg: "There was an issue with the getUserMedia API - Timed out while trying to start streaming",
1818
+ image: null,
1819
+ cameraStream: {}
1820
+ });
1821
+ }
1822
+ },
1823
+ stream: function stream(obj) {
1824
+ const existingVideo2 = utils.isArray(obj.existingVideo) ? obj.existingVideo[0] : obj.existingVideo;
1825
+ const cameraStream = obj.cameraStream;
1826
+ const completedCallback = obj.completedCallback;
1827
+ const streamedCallback = obj.streamedCallback;
1828
+ const videoElement = obj.videoElement;
1829
+ if (utils.isFunction(streamedCallback)) {
1830
+ streamedCallback();
1831
+ }
1832
+ if (existingVideo2) {
1833
+ if (utils.isString(existingVideo2)) {
1834
+ videoElement.src = existingVideo2;
1835
+ videoElement.innerHTML = `<source src="${existingVideo2}" type="video/${utils.getExtension(existingVideo2)}" />`;
1836
+ } else if (existingVideo2 instanceof Blob) {
1837
+ try {
1838
+ videoElement.src = utils.URL.createObjectURL(existingVideo2);
1839
+ } catch (e) {
1840
+ }
1841
+ videoElement.innerHTML = `<source src="${existingVideo2}" type="${existingVideo2.type}" />`;
1842
+ }
1843
+ } else if (videoElement.mozSrcObject) {
1844
+ videoElement.mozSrcObject = cameraStream;
1845
+ } else if (utils.URL) {
1846
+ try {
1847
+ videoElement.srcObject = cameraStream;
1848
+ videoElement.src = utils.URL.createObjectURL(cameraStream);
1849
+ } catch (e) {
1850
+ videoElement.srcObject = cameraStream;
1851
+ }
1852
+ }
1853
+ videoElement.play();
1854
+ utils.requestTimeout(function checkLoadedData() {
1855
+ checkLoadedData.count = checkLoadedData.count || 0;
1856
+ if (videoStream.loadedData === true) {
1857
+ videoStream.findVideoSize({
1858
+ videoElement,
1859
+ cameraStream,
1860
+ completedCallback
1861
+ });
1862
+ videoStream.loadedData = false;
1863
+ } else {
1864
+ checkLoadedData.count += 1;
1865
+ if (checkLoadedData.count > 10) {
1866
+ videoStream.findVideoSize({
1867
+ videoElement,
1868
+ cameraStream,
1869
+ completedCallback
1870
+ });
1871
+ } else {
1872
+ checkLoadedData();
1873
+ }
1874
+ }
1875
+ }, 0);
1876
+ },
1877
+ startStreaming: function startStreaming(obj) {
1878
+ const errorCallback = utils.isFunction(obj.error) ? obj.error : utils.noop;
1879
+ const streamedCallback = utils.isFunction(obj.streamed) ? obj.streamed : utils.noop;
1880
+ const completedCallback = utils.isFunction(obj.completed) ? obj.completed : utils.noop;
1881
+ const crossOrigin = obj.crossOrigin;
1882
+ const existingVideo2 = obj.existingVideo;
1883
+ const lastCameraStream = obj.lastCameraStream;
1884
+ const options = obj.options;
1885
+ const webcamVideoElement = obj.webcamVideoElement;
1886
+ const videoElement = utils.isElement(existingVideo2) ? existingVideo2 : webcamVideoElement ? webcamVideoElement : document.createElement("video");
1887
+ const cameraStream = void 0;
1888
+ if (crossOrigin) {
1889
+ videoElement.crossOrigin = options.crossOrigin;
1890
+ }
1891
+ videoElement.autoplay = true;
1892
+ videoElement.loop = true;
1893
+ videoElement.muted = true;
1894
+ videoElement.addEventListener("loadeddata", function(event) {
1895
+ videoStream.loadedData = true;
1896
+ if (options.offset) {
1897
+ videoElement.currentTime = options.offset;
1898
+ }
1899
+ });
1900
+ if (existingVideo2) {
1901
+ videoStream.stream({
1902
+ videoElement,
1903
+ existingVideo: existingVideo2,
1904
+ completedCallback
1905
+ });
1906
+ } else if (lastCameraStream) {
1907
+ videoStream.stream({
1908
+ videoElement,
1909
+ cameraStream: lastCameraStream,
1910
+ streamedCallback,
1911
+ completedCallback
1912
+ });
1913
+ } else {
1914
+ utils.getUserMedia({
1915
+ video: true
1916
+ }, function(stream2) {
1917
+ videoStream.stream({
1918
+ videoElement,
1919
+ cameraStream: stream2,
1920
+ streamedCallback,
1921
+ completedCallback
1922
+ });
1923
+ }, errorCallback);
1924
+ }
1925
+ },
1926
+ startVideoStreaming: function startVideoStreaming(callback) {
1927
+ const options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
1928
+ const timeoutLength = options.timeout !== void 0 ? options.timeout : 0;
1929
+ const originalCallback = options.callback;
1930
+ const webcamVideoElement = options.webcamVideoElement;
1931
+ let noGetUserMediaSupportTimeout = void 0;
1932
+ if (timeoutLength > 0) {
1933
+ noGetUserMediaSupportTimeout = utils.requestTimeout(function() {
1934
+ videoStream.onStreamingTimeout(originalCallback);
1935
+ }, 1e4);
1936
+ }
1937
+ videoStream.startStreaming({
1938
+ error: function error2() {
1939
+ originalCallback({
1940
+ error: true,
1941
+ errorCode: "getUserMedia",
1942
+ errorMsg: "There was an issue with the getUserMedia API - the user probably denied permission",
1943
+ image: null,
1944
+ cameraStream: {}
1945
+ });
1946
+ },
1947
+ streamed: function streamed() {
1948
+ clearTimeout(noGetUserMediaSupportTimeout);
1949
+ },
1950
+ completed: function completed() {
1951
+ const obj = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
1952
+ const cameraStream = obj.cameraStream;
1953
+ const videoElement = obj.videoElement;
1954
+ const videoHeight = obj.videoHeight;
1955
+ const videoWidth = obj.videoWidth;
1956
+ callback({
1957
+ cameraStream,
1958
+ videoElement,
1959
+ videoHeight,
1960
+ videoWidth
1961
+ });
1962
+ },
1963
+ lastCameraStream: options.lastCameraStream,
1964
+ webcamVideoElement,
1965
+ crossOrigin: options.crossOrigin,
1966
+ options
1967
+ });
1968
+ },
1969
+ stopVideoStreaming: function stopVideoStreaming(obj) {
1970
+ obj = utils.isObject(obj) ? obj : {};
1971
+ const _obj = obj;
1972
+ const keepCameraOn = _obj.keepCameraOn;
1973
+ const videoElement = _obj.videoElement;
1974
+ const webcamVideoElement = _obj.webcamVideoElement;
1975
+ const cameraStream = obj.cameraStream || {};
1976
+ const cameraStreamTracks = cameraStream.getTracks ? cameraStream.getTracks() || [] : [];
1977
+ const hasCameraStreamTracks = Boolean(cameraStreamTracks.length);
1978
+ const firstCameraStreamTrack = cameraStreamTracks[0];
1979
+ if (!keepCameraOn && hasCameraStreamTracks) {
1980
+ if (utils.isFunction(firstCameraStreamTrack.stop)) {
1981
+ firstCameraStreamTrack.stop();
1982
+ }
1983
+ }
1984
+ if (utils.isElement(videoElement) && !webcamVideoElement) {
1985
+ videoElement.pause();
1986
+ if (utils.isFunction(utils.URL.revokeObjectURL) && !utils.webWorkerError) {
1987
+ if (videoElement.src) {
1988
+ utils.URL.revokeObjectURL(videoElement.src);
1989
+ }
1990
+ }
1991
+ utils.removeElement(videoElement);
1992
+ }
1993
+ }
1994
+ };
1995
+ API = {
1996
+ utils: utils$2,
1997
+ error: error$2,
1998
+ defaultOptions: defaultOptions$2,
1999
+ createGIF,
2000
+ takeSnapShot,
2001
+ stopVideoStreaming: stopVideoStreaming2,
2002
+ isSupported,
2003
+ isWebCamGIFSupported,
2004
+ isExistingVideoGIFSupported,
2005
+ isExistingImagesGIFSupported: isSupported$1,
2006
+ VERSION: "0.4.5"
2007
+ };
2008
+ gifshot_default = API;
2009
+ }
2010
+ });
2011
+
2012
+ // src/gif-builder.ts
2013
+ var GIF_BUILDER_OPTIONS, GIFBuilder;
2014
+ var init_gif_builder = __esm({
2015
+ "src/gif-builder.ts"() {
2016
+ init_assert();
2017
+ init_gifshot();
2018
+ GIF_BUILDER_OPTIONS = {
2019
+ source: "images",
2020
+ width: 200,
2021
+ height: 200,
2022
+ crossOrigin: "Anonymous",
2023
+ progressCallback: (captureProgress) => {
2024
+ },
2025
+ completeCallback: () => {
2026
+ },
2027
+ numWorkers: 2,
2028
+ sampleInterval: 10,
2029
+ interval: 0.1,
2030
+ offset: null,
2031
+ numFrames: 10,
2032
+ frameDuration: 1,
2033
+ filter: "",
2034
+ waterMark: null,
2035
+ waterMarkHeight: null,
2036
+ waterMarkWidth: null,
2037
+ waterMarkXCoordinate: 1,
2038
+ waterMarkYCoordinate: 1,
2039
+ text: "",
2040
+ showFrameText: true,
2041
+ fontWeight: "normal",
2042
+ fontSize: "16px",
2043
+ minFontSize: "10px",
2044
+ resizeFont: false,
2045
+ fontFamily: "sans-serif",
2046
+ fontColor: "#ffffff",
2047
+ textAlign: "center",
2048
+ textBaseline: "bottom",
2049
+ textXCoordinate: null,
2050
+ textYCoordinate: null,
2051
+ webcamVideoElement: null,
2052
+ keepCameraOn: false,
2053
+ cameraStream: null,
2054
+ saveRenderingContexts: false,
2055
+ savedRenderingContexts: []
2056
+ };
2057
+ GIFBuilder = class {
2058
+ static get properties() {
2059
+ return {
2060
+ id: "gif",
2061
+ name: "GIF",
2062
+ extensions: ["gif"],
2063
+ mimeTypes: ["image/gif"],
2064
+ builder: GIFBuilder,
2065
+ options: GIF_BUILDER_OPTIONS
2066
+ };
2067
+ }
2068
+ constructor(options) {
2069
+ this.options = { ...options };
2070
+ this.source = options.source;
2071
+ delete options.source;
2072
+ this.files = [];
2073
+ this.gifshot = gifshot_default;
2074
+ }
2075
+ async initialize(options) {
2076
+ }
2077
+ async add(file) {
2078
+ await this.initialize();
2079
+ this.files.push(file);
2080
+ }
2081
+ async build() {
2082
+ await this.initialize();
2083
+ this._cleanOptions(this.options);
2084
+ switch (this.source) {
2085
+ case "images":
2086
+ this.options.images = this.files;
2087
+ break;
2088
+ case "video":
2089
+ this.options.video = this.files;
2090
+ break;
2091
+ case "webcam":
2092
+ assert(this.files.length === 0);
2093
+ break;
2094
+ default:
2095
+ throw new Error("GIFBuilder: invalid source");
2096
+ }
2097
+ return await this._createGIF();
2098
+ }
2099
+ async _createGIF() {
2100
+ return new Promise((resolve, reject) => {
2101
+ this.gifshot.createGIF(this.options, (result) => {
2102
+ if (result.error) {
2103
+ reject(result.errorMsg);
2104
+ return;
2105
+ }
2106
+ resolve(result.image);
2107
+ });
2108
+ });
2109
+ }
2110
+ _cleanOptions(options) {
2111
+ if (options.video || options.images || options.gifWidth || options.gifHeight) {
2112
+ console.warn("GIFBuilder: ignoring options");
2113
+ }
2114
+ delete options.video;
2115
+ delete options.images;
2116
+ options.gifWidth = options.width;
2117
+ options.gifHeight = options.height;
2118
+ delete options.width;
2119
+ delete options.height;
2120
+ }
2121
+ };
2122
+ }
2123
+ });
2124
+
2125
+ // src/index.ts
2126
+ var src_exports = {};
2127
+ __export(src_exports, {
2128
+ GIFBuilder: () => GIFBuilder,
2129
+ VideoLoader: () => VideoLoader
2130
+ });
2131
+ var init_src = __esm({
2132
+ "src/index.ts"() {
2133
+ init_video_loader();
2134
+ init_gif_builder();
2135
+ }
2136
+ });
2137
+
2138
+ // src/bundle.ts
2139
+ var require_bundle = __commonJS({
2140
+ "src/bundle.ts"(exports, module) {
2141
+ var moduleExports = (init_src(), src_exports);
2142
+ globalThis.loaders = globalThis.loaders || {};
2143
+ module.exports = Object.assign(globalThis.loaders, moduleExports);
2144
+ }
2145
+ });
2146
+ require_bundle();
2147
+ })();