jassub 1.2.3 → 1.3.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/dist/jassub.es.js CHANGED
@@ -53,6 +53,8 @@ const _JASSUB = class extends EventTarget {
53
53
  this._onDemandRender = "requestVideoFrameCallback" in HTMLVideoElement.prototype && ((_c = options.onDemandRender) != null ? _c : true);
54
54
  this.timeOffset = options.timeOffset || 0;
55
55
  this._video = options.video;
56
+ this._videoHeight = 0;
57
+ this._videoWidth = 0;
56
58
  this._canvas = options.canvas;
57
59
  if (this._video && !this._canvas) {
58
60
  this._canvasParent = document.createElement("div");
@@ -87,8 +89,8 @@ const _JASSUB = class extends EventTarget {
87
89
  target: "init",
88
90
  asyncRender,
89
91
  onDemandRender: this._onDemandRender,
90
- width: this._canvas.width,
91
- height: this._canvas.height,
92
+ width: this._canvasctrl.width,
93
+ height: this._canvasctrl.height,
92
94
  preMain: true,
93
95
  blendMode,
94
96
  subUrl: options.subUrl,
@@ -160,49 +162,36 @@ const _JASSUB = class extends EventTarget {
160
162
  canvas1.remove();
161
163
  canvas2.remove();
162
164
  }
163
- resize(width = 0, height = 0, top = 0, left = 0) {
164
- let videoSize = null;
165
+ resize(width = 0, height = 0, top = 0, left = 0, force = ((_a) => (_a = this._video) == null ? void 0 : _a.paused)()) {
165
166
  if ((!width || !height) && this._video) {
166
- videoSize = this._getVideoPosition();
167
- const newsize = this._computeCanvasSize((videoSize.width || 0) * (self.devicePixelRatio || 1), (videoSize.height || 0) * (self.devicePixelRatio || 1));
168
- width = newsize.width;
169
- height = newsize.height;
167
+ const videoSize = this._getVideoPosition();
168
+ let renderSize = null;
169
+ if (this._videoWidth) {
170
+ const widthRatio = this._video.videoWidth / this._videoWidth;
171
+ const heightRatio = this._video.videoHeight / this._videoHeight;
172
+ renderSize = this._computeCanvasSize((videoSize.width || 0) / widthRatio, (videoSize.height || 0) / heightRatio);
173
+ } else {
174
+ renderSize = this._computeCanvasSize(videoSize.width || 0, videoSize.height || 0);
175
+ }
176
+ width = renderSize.width;
177
+ height = renderSize.height;
170
178
  if (this._canvasParent) {
171
179
  top = videoSize.y - (this._canvasParent.getBoundingClientRect().top - this._video.getBoundingClientRect().top);
172
180
  left = videoSize.x;
173
181
  }
174
- }
175
- if (videoSize != null) {
176
- this._canvas.style.top = top + "px";
177
- this._canvas.style.left = left + "px";
178
182
  this._canvas.style.width = videoSize.width + "px";
179
183
  this._canvas.style.height = videoSize.height + "px";
180
184
  }
181
- if (!(this._canvasctrl.width === width && this._canvasctrl.height === height)) {
182
- if (this._resizeTimeoutBuffer) {
183
- clearTimeout(this._resizeTimeoutBuffer);
184
- this._resizeTimeoutBuffer = setTimeout(() => {
185
- this._resizeTimeoutBuffer = void 0;
186
- this._canvasctrl.width = width;
187
- this._canvasctrl.height = height;
188
- this.sendMessage("canvas", { width, height });
189
- }, 100);
190
- } else {
191
- this._canvasctrl.width = width;
192
- this._canvasctrl.height = height;
193
- this.sendMessage("canvas", { width, height });
194
- this._resizeTimeoutBuffer = setTimeout(() => {
195
- this._resizeTimeoutBuffer = void 0;
196
- }, 100);
197
- }
198
- }
185
+ this._canvas.style.top = top + "px";
186
+ this._canvas.style.left = left + "px";
187
+ this.sendMessage("canvas", { width, height, force: force && this.busy === false });
199
188
  }
200
- _getVideoPosition() {
201
- const videoRatio = this._video.videoWidth / this._video.videoHeight;
189
+ _getVideoPosition(width = this._video.videoWidth, height = this._video.videoHeight) {
190
+ const videoRatio = width / height;
202
191
  const { offsetWidth, offsetHeight } = this._video;
203
192
  const elementRatio = offsetWidth / offsetHeight;
204
- let width = offsetWidth;
205
- let height = offsetHeight;
193
+ width = offsetWidth;
194
+ height = offsetHeight;
206
195
  if (elementRatio > videoRatio) {
207
196
  width = Math.floor(offsetHeight * videoRatio);
208
197
  } else {
@@ -214,12 +203,13 @@ const _JASSUB = class extends EventTarget {
214
203
  }
215
204
  _computeCanvasSize(width = 0, height = 0) {
216
205
  const scalefactor = this.prescaleFactor <= 0 ? 1 : this.prescaleFactor;
206
+ const ratio = self.devicePixelRatio || 1;
217
207
  if (height <= 0 || width <= 0) {
218
208
  width = 0;
219
209
  height = 0;
220
210
  } else {
221
211
  const sgn = scalefactor < 1 ? -1 : 1;
222
- let newH = height;
212
+ let newH = height * ratio;
223
213
  if (sgn * newH * scalefactor <= sgn * this.prescaleHeightLimit) {
224
214
  newH *= scalefactor;
225
215
  } else if (sgn * newH < sgn * this.prescaleHeightLimit) {
@@ -227,7 +217,7 @@ const _JASSUB = class extends EventTarget {
227
217
  }
228
218
  if (this.maxRenderHeight > 0 && newH > this.maxRenderHeight)
229
219
  newH = this.maxRenderHeight;
230
- width *= newH / height;
220
+ width *= ratio * newH / height;
231
221
  height = newH;
232
222
  }
233
223
  return { width, height };
@@ -257,10 +247,10 @@ const _JASSUB = class extends EventTarget {
257
247
  video.addEventListener("seeking", this._boundTimeUpdate, false);
258
248
  video.addEventListener("playing", this._boundTimeUpdate, false);
259
249
  video.addEventListener("ratechange", this._boundSetRate, false);
250
+ video.addEventListener("resize", this._boundResize);
260
251
  }
261
252
  if (video.videoWidth > 0)
262
253
  this.resize();
263
- video.addEventListener("resize", this._boundResize);
264
254
  if (typeof ResizeObserver !== "undefined") {
265
255
  if (!this._ro)
266
256
  this._ro = new ResizeObserver(() => this.resize());
@@ -365,24 +355,35 @@ const _JASSUB = class extends EventTarget {
365
355
  this.busy = false;
366
356
  }
367
357
  }
368
- _handleRVFC(now, { mediaTime }) {
358
+ _handleRVFC(now, { mediaTime, width, height }) {
369
359
  if (this._destroyed)
370
360
  return null;
371
361
  if (this.busy) {
372
- this._lastDemandTime = mediaTime;
362
+ this._lastDemandTime = { mediaTime, width, height };
373
363
  } else {
374
364
  this.busy = true;
375
- this._demandRender(mediaTime);
365
+ this._demandRender({ mediaTime, width, height });
376
366
  }
377
367
  this._video.requestVideoFrameCallback(this._handleRVFC.bind(this));
378
368
  }
379
- _demandRender(time) {
369
+ _demandRender({ mediaTime, width, height }) {
380
370
  this._lastDemandTime = null;
381
- this.sendMessage("demand", { time: time + this.timeOffset });
371
+ if (width !== this._videoWidth || height !== this._videoHeight) {
372
+ this._videoWidth = width;
373
+ this._videoHeight = height;
374
+ this.resize();
375
+ }
376
+ this.sendMessage("demand", { time: mediaTime + this.timeOffset });
382
377
  }
383
- _render({ images, async, times }) {
378
+ _render({ images, async, times, width, height }) {
379
+ this._unbusy();
384
380
  const drawStartTime = Date.now();
385
- this._ctx.clearRect(0, 0, this._canvasctrl.width, this._canvasctrl.height);
381
+ if (this._canvasctrl.width !== width || this._canvasctrl.height !== height) {
382
+ this._canvasctrl.width = width;
383
+ this._canvasctrl.height = height;
384
+ } else {
385
+ this._ctx.clearRect(0, 0, this._canvasctrl.width, this._canvasctrl.height);
386
+ }
386
387
  for (const image of images) {
387
388
  if (image.image) {
388
389
  if (async) {
@@ -464,9 +465,15 @@ const _JASSUB = class extends EventTarget {
464
465
  this["_" + data.target](data);
465
466
  }
466
467
  _error(err) {
467
- if (!(err instanceof ErrorEvent))
468
- this.dispatchEvent(new ErrorEvent("error", { message: err instanceof Error ? err.cause : err }));
469
- throw err instanceof Error ? err : new Error(err instanceof ErrorEvent ? err.message : "error", { cause: err });
468
+ this.dispatchEvent(err instanceof ErrorEvent ? err : new ErrorEvent("error", { cause: err instanceof Error ? err.cause : err }));
469
+ if (!(err instanceof Error)) {
470
+ if (err instanceof ErrorEvent) {
471
+ err = err.error;
472
+ } else {
473
+ err = new Error("error", { cause: err });
474
+ }
475
+ }
476
+ console.error(err);
470
477
  }
471
478
  _removeListeners() {
472
479
  if (this._video) {