Skip to content

Commit 345d701

Browse files
committed
Add ability to limit canvas size
1 parent 825c999 commit 345d701

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

src/subtitles-octopus.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@ var SubtitlesOctopus = function (options) {
1414
var self = this;
1515
self.canvas = options.canvas; // HTML canvas element (optional if video specified)
1616
self.renderMode = options.renderMode || (options.lossyRender ? 'fast' : (options.blendRender ? 'blend' : 'normal'));
17+
18+
// play with those when you need some speed, e.g. for slow devices
1719
self.dropAllAnimations = options.dropAllAnimations || false;
1820
self.libassMemoryLimit = options.libassMemoryLimit || 0; // set libass bitmap cache memory limit in MiB (approximate)
1921
self.libassGlyphLimit = options.libassGlyphLimit || 0; // set libass glyph cache memory limit in MiB (approximate)
2022
self.targetFps = options.targetFps || 30;
23+
self.prescaleTradeoff = options.prescaleTradeoff || 1.0; // render subtitles less than viewport when less than 1.0 to improve speed, render to more than 1.0 to improve quality
24+
self.softHeightLimit = options.softHeightLimit || 1080; // don't apply prescaleTradeoff < 1 when viewport height is less that this limit
25+
self.hardHeightLimit = options.hardHeightLimit || 1600; // don't ever go above this limit
26+
2127
self.renderAhead = options.renderAhead || 0; // how many MiB to render ahead and store; 0 to disable (approximate)
2228
self.isOurCanvas = false; // (internal) we created canvas and manage it
2329
self.video = options.video; // HTML video element (optional if canvas specified)
@@ -667,14 +673,43 @@ var SubtitlesOctopus = function (options) {
667673
}
668674
};
669675

676+
function _computeCanvasSize(width, height) {
677+
if (self.prescaleTradeoff > 1) {
678+
if (height * self.prescaleTradeoff <= self.softHeightLimit) {
679+
width *= self.prescaleTradeoff;
680+
height *= self.prescaleTradeoff;
681+
} else if (height < self.softHeightLimit) {
682+
width = width * self.softHeightLimit / height;
683+
height = self.softHeightLimit;
684+
} else if (height >= self.hardHeightLimit) {
685+
width = width * self.hardHeightLimit / height;
686+
height = self.hardHeightLimit;
687+
}
688+
} else if (height >= self.softHeightLimit) {
689+
if (height * self.prescaleTradeoff <= self.softHeightLimit) {
690+
width = width * self.softHeightLimit / height;
691+
height = self.softHeightLimit;
692+
} else if (height * self.prescaleTradeoff <= self.hardHeightLimit) {
693+
width *= self.prescaleTradeoff;
694+
height *= self.prescaleTradeoff;
695+
} else {
696+
width = width * self.hardHeightLimit / height;
697+
height = self.hardHeightLimit;
698+
}
699+
}
700+
701+
return {'width': width, 'height': height};
702+
}
703+
670704
self.resize = function (width, height, top, left) {
671705
var videoSize = null;
672706
top = top || 0;
673707
left = left || 0;
674708
if ((!width || !height) && self.video) {
675709
videoSize = self.getVideoPosition();
676-
width = videoSize.width * self.pixelRatio;
677-
height = videoSize.height * self.pixelRatio;
710+
var newsize = _computeCanvasSize(videoSize.width * self.pixelRatio, videoSize.height * self.pixelRatio);
711+
width = newsize.width;
712+
height = newsize.height;
678713
var offset = self.canvasParent.getBoundingClientRect().top - self.video.getBoundingClientRect().top;
679714
top = videoSize.y - offset;
680715
left = videoSize.x;

0 commit comments

Comments
 (0)