Skip to content

Commit 7ee061b

Browse files
committed
Add a helper function, in web/pdf_thumbnail_view.js, for "zeroing" a canvas
This removes a tiny bit of code duplication.
1 parent b7eef92 commit 7ee061b

1 file changed

Lines changed: 11 additions & 14 deletions

File tree

web/pdf_thumbnail_view.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ const DRAW_UPSCALE_FACTOR = 2; // See comment in `PDFThumbnailView.draw` below.
3131
const MAX_NUM_SCALING_STEPS = 3;
3232
const THUMBNAIL_WIDTH = 98; // px
3333

34+
function zeroCanvas(c) {
35+
// Zeroing the width and height causes Firefox to release graphics
36+
// resources immediately, which can greatly reduce memory consumption.
37+
c.width = 0;
38+
c.height = 0;
39+
}
40+
3441
/**
3542
* @typedef {Object} PDFThumbnailViewOptions
3643
* @property {HTMLDivElement} container - The viewer element.
@@ -74,12 +81,8 @@ class TempImageFactory {
7481
}
7582

7683
static destroyCanvas() {
77-
const tempCanvas = this.#tempCanvas;
78-
if (tempCanvas) {
79-
// Zeroing the width and height causes Firefox to release graphics
80-
// resources immediately, which can greatly reduce memory consumption.
81-
tempCanvas.width = 0;
82-
tempCanvas.height = 0;
84+
if (this.#tempCanvas) {
85+
zeroCanvas(this.#tempCanvas);
8386
}
8487
this.#tempCanvas = null;
8588
}
@@ -255,10 +258,7 @@ class PDFThumbnailView {
255258
this.div.setAttribute("data-loaded", true);
256259
this._placeholderImg.replaceWith(image);
257260

258-
// Zeroing the width and height causes Firefox to release graphics
259-
// resources immediately, which can greatly reduce memory consumption.
260-
reducedCanvas.width = 0;
261-
reducedCanvas.height = 0;
261+
zeroCanvas(reducedCanvas);
262262
}
263263

264264
async #finishRenderTask(renderTask, canvas, error = null) {
@@ -331,10 +331,7 @@ class PDFThumbnailView {
331331
error => this.#finishRenderTask(renderTask, canvas, error)
332332
);
333333
resultPromise.finally(() => {
334-
// Zeroing the width and height causes Firefox to release graphics
335-
// resources immediately, which can greatly reduce memory consumption.
336-
canvas.width = 0;
337-
canvas.height = 0;
334+
zeroCanvas(canvas);
338335

339336
this.eventBus.dispatch("thumbnailrendered", {
340337
source: this,

0 commit comments

Comments
 (0)