@nmmty/lazycanvas
Version:
A simple way to interact with @napi-rs/canvas in an advanced way!
186 lines (185 loc) • 7.29 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LazyCanvas = void 0;
const types_1 = require("../types");
const canvas_1 = require("@napi-rs/canvas");
const managers_1 = require("./managers");
const LazyUtil_1 = require("../utils/LazyUtil");
const utils_1 = require("../utils/utils");
/**
* Class representing a LazyCanvas, which provides a structured way to manage canvas rendering.
*/
class LazyCanvas {
/**
* The canvas instance, which can be either a Canvas or SvgCanvas.
*/
canvas;
/**
* The 2D rendering context of the canvas.
*/
ctx;
/**
* The manager object containing various managers for layers, rendering, fonts, and animation.
* The manager object containing various managers for layers, rendering, fonts, animation, and plugins.
*/
manager;
/**
* The options for configuring the LazyCanvas instance.
*/
options;
/**
* Constructs a new LazyCanvas instance.
* @param {Object} [opts] - Optional settings for the LazyCanvas instance.
* @param {boolean} [opts.debug] - Whether debugging is enabled.
* @param {IOLazyCanvas} [opts.settings] - The input settings for the LazyCanvas instance.
*/
constructor(opts) {
this.canvas = new canvas_1.Canvas(0, 0);
this.ctx = this.canvas.getContext('2d');
this.manager = {
layers: new managers_1.LayersManager(this, { debug: opts?.debug }),
render: new managers_1.RenderManager(this, { debug: opts?.debug }),
fonts: new managers_1.FontsManager({ debug: opts?.debug }),
animation: new managers_1.AnimationManager({ debug: opts?.debug, settings: { options: opts?.settings?.animation } }),
plugins: new managers_1.PluginManager(this, { debug: opts?.debug })
};
this.options = {
width: 0,
height: 0,
animated: false,
exportType: types_1.Export.BUFFER,
flag: canvas_1.SvgExportFlag.RelativePathEncoding,
...opts?.settings?.options
};
if (opts?.debug)
LazyUtil_1.LazyLog.log('info', 'LazyCanvas initialized with settings:', opts.settings);
}
/**
* Sets the export type for the canvas.
* @param {AnyExport} [type] - The export type (e.g., buffer, SVG, etc.).
* @returns {this} The current instance for chaining.
*/
setExportType(type) {
this.options.exportType = type;
switch (type) {
case types_1.Export.BUFFER:
this.canvas = new canvas_1.Canvas(this.options.width, this.options.height);
this.ctx = this.canvas.getContext('2d');
break;
case types_1.Export.CTX:
break;
case types_1.Export.SVG:
this.canvas = new canvas_1.Canvas(this.options.width, this.options.height, this.options.flag || canvas_1.SvgExportFlag.RelativePathEncoding);
this.ctx = this.canvas.getContext('2d');
break;
}
return this;
}
/**
* Sets the SVG export flag. This method should be called after `setExportType`.
* @param {SvgExportFlag} [flag] - The SVG export flag.
* @returns {this} The current instance for chaining.
*/
setSvgExportFlag(flag) {
if (this.options.exportType === types_1.Export.SVG) {
this.canvas = new canvas_1.Canvas(this.options.width, this.options.height, flag);
this.ctx = this.canvas.getContext('2d');
this.options.flag = flag;
}
return this;
}
/**
* Enables animation for the canvas.
* @returns {this} The current instance for chaining.
*/
animated() {
this.options.animated = true;
return this;
}
/**
* Resizes the canvas to the specified dimensions.
* @param {number} [ratio] - The ratio to resize the canvas.
* @returns {this} The current instance for chaining.
*/
resize(ratio) {
if (this.options.width <= 0 || this.options.height <= 0) {
throw new Error('Canvas dimensions are not set.');
}
this.options.width = (0, utils_1.resize)(this.options.width, ratio);
this.options.height = (0, utils_1.resize)(this.options.height, ratio);
if (this.options.exportType === types_1.Export.SVG) {
this.canvas = new canvas_1.Canvas(this.options.width, this.options.height, this.options.flag || canvas_1.SvgExportFlag.RelativePathEncoding);
}
else {
this.canvas = new canvas_1.Canvas(this.options.width, this.options.height);
}
this.ctx = this.canvas.getContext('2d');
const layers = (0, utils_1.resizeLayers)(this.manager.layers.toArray(), ratio);
this.manager.layers.fromArray(layers);
// Выполняем хук onResize для всех плагинов
this.manager.plugins.executeHook('onResize', this, ratio);
return this;
}
/**
* Creates a new canvas with the specified dimensions.
* @param {number} [width] - The width of the canvas.
* @param {number} [height] - The height of the canvas.
* @returns {this} The current instance for chaining.
*/
create(width, height) {
this.options.width = width;
this.options.height = height;
if (this.options.exportType === types_1.Export.SVG) {
this.canvas = new canvas_1.Canvas(width, height, this.options.flag || canvas_1.SvgExportFlag.RelativePathEncoding);
}
else {
this.canvas = new canvas_1.Canvas(width, height);
}
this.ctx = this.canvas.getContext('2d');
this.manager.layers = new managers_1.LayersManager(this, { debug: this.manager.layers.debug });
// Выполняем хук onCanvasCreated для всех плагинов
this.manager.plugins.executeHook('onCanvasCreated', this, width, height);
return this;
}
/**
* Installs a plugin to the canvas.
* @param {ILazyCanvasPlugin} [plugin] - The plugin to install.
* @returns {this} The current instance for chaining.
*/
use(plugin) {
this.manager.plugins.register(plugin);
return this;
}
/**
* Removes a plugin from the canvas.
* @param {string} [pluginName] - The name of the plugin to remove.
* @returns {this} The current instance for chaining.
*/
removePlugin(pluginName) {
this.manager.plugins.unregister(pluginName);
return this;
}
/**
* Gets a plugin by name.
* @param {string} [pluginName] - The name of the plugin.
* @returns {ILazyCanvasPlugin | undefined} The plugin or undefined if not found.
*/
getPlugin(pluginName) {
return this.manager.plugins.get(pluginName);
}
/**
* Lists all installed plugins.
* @returns {string[]} Array of plugin names.
*/
listPlugins() {
return this.manager.plugins.list();
}
/**
* Gets information about all installed plugins.
* @returns Array of plugin information objects.
*/
getPluginsInfo() {
return this.manager.plugins.getPluginInfo();
}
}
exports.LazyCanvas = LazyCanvas;