jassub 1.1.12 → 1.2.0
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.
- package/LICENSE +1 -1
- package/README.md +3 -3
- package/dist/COPYRIGHT +951 -951
- package/dist/jassub-worker-legacy.js +20 -20
- package/dist/jassub-worker.js +1 -1
- package/dist/jassub.es.js +495 -491
- package/dist/jassub.js +1607 -1607
- package/dist/jassub.umd.js +1 -1
- package/package.json +2 -2
- package/src/jassub.js +58 -38
package/dist/jassub.es.js
CHANGED
|
@@ -1,491 +1,495 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
this.
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
this.
|
|
70
|
-
this.
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
this.
|
|
84
|
-
this.
|
|
85
|
-
this.
|
|
86
|
-
this.
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
if (
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
const
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
this.
|
|
191
|
-
|
|
192
|
-
this.
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
const
|
|
209
|
-
const
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
this.
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
this.
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
this.
|
|
390
|
-
this.
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
const
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
this.
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
if (
|
|
461
|
-
this
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
this._video.removeEventListener("
|
|
473
|
-
this._video.removeEventListener("
|
|
474
|
-
this._video.removeEventListener("
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
this.
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => {
|
|
4
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
+
return value;
|
|
6
|
+
};
|
|
7
|
+
if (!("requestVideoFrameCallback" in HTMLVideoElement.prototype) && "getVideoPlaybackQuality" in HTMLVideoElement.prototype) {
|
|
8
|
+
HTMLVideoElement.prototype._rvfcpolyfillmap = {};
|
|
9
|
+
HTMLVideoElement.prototype.requestVideoFrameCallback = function(callback) {
|
|
10
|
+
const quality = this.getVideoPlaybackQuality();
|
|
11
|
+
const baseline = this.mozPresentedFrames || this.mozPaintedFrames || quality.totalVideoFrames - quality.droppedVideoFrames;
|
|
12
|
+
const check = (old, now2) => {
|
|
13
|
+
const newquality = this.getVideoPlaybackQuality();
|
|
14
|
+
const presentedFrames = this.mozPresentedFrames || this.mozPaintedFrames || newquality.totalVideoFrames - newquality.droppedVideoFrames;
|
|
15
|
+
if (presentedFrames > baseline) {
|
|
16
|
+
const processingDuration = this.mozFrameDelay || newquality.totalFrameDelay - quality.totalFrameDelay || 0;
|
|
17
|
+
const timediff = now2 - old;
|
|
18
|
+
callback(now2, {
|
|
19
|
+
presentationTime: now2 + processingDuration * 1e3,
|
|
20
|
+
expectedDisplayTime: now2 + timediff,
|
|
21
|
+
width: this.videoWidth,
|
|
22
|
+
height: this.videoHeight,
|
|
23
|
+
mediaTime: Math.max(0, this.currentTime || 0) + timediff / 1e3,
|
|
24
|
+
presentedFrames,
|
|
25
|
+
processingDuration
|
|
26
|
+
});
|
|
27
|
+
delete this._rvfcpolyfillmap[handle];
|
|
28
|
+
} else {
|
|
29
|
+
this._rvfcpolyfillmap[handle] = requestAnimationFrame((newer) => check(now2, newer));
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
const handle = Date.now();
|
|
33
|
+
const now = performance.now();
|
|
34
|
+
this._rvfcpolyfillmap[handle] = requestAnimationFrame((newer) => check(now, newer));
|
|
35
|
+
return handle;
|
|
36
|
+
};
|
|
37
|
+
HTMLVideoElement.prototype.cancelVideoFrameCallback = function(handle) {
|
|
38
|
+
cancelAnimationFrame(this._rvfcpolyfillmap[handle]);
|
|
39
|
+
delete this._rvfcpolyfillmap[handle];
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
const _JASSUB = class extends EventTarget {
|
|
43
|
+
constructor(options = {}) {
|
|
44
|
+
var _a, _b, _c, _d, _e;
|
|
45
|
+
super();
|
|
46
|
+
if (!globalThis.Worker) {
|
|
47
|
+
this.destroy("Worker not supported");
|
|
48
|
+
}
|
|
49
|
+
_JASSUB._test();
|
|
50
|
+
const blendMode = options.blendMode || "js";
|
|
51
|
+
const asyncRender = typeof createImageBitmap !== "undefined" && ((_a = options.asyncRender) != null ? _a : true);
|
|
52
|
+
const offscreenRender = typeof OffscreenCanvas !== "undefined" && ((_b = options.offscreenRender) != null ? _b : true);
|
|
53
|
+
this._onDemandRender = "requestVideoFrameCallback" in HTMLVideoElement.prototype && ((_c = options.onDemandRender) != null ? _c : true);
|
|
54
|
+
this.timeOffset = options.timeOffset || 0;
|
|
55
|
+
this._video = options.video;
|
|
56
|
+
this._canvasParent = null;
|
|
57
|
+
if (this._video) {
|
|
58
|
+
this._canvasParent = document.createElement("div");
|
|
59
|
+
this._canvasParent.className = "JASSUB";
|
|
60
|
+
this._canvasParent.style.position = "relative";
|
|
61
|
+
if (this._video.nextSibling) {
|
|
62
|
+
this._video.parentNode.insertBefore(this._canvasParent, this._video.nextSibling);
|
|
63
|
+
} else {
|
|
64
|
+
this._video.parentNode.appendChild(this._canvasParent);
|
|
65
|
+
}
|
|
66
|
+
} else if (!this._canvas) {
|
|
67
|
+
this.destroy("Don't know where to render: you should give video or canvas in options.");
|
|
68
|
+
}
|
|
69
|
+
this._canvas = options.canvas || document.createElement("canvas");
|
|
70
|
+
this._canvas.style.display = "block";
|
|
71
|
+
this._canvas.style.position = "absolute";
|
|
72
|
+
this._canvas.style.pointerEvents = "none";
|
|
73
|
+
this._canvasParent.appendChild(this._canvas);
|
|
74
|
+
this._bufferCanvas = document.createElement("canvas");
|
|
75
|
+
this._bufferCtx = this._bufferCanvas.getContext("2d");
|
|
76
|
+
this._canvasctrl = offscreenRender ? this._canvas.transferControlToOffscreen() : this._canvas;
|
|
77
|
+
this._ctx = !offscreenRender && this._canvasctrl.getContext("2d");
|
|
78
|
+
this._lastRenderTime = 0;
|
|
79
|
+
this.debug = !!options.debug;
|
|
80
|
+
this.prescaleFactor = options.prescaleFactor || 1;
|
|
81
|
+
this.prescaleHeightLimit = options.prescaleHeightLimit || 1080;
|
|
82
|
+
this.maxRenderHeight = options.maxRenderHeight || 0;
|
|
83
|
+
this._worker = new Worker(_JASSUB._supportsWebAssembly ? options.workerUrl || "jassub-worker.js" : options.legacyWorkerUrl || "jassub-worker-legacy.js");
|
|
84
|
+
this._worker.onmessage = (e) => this._onmessage(e);
|
|
85
|
+
this._worker.onerror = (e) => this._error(e);
|
|
86
|
+
this._worker.postMessage({
|
|
87
|
+
target: "init",
|
|
88
|
+
asyncRender,
|
|
89
|
+
onDemandRender: this._onDemandRender,
|
|
90
|
+
width: this._canvas.width,
|
|
91
|
+
height: this._canvas.height,
|
|
92
|
+
preMain: true,
|
|
93
|
+
blendMode,
|
|
94
|
+
subUrl: options.subUrl,
|
|
95
|
+
subContent: options.subContent || null,
|
|
96
|
+
fonts: options.fonts || [],
|
|
97
|
+
availableFonts: options.availableFonts || { "liberation sans": "./default.woff2" },
|
|
98
|
+
fallbackFont: options.fallbackFont || "liberation sans",
|
|
99
|
+
debug: this.debug,
|
|
100
|
+
targetFps: options.targetFps || 24,
|
|
101
|
+
dropAllAnimations: options.dropAllAnimations,
|
|
102
|
+
libassMemoryLimit: options.libassMemoryLimit || 0,
|
|
103
|
+
libassGlyphLimit: options.libassGlyphLimit || 0,
|
|
104
|
+
hasAlphaBug: _JASSUB._hasAlphaBug,
|
|
105
|
+
useLocalFonts: "queryLocalFonts" in self && ((_d = options.useLocalFonts) != null ? _d : true)
|
|
106
|
+
});
|
|
107
|
+
if (offscreenRender === true)
|
|
108
|
+
this.sendMessage("offscreenCanvas", null, [this._canvasctrl]);
|
|
109
|
+
this._boundResize = this.resize.bind(this);
|
|
110
|
+
this._boundTimeUpdate = this._timeupdate.bind(this);
|
|
111
|
+
this._boundSetRate = this.setRate.bind(this);
|
|
112
|
+
this.setVideo(options.video);
|
|
113
|
+
if (this._onDemandRender) {
|
|
114
|
+
this.busy = false;
|
|
115
|
+
this._lastDemandTime = null;
|
|
116
|
+
(_e = this._video) == null ? void 0 : _e.requestVideoFrameCallback(this._handleRVFC.bind(this));
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
static _test() {
|
|
120
|
+
if (_JASSUB._supportsWebAssembly !== null)
|
|
121
|
+
return null;
|
|
122
|
+
const canvas1 = document.createElement("canvas");
|
|
123
|
+
const ctx1 = canvas1.getContext("2d", { willReadFrequently: true });
|
|
124
|
+
if (typeof ImageData.prototype.constructor === "function") {
|
|
125
|
+
try {
|
|
126
|
+
new ImageData(new Uint8ClampedArray([0, 0, 0, 0]), 1, 1);
|
|
127
|
+
} catch (e) {
|
|
128
|
+
console.log("detected that ImageData is not constructable despite browser saying so");
|
|
129
|
+
window.ImageData = function(data, width, height) {
|
|
130
|
+
const imageData = ctx1.createImageData(width, height);
|
|
131
|
+
if (data)
|
|
132
|
+
imageData.data.set(data);
|
|
133
|
+
return imageData;
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
if (typeof WebAssembly === "object" && typeof WebAssembly.instantiate === "function") {
|
|
139
|
+
const module = new WebAssembly.Module(Uint8Array.of(0, 97, 115, 109, 1, 0, 0, 0));
|
|
140
|
+
if (module instanceof WebAssembly.Module) {
|
|
141
|
+
_JASSUB._supportsWebAssembly = new WebAssembly.Instance(module) instanceof WebAssembly.Instance;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
} catch (e) {
|
|
145
|
+
_JASSUB._supportsWebAssembly = false;
|
|
146
|
+
}
|
|
147
|
+
const canvas2 = document.createElement("canvas");
|
|
148
|
+
const ctx2 = canvas2.getContext("2d", { willReadFrequently: true });
|
|
149
|
+
canvas1.width = canvas2.width = 1;
|
|
150
|
+
canvas1.height = canvas2.height = 1;
|
|
151
|
+
ctx1.clearRect(0, 0, 1, 1);
|
|
152
|
+
ctx2.clearRect(0, 0, 1, 1);
|
|
153
|
+
const prePut = ctx2.getImageData(0, 0, 1, 1).data;
|
|
154
|
+
ctx1.putImageData(new ImageData(new Uint8ClampedArray([0, 255, 0, 0]), 1, 1), 0, 0);
|
|
155
|
+
ctx2.drawImage(canvas1, 0, 0);
|
|
156
|
+
const postPut = ctx2.getImageData(0, 0, 1, 1).data;
|
|
157
|
+
_JASSUB._hasAlphaBug = prePut[1] !== postPut[1];
|
|
158
|
+
if (_JASSUB._hasAlphaBug)
|
|
159
|
+
console.log("Detected a browser having issue with transparent pixels, applying workaround");
|
|
160
|
+
canvas2.remove();
|
|
161
|
+
}
|
|
162
|
+
resize(width = 0, height = 0, top = 0, left = 0) {
|
|
163
|
+
let videoSize = null;
|
|
164
|
+
if ((!width || !height) && this._video) {
|
|
165
|
+
videoSize = this._getVideoPosition();
|
|
166
|
+
const newsize = this._computeCanvasSize((videoSize.width || 0) * (window.devicePixelRatio || 1), (videoSize.height || 0) * (window.devicePixelRatio || 1));
|
|
167
|
+
width = newsize.width;
|
|
168
|
+
height = newsize.height;
|
|
169
|
+
top = videoSize.y - (this._canvasParent.getBoundingClientRect().top - this._video.getBoundingClientRect().top);
|
|
170
|
+
left = videoSize.x;
|
|
171
|
+
}
|
|
172
|
+
if (videoSize != null) {
|
|
173
|
+
this._canvas.style.top = top + "px";
|
|
174
|
+
this._canvas.style.left = left + "px";
|
|
175
|
+
this._canvas.style.width = videoSize.width + "px";
|
|
176
|
+
this._canvas.style.height = videoSize.height + "px";
|
|
177
|
+
}
|
|
178
|
+
if (!(this._canvasctrl.width === width && this._canvasctrl.height === height)) {
|
|
179
|
+
if (this._resizeTimeoutBuffer) {
|
|
180
|
+
clearTimeout(this._resizeTimeoutBuffer);
|
|
181
|
+
this._resizeTimeoutBuffer = setTimeout(() => {
|
|
182
|
+
this._resizeTimeoutBuffer = void 0;
|
|
183
|
+
this._canvasctrl.width = width;
|
|
184
|
+
this._canvasctrl.height = height;
|
|
185
|
+
this.sendMessage("canvas", { width, height });
|
|
186
|
+
}, 100);
|
|
187
|
+
} else {
|
|
188
|
+
this._canvasctrl.width = width;
|
|
189
|
+
this._canvasctrl.height = height;
|
|
190
|
+
this.sendMessage("canvas", { width, height });
|
|
191
|
+
this._resizeTimeoutBuffer = setTimeout(() => {
|
|
192
|
+
this._resizeTimeoutBuffer = void 0;
|
|
193
|
+
}, 100);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
_getVideoPosition() {
|
|
198
|
+
const videoRatio = this._video.videoWidth / this._video.videoHeight;
|
|
199
|
+
const { offsetWidth, offsetHeight } = this._video;
|
|
200
|
+
const elementRatio = offsetWidth / offsetHeight;
|
|
201
|
+
let width = offsetWidth;
|
|
202
|
+
let height = offsetHeight;
|
|
203
|
+
if (elementRatio > videoRatio) {
|
|
204
|
+
width = Math.floor(offsetHeight * videoRatio);
|
|
205
|
+
} else {
|
|
206
|
+
height = Math.floor(offsetWidth / videoRatio);
|
|
207
|
+
}
|
|
208
|
+
const x = (offsetWidth - width) / 2;
|
|
209
|
+
const y = (offsetHeight - height) / 2;
|
|
210
|
+
return { width, height, x, y };
|
|
211
|
+
}
|
|
212
|
+
_computeCanvasSize(width = 0, height = 0) {
|
|
213
|
+
const scalefactor = this.prescaleFactor <= 0 ? 1 : this.prescaleFactor;
|
|
214
|
+
if (height <= 0 || width <= 0) {
|
|
215
|
+
width = 0;
|
|
216
|
+
height = 0;
|
|
217
|
+
} else {
|
|
218
|
+
const sgn = scalefactor < 1 ? -1 : 1;
|
|
219
|
+
let newH = height;
|
|
220
|
+
if (sgn * newH * scalefactor <= sgn * this.prescaleHeightLimit) {
|
|
221
|
+
newH *= scalefactor;
|
|
222
|
+
} else if (sgn * newH < sgn * this.prescaleHeightLimit) {
|
|
223
|
+
newH = this.prescaleHeightLimit;
|
|
224
|
+
}
|
|
225
|
+
if (this.maxRenderHeight > 0 && newH > this.maxRenderHeight)
|
|
226
|
+
newH = this.maxRenderHeight;
|
|
227
|
+
width *= newH / height;
|
|
228
|
+
height = newH;
|
|
229
|
+
}
|
|
230
|
+
return { width, height };
|
|
231
|
+
}
|
|
232
|
+
_timeupdate({ type }) {
|
|
233
|
+
const eventmap = {
|
|
234
|
+
seeking: true,
|
|
235
|
+
waiting: true,
|
|
236
|
+
playing: false
|
|
237
|
+
};
|
|
238
|
+
const playing = eventmap[type];
|
|
239
|
+
if (playing != null)
|
|
240
|
+
this._playstate = playing;
|
|
241
|
+
this.setCurrentTime(this._video.paused || this._playstate, this._video.currentTime + this.timeOffset);
|
|
242
|
+
}
|
|
243
|
+
setVideo(video) {
|
|
244
|
+
if (video instanceof HTMLVideoElement) {
|
|
245
|
+
this._removeListeners();
|
|
246
|
+
this._video = video;
|
|
247
|
+
if (this._onDemandRender) {
|
|
248
|
+
this._video.requestVideoFrameCallback(this._handleRVFC.bind(this));
|
|
249
|
+
} else {
|
|
250
|
+
this._playstate = video.paused;
|
|
251
|
+
video.addEventListener("timeupdate", this._boundTimeUpdate, false);
|
|
252
|
+
video.addEventListener("progress", this._boundTimeUpdate, false);
|
|
253
|
+
video.addEventListener("waiting", this._boundTimeUpdate, false);
|
|
254
|
+
video.addEventListener("seeking", this._boundTimeUpdate, false);
|
|
255
|
+
video.addEventListener("playing", this._boundTimeUpdate, false);
|
|
256
|
+
video.addEventListener("ratechange", this._boundSetRate, false);
|
|
257
|
+
}
|
|
258
|
+
if (video.videoWidth > 0)
|
|
259
|
+
this.resize();
|
|
260
|
+
video.addEventListener("resize", this._boundResize);
|
|
261
|
+
if (typeof ResizeObserver !== "undefined") {
|
|
262
|
+
if (!this._ro)
|
|
263
|
+
this._ro = new ResizeObserver(() => this.resize());
|
|
264
|
+
this._ro.observe(video);
|
|
265
|
+
}
|
|
266
|
+
} else {
|
|
267
|
+
this._error("Video element invalid!");
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
runBenchmark() {
|
|
271
|
+
this.sendMessage("runBenchmark");
|
|
272
|
+
}
|
|
273
|
+
setTrackByUrl(url) {
|
|
274
|
+
this.sendMessage("setTrackByUrl", { url });
|
|
275
|
+
}
|
|
276
|
+
setTrack(content) {
|
|
277
|
+
this.sendMessage("setTrack", { content });
|
|
278
|
+
}
|
|
279
|
+
freeTrack() {
|
|
280
|
+
this.sendMessage("freeTrack");
|
|
281
|
+
}
|
|
282
|
+
setIsPaused(isPaused) {
|
|
283
|
+
this.sendMessage("video", { isPaused });
|
|
284
|
+
}
|
|
285
|
+
setRate(rate) {
|
|
286
|
+
this.sendMessage("video", { rate });
|
|
287
|
+
}
|
|
288
|
+
setCurrentTime(isPaused, currentTime, rate) {
|
|
289
|
+
this.sendMessage("video", { isPaused, currentTime, rate });
|
|
290
|
+
}
|
|
291
|
+
createEvent(event) {
|
|
292
|
+
this.sendMessage("createEvent", { event });
|
|
293
|
+
}
|
|
294
|
+
setEvent(event, index) {
|
|
295
|
+
this.sendMessage("setEvent", { event, index });
|
|
296
|
+
}
|
|
297
|
+
removeEvent(index) {
|
|
298
|
+
this.sendMessage("removeEvent", { index });
|
|
299
|
+
}
|
|
300
|
+
getEvents(callback) {
|
|
301
|
+
this._fetchFromWorker({
|
|
302
|
+
target: "getEvents"
|
|
303
|
+
}, (err, { events }) => {
|
|
304
|
+
callback(err, events);
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
createStyle(style) {
|
|
308
|
+
this.sendMessage("createStyle", { style });
|
|
309
|
+
}
|
|
310
|
+
setStyle(event, index) {
|
|
311
|
+
this.sendMessage("setStyle", { event, index });
|
|
312
|
+
}
|
|
313
|
+
removeStyle(index) {
|
|
314
|
+
this.sendMessage("removeStyle", { index });
|
|
315
|
+
}
|
|
316
|
+
getStyles(callback) {
|
|
317
|
+
this._fetchFromWorker({
|
|
318
|
+
target: "getStyles"
|
|
319
|
+
}, (err, { styles }) => {
|
|
320
|
+
callback(err, styles);
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
addFont(font) {
|
|
324
|
+
this.sendMessage("addFont", { font });
|
|
325
|
+
}
|
|
326
|
+
_sendLocalFont(font) {
|
|
327
|
+
try {
|
|
328
|
+
queryLocalFonts().then((fontData) => {
|
|
329
|
+
const filtered = fontData && fontData.filter((obj) => obj.fullName.toLowerCase() === font);
|
|
330
|
+
if (filtered && filtered.length) {
|
|
331
|
+
filtered[0].blob().then((blob) => {
|
|
332
|
+
blob.arrayBuffer().then((buffer) => {
|
|
333
|
+
this.addFont(new Uint8Array(buffer));
|
|
334
|
+
});
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
} catch (e) {
|
|
339
|
+
console.warn("Local fonts API:", e);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
_getLocalFont({ font }) {
|
|
343
|
+
var _a;
|
|
344
|
+
try {
|
|
345
|
+
if ((_a = navigator == null ? void 0 : navigator.permissions) == null ? void 0 : _a.query) {
|
|
346
|
+
navigator.permissions.query({ name: "local-fonts" }).then((permission) => {
|
|
347
|
+
if (permission.state === "granted") {
|
|
348
|
+
this._sendLocalFont(font);
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
} else {
|
|
352
|
+
this._sendLocalFont(font);
|
|
353
|
+
}
|
|
354
|
+
} catch (e) {
|
|
355
|
+
console.warn("Local fonts API:", e);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
_unbusy() {
|
|
359
|
+
if (this._lastDemandTime) {
|
|
360
|
+
this._demandRender(this._lastDemandTime);
|
|
361
|
+
} else {
|
|
362
|
+
this.busy = false;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
_handleRVFC(now, { mediaTime }) {
|
|
366
|
+
if (this._destroyed)
|
|
367
|
+
return null;
|
|
368
|
+
if (this.busy) {
|
|
369
|
+
this._lastDemandTime = mediaTime;
|
|
370
|
+
} else {
|
|
371
|
+
this.busy = true;
|
|
372
|
+
this._demandRender(mediaTime);
|
|
373
|
+
}
|
|
374
|
+
this._video.requestVideoFrameCallback(this._handleRVFC.bind(this));
|
|
375
|
+
}
|
|
376
|
+
_demandRender(time) {
|
|
377
|
+
this._lastDemandTime = null;
|
|
378
|
+
this.sendMessage("demand", { time: time + this.timeOffset });
|
|
379
|
+
}
|
|
380
|
+
_render({ images, async, times }) {
|
|
381
|
+
const drawStartTime = Date.now();
|
|
382
|
+
this._ctx.clearRect(0, 0, this._canvasctrl.width, this._canvasctrl.height);
|
|
383
|
+
for (const image of images) {
|
|
384
|
+
if (image.image) {
|
|
385
|
+
if (async) {
|
|
386
|
+
this._ctx.drawImage(image.image, image.x, image.y);
|
|
387
|
+
image.image.close();
|
|
388
|
+
} else {
|
|
389
|
+
this._bufferCanvas.width = image.w;
|
|
390
|
+
this._bufferCanvas.height = image.h;
|
|
391
|
+
this._bufferCtx.putImageData(new ImageData(this._fixAlpha(new Uint8ClampedArray(image.image)), image.w, image.h), 0, 0);
|
|
392
|
+
this._ctx.drawImage(this._bufferCanvas, image.x, image.y);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
if (this.debug) {
|
|
397
|
+
times.drawTime = Date.now() - drawStartTime;
|
|
398
|
+
let total = 0;
|
|
399
|
+
for (const key in times)
|
|
400
|
+
total += times[key];
|
|
401
|
+
console.log("Bitmaps: " + images.length + " Total: " + Math.round(total) + "ms", times);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
_fixAlpha(uint8) {
|
|
405
|
+
if (_JASSUB._hasAlphaBug) {
|
|
406
|
+
for (let j = 3; j < uint8.length; j += 4) {
|
|
407
|
+
uint8[j] = uint8[j] > 1 ? uint8[j] : 1;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return uint8;
|
|
411
|
+
}
|
|
412
|
+
_ready() {
|
|
413
|
+
this.dispatchEvent(new CustomEvent("ready"));
|
|
414
|
+
}
|
|
415
|
+
sendMessage(target, data = {}, transferable) {
|
|
416
|
+
if (transferable) {
|
|
417
|
+
this._worker.postMessage({
|
|
418
|
+
target,
|
|
419
|
+
transferable,
|
|
420
|
+
...data
|
|
421
|
+
}, [...transferable]);
|
|
422
|
+
} else {
|
|
423
|
+
this._worker.postMessage({
|
|
424
|
+
target,
|
|
425
|
+
...data
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
_fetchFromWorker(workerOptions, callback) {
|
|
430
|
+
try {
|
|
431
|
+
const target = workerOptions.target;
|
|
432
|
+
const timeout = setTimeout(() => {
|
|
433
|
+
reject(new Error("Error: Timeout while try to fetch " + target));
|
|
434
|
+
}, 5e3);
|
|
435
|
+
const resolve = ({ data }) => {
|
|
436
|
+
if (data.target === target) {
|
|
437
|
+
callback(null, data);
|
|
438
|
+
this._worker.removeEventListener("message", resolve);
|
|
439
|
+
this._worker.removeEventListener("error", reject);
|
|
440
|
+
clearTimeout(timeout);
|
|
441
|
+
}
|
|
442
|
+
};
|
|
443
|
+
const reject = (event) => {
|
|
444
|
+
callback(event);
|
|
445
|
+
this._worker.removeEventListener("message", resolve);
|
|
446
|
+
this._worker.removeEventListener("error", reject);
|
|
447
|
+
clearTimeout(timeout);
|
|
448
|
+
};
|
|
449
|
+
this._worker.addEventListener("message", resolve);
|
|
450
|
+
this._worker.addEventListener("error", reject);
|
|
451
|
+
this._worker.postMessage(workerOptions);
|
|
452
|
+
} catch (error) {
|
|
453
|
+
this._error(error);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
_console({ content, command }) {
|
|
457
|
+
console[command].apply(console, JSON.parse(content));
|
|
458
|
+
}
|
|
459
|
+
_onmessage({ data }) {
|
|
460
|
+
if (this["_" + data.target])
|
|
461
|
+
this["_" + data.target](data);
|
|
462
|
+
}
|
|
463
|
+
_error(err) {
|
|
464
|
+
if (!(err instanceof ErrorEvent))
|
|
465
|
+
this.dispatchEvent(new ErrorEvent("error", { message: err instanceof Error ? err.cause : err }));
|
|
466
|
+
throw err instanceof Error ? err : new Error(err instanceof ErrorEvent ? err.message : "error", { cause: err });
|
|
467
|
+
}
|
|
468
|
+
_removeListeners() {
|
|
469
|
+
if (this._video) {
|
|
470
|
+
if (this._ro)
|
|
471
|
+
this._ro.unobserve(this._video);
|
|
472
|
+
this._video.removeEventListener("timeupdate", this._boundTimeUpdate);
|
|
473
|
+
this._video.removeEventListener("progress", this._boundTimeUpdate);
|
|
474
|
+
this._video.removeEventListener("waiting", this._boundTimeUpdate);
|
|
475
|
+
this._video.removeEventListener("seeking", this._boundTimeUpdate);
|
|
476
|
+
this._video.removeEventListener("playing", this._boundTimeUpdate);
|
|
477
|
+
this._video.removeEventListener("ratechange", this._boundSetRate);
|
|
478
|
+
this._video.removeEventListener("resize", this._boundResize);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
destroy(err) {
|
|
482
|
+
if (err)
|
|
483
|
+
this._error(err);
|
|
484
|
+
if (this._video)
|
|
485
|
+
this._video.parentNode.removeChild(this._canvasParent);
|
|
486
|
+
this._destroyed = true;
|
|
487
|
+
this._removeListeners();
|
|
488
|
+
this.sendMessage("destroy");
|
|
489
|
+
this._worker.terminate();
|
|
490
|
+
}
|
|
491
|
+
};
|
|
492
|
+
let JASSUB = _JASSUB;
|
|
493
|
+
__publicField(JASSUB, "_supportsWebAssembly", null);
|
|
494
|
+
__publicField(JASSUB, "_hasAlphaBug", null);
|
|
495
|
+
export { JASSUB as default };
|