Move most functionality in the create methods into the BaseCanvasFactory

This *slightly* reduces the amount of code duplication in the `DOMCanvasFactory.create` and `NodeCanvasFactory.create` methods.
This commit is contained in:
Jonas Jenwald 2021-06-11 16:35:42 +02:00
parent 7b4fa0a038
commit d10b850916
3 changed files with 19 additions and 19 deletions

View file

@ -23,7 +23,14 @@ class BaseCanvasFactory {
}
create(width, height) {
unreachable("Abstract method `create` called.");
if (width <= 0 || height <= 0) {
throw new Error("Invalid canvas size");
}
const canvas = this._createCanvas(width, height);
return {
canvas,
context: canvas.getContext("2d"),
};
}
reset(canvasAndContext, width, height) {
@ -48,6 +55,13 @@ class BaseCanvasFactory {
canvasAndContext.canvas = null;
canvasAndContext.context = null;
}
/**
* @private
*/
_createCanvas(width, height) {
unreachable("Abstract method `_createCanvas` called.");
}
}
class BaseCMapReaderFactory {