@nmmty/lazycanvas
Version:
A simple way to interact with @napi-rs/canvas in an advanced way!
77 lines (76 loc) • 2.73 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LazyCanvas = void 0;
const enum_1 = require("../types/enum");
const canvas_1 = require("@napi-rs/canvas");
const LayersManager_1 = require("./managers/LayersManager");
const RenderManager_1 = require("./managers/RenderManager");
const FontsManager_1 = require("./managers/FontsManager");
const AnimationManager_1 = require("./managers/AnimationManager");
class LazyCanvas {
width;
height;
canvas;
ctx;
layers;
render;
fonts;
animation;
exportType;
constructor(debug = false) {
this.width = 0;
this.height = 0;
this.canvas = new canvas_1.Canvas(0, 0);
this.ctx = this.canvas.getContext('2d');
this.layers = new LayersManager_1.LayersManager(debug);
this.render = new RenderManager_1.RenderManager(this, debug);
this.fonts = new FontsManager_1.FontsManager(debug);
this.animation = new AnimationManager_1.AnimationManager(debug);
this.exportType = enum_1.Export.Buffer;
}
/**
* Set the export type
* @param type {AnyExport} - The `export` type
*/
setExportType(type) {
this.exportType = type;
switch (type) {
case enum_1.Export.Buffer:
this.canvas = new canvas_1.Canvas(this.width, this.height);
this.ctx = this.canvas.getContext('2d');
break;
case enum_1.Export.CTX:
break;
case enum_1.Export.SVG:
this.canvas = new canvas_1.Canvas(this.width, this.height, canvas_1.SvgExportFlag.RelativePathEncoding);
this.ctx = this.canvas.getContext('2d');
break;
}
return this;
}
/**
* Set the SVG export flag. This method should be called after `setExportType` method.
* @param flag {SvgExportFlag} - The `flag` of the SVG export
*/
setSvgExportFlag(flag) {
if (this.exportType === enum_1.Export.SVG) {
this.canvas = new canvas_1.Canvas(this.width, this.height, flag);
this.ctx = this.canvas.getContext('2d');
}
return this;
}
/**
* Create a new canvas. This method should be called before any other methods.
* @param width {number} - The `width` of the canvas
* @param height {number} - The `height` of the canvas
*/
create(width, height) {
this.width = width;
this.height = height;
this.canvas = new canvas_1.Canvas(width, height);
this.ctx = this.canvas.getContext('2d');
this.layers = new LayersManager_1.LayersManager();
return this;
}
}
exports.LazyCanvas = LazyCanvas;