Add a helper function, in web/pdf_thumbnail_view.js, for "zeroing" a canvas

This removes a tiny bit of code duplication.
This commit is contained in:
Jonas Jenwald 2025-03-15 13:24:52 +01:00
parent b7eef925ac
commit 7ee061bcf1

View File

@ -31,6 +31,13 @@ const DRAW_UPSCALE_FACTOR = 2; // See comment in `PDFThumbnailView.draw` below.
const MAX_NUM_SCALING_STEPS = 3; const MAX_NUM_SCALING_STEPS = 3;
const THUMBNAIL_WIDTH = 98; // px const THUMBNAIL_WIDTH = 98; // px
function zeroCanvas(c) {
// Zeroing the width and height causes Firefox to release graphics
// resources immediately, which can greatly reduce memory consumption.
c.width = 0;
c.height = 0;
}
/** /**
* @typedef {Object} PDFThumbnailViewOptions * @typedef {Object} PDFThumbnailViewOptions
* @property {HTMLDivElement} container - The viewer element. * @property {HTMLDivElement} container - The viewer element.
@ -74,12 +81,8 @@ class TempImageFactory {
} }
static destroyCanvas() { static destroyCanvas() {
const tempCanvas = this.#tempCanvas; if (this.#tempCanvas) {
if (tempCanvas) { zeroCanvas(this.#tempCanvas);
// Zeroing the width and height causes Firefox to release graphics
// resources immediately, which can greatly reduce memory consumption.
tempCanvas.width = 0;
tempCanvas.height = 0;
} }
this.#tempCanvas = null; this.#tempCanvas = null;
} }
@ -255,10 +258,7 @@ class PDFThumbnailView {
this.div.setAttribute("data-loaded", true); this.div.setAttribute("data-loaded", true);
this._placeholderImg.replaceWith(image); this._placeholderImg.replaceWith(image);
// Zeroing the width and height causes Firefox to release graphics zeroCanvas(reducedCanvas);
// resources immediately, which can greatly reduce memory consumption.
reducedCanvas.width = 0;
reducedCanvas.height = 0;
} }
async #finishRenderTask(renderTask, canvas, error = null) { async #finishRenderTask(renderTask, canvas, error = null) {
@ -331,10 +331,7 @@ class PDFThumbnailView {
error => this.#finishRenderTask(renderTask, canvas, error) error => this.#finishRenderTask(renderTask, canvas, error)
); );
resultPromise.finally(() => { resultPromise.finally(() => {
// Zeroing the width and height causes Firefox to release graphics zeroCanvas(canvas);
// resources immediately, which can greatly reduce memory consumption.
canvas.width = 0;
canvas.height = 0;
this.eventBus.dispatch("thumbnailrendered", { this.eventBus.dispatch("thumbnailrendered", {
source: this, source: this,