Skip to content

Commit 2b1e507

Browse files
committed
js: fix subtitles disappear when resizing
1. Missing `px` in offset comparison - deferred resizing resets the canvas by writing the same values to `width` and `height`. [turuslan <turuslan.devbox@gmail.com>] found this bug. 2. Since RenderAhead resizes the canvas according to the rendered frame, the native properties will be overwritten, and we need additional properties for comparison.
1 parent cad4581 commit 2b1e507

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/subtitles-octopus.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ var SubtitlesOctopus = function (options) {
6363

6464
self.hasAlphaBug = false;
6565

66+
// private
67+
var targetWidth; // Width of render target
68+
var targetHeight; // Height of render target
69+
6670
(function() {
6771
if (typeof ImageData.prototype.constructor === 'function') {
6872
try {
@@ -119,6 +123,10 @@ var SubtitlesOctopus = function (options) {
119123
self.createCanvas();
120124
self.setVideo(options.video);
121125
self.setSubUrl(options.subUrl);
126+
127+
targetWidth = self.canvas.width;
128+
targetHeight = self.canvas.height;
129+
122130
self.worker.postMessage({
123131
target: 'worker-init',
124132
width: self.canvas.width,
@@ -742,26 +750,22 @@ var SubtitlesOctopus = function (options) {
742750
return;
743751
}
744752

753+
if (videoSize != null) {
754+
self.canvasParent.style.position = 'relative';
755+
self.canvas.style.display = 'block';
756+
self.canvas.style.position = 'absolute';
757+
self.canvas.style.width = videoSize.width + 'px';
758+
self.canvas.style.height = videoSize.height + 'px';
759+
self.canvas.style.top = top + 'px';
760+
self.canvas.style.left = left + 'px';
761+
self.canvas.style.pointerEvents = 'none';
762+
}
745763

746-
if (
747-
self.canvas.width != width ||
748-
self.canvas.height != height ||
749-
self.canvas.style.top != top ||
750-
self.canvas.style.left != left
751-
) {
764+
if (targetWidth !== width || targetHeight !== height) {
752765
self.canvas.width = width;
753766
self.canvas.height = height;
754-
755-
if (videoSize != null) {
756-
self.canvasParent.style.position = 'relative';
757-
self.canvas.style.display = 'block';
758-
self.canvas.style.position = 'absolute';
759-
self.canvas.style.width = videoSize.width + 'px';
760-
self.canvas.style.height = videoSize.height + 'px';
761-
self.canvas.style.top = top + 'px';
762-
self.canvas.style.left = left + 'px';
763-
self.canvas.style.pointerEvents = 'none';
764-
}
767+
targetWidth = width;
768+
targetHeight = height;
765769

766770
self.worker.postMessage({
767771
target: 'canvas',

0 commit comments

Comments
 (0)