@loaders.gl/video 4.0.0-beta.2 → 4.0.0-beta.4

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