Convert TempImageFactory to a class, using static fields/methods

Please refer to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields
This commit is contained in:
Jonas Jenwald 2021-10-17 13:42:21 +02:00
parent 89785a23f3
commit 7216511fbf

View File

@ -36,48 +36,42 @@ const THUMBNAIL_WIDTH = 98; // px
* @property {IL10n} l10n - Localization service. * @property {IL10n} l10n - Localization service.
*/ */
const TempImageFactory = (function TempImageFactoryClosure() { class TempImageFactory {
let tempCanvasCache = null; static #tempCanvas = null;
return { static getCanvas(width, height) {
getCanvas(width, height) { const tempCanvas = (this.#tempCanvas ||= document.createElement("canvas"));
let tempCanvas = tempCanvasCache; tempCanvas.width = width;
if (!tempCanvas) { tempCanvas.height = height;
tempCanvas = document.createElement("canvas");
tempCanvasCache = tempCanvas;
}
tempCanvas.width = width;
tempCanvas.height = height;
// Since this is a temporary canvas, we need to fill it with a white // Since this is a temporary canvas, we need to fill it with a white
// background ourselves. `_getPageDrawContext` uses CSS rules for this. // background ourselves. `_getPageDrawContext` uses CSS rules for this.
if ( if (
typeof PDFJSDev === "undefined" || typeof PDFJSDev === "undefined" ||
PDFJSDev.test("MOZCENTRAL || GENERIC") PDFJSDev.test("MOZCENTRAL || GENERIC")
) { ) {
tempCanvas.mozOpaque = true; tempCanvas.mozOpaque = true;
} }
const ctx = tempCanvas.getContext("2d", { alpha: false }); const ctx = tempCanvas.getContext("2d", { alpha: false });
ctx.save(); ctx.save();
ctx.fillStyle = "rgb(255, 255, 255)"; ctx.fillStyle = "rgb(255, 255, 255)";
ctx.fillRect(0, 0, width, height); ctx.fillRect(0, 0, width, height);
ctx.restore(); ctx.restore();
return [tempCanvas, tempCanvas.getContext("2d")]; return [tempCanvas, tempCanvas.getContext("2d")];
}, }
destroyCanvas() { static destroyCanvas() {
const tempCanvas = tempCanvasCache; const tempCanvas = this.#tempCanvas;
if (tempCanvas) { if (tempCanvas) {
// Zeroing the width and height causes Firefox to release graphics // Zeroing the width and height causes Firefox to release graphics
// resources immediately, which can greatly reduce memory consumption. // resources immediately, which can greatly reduce memory consumption.
tempCanvas.width = 0; tempCanvas.width = 0;
tempCanvas.height = 0; tempCanvas.height = 0;
} }
tempCanvasCache = null; this.#tempCanvas = null;
}, }
}; }
})();
/** /**
* @implements {IRenderableView} * @implements {IRenderableView}