UNPKG

@nmmty/lazycanvas

Version:

A simple way to interact with @napi-rs/canvas in an advanced way!

169 lines (168 loc) 6.94 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Exporter = void 0; const types_1 = require("../../types"); const LazyUtil_1 = require("../../utils/LazyUtil"); const fs = __importStar(require("fs")); const utils_1 = require("../../utils/utils"); const _yaml = __importStar(require("js-yaml")); /** * Class responsible for exporting a LazyCanvas instance to various formats. */ class Exporter { /** * The LazyCanvas instance to be exported. */ canvas; /** * Constructs a new Exporter instance. * @param canvas {LazyCanvas} - The LazyCanvas instance to be exported. */ constructor(canvas) { this.canvas = canvas; } /** * Saves a file to the filesystem. * @param buffer {any} - The data to be saved. * @param extension {Extensions} - The file extension. * @param name {string} - The name of the file (optional). * @throws {LazyError} If the buffer or extension is not provided. */ async saveFile(buffer, extension, name) { if (!buffer) throw new LazyUtil_1.LazyError('Buffer must be provided'); if (!extension) throw new LazyUtil_1.LazyError('Extension must be provided'); fs.writeFileSync(`${name === undefined ? (0, utils_1.generateRandomName)() : name}.${extension}`, buffer); } /** * Exports all layers from the LayersManager as an array of JSON objects. * @param manager {LayersManager} - The LayersManager instance. * @returns {any[]} An array of JSON representations of the layers. */ exportLayers(manager) { let arr = []; for (const layer of Array.from(manager.map.values())) { arr.push(layer.toJSON()); } return arr; } /** * Exports the canvas to the specified format. * @param exportType {AnyExport} - The type of export (e.g., "png", "json"). * @param opts {Object} - Optional settings. * @param opts.name {string} - The name of the file (optional). * @param opts.saveAsFile {boolean} - Whether to save the export as a file (optional). * @returns {Promise<Buffer | SKRSContext2D | Canvas | SvgCanvas | string>} The exported data. * @throws {LazyError} If the export type is not supported. */ async export(exportType, opts) { switch (exportType) { case types_1.Export.CTX: case "ctx": return await this.canvas.manager.render.render(exportType); case types_1.Export.SVG: case "svg": const svg = await this.canvas.manager.render.render('svg'); if (opts?.saveAsFile) { await this.saveFile(svg, 'svg', opts.name); } return svg; case types_1.Export.BUFFER: case "buffer": const buffer = await this.canvas.manager.render.render('buffer'); if (opts?.saveAsFile) { await this.saveFile(buffer, 'png', opts.name); } return buffer; case types_1.Export.GIF: case "gif": const gif = await this.canvas.manager.render.render('buffer'); if (opts?.saveAsFile) { await this.saveFile(gif, 'gif', opts.name); } return gif; case types_1.Export.WEBP: case "webp": const webp = await this.canvas.manager.render.render('buffer'); if (opts?.saveAsFile) { await this.saveFile(webp, 'webp', opts.name); } return webp; case types_1.Export.JPEG: case "jpeg": const jpeg = await this.canvas.manager.render.render('buffer'); await this.saveFile(jpeg, 'jpeg', opts?.name); return jpeg; case types_1.Export.JPG: case "jpg": const jpg = await this.canvas.manager.render.render('buffer'); await this.saveFile(jpg, 'jpg', opts?.name); return jpg; case types_1.Export.PNG: case "png": const png = await this.canvas.manager.render.render('buffer'); await this.saveFile(png, 'png', opts?.name); return png; case types_1.Export.JSON: case "json": const json = this.syncExport(exportType); if (opts?.saveAsFile) { await this.saveFile(JSON.stringify(json), 'json', opts.name); } return JSON.stringify(json); case types_1.Export.CANVAS: case "canvas": return await this.canvas.manager.render.render(exportType); case types_1.Export.YAML: case "yaml": const yaml = _yaml.dump(this.syncExport(types_1.Export.JSON)); if (opts?.saveAsFile) { await this.saveFile(yaml, 'yaml', opts.name); } return yaml; default: throw new LazyUtil_1.LazyError(`Export type ${exportType} is not supported`); } } /** * Synchronously exports the canvas to the specified format. * @param exportType {AnyExport} - The type of export (e.g., "json"). * @returns {IOLazyCanvas | void} The exported data or void if the export type is unsupported. */ syncExport(exportType) { switch (exportType) { case types_1.Export.JSON: case "json": return { options: this.canvas.options, animation: this.canvas.manager.animation.options, layers: this.exportLayers(this.canvas.manager.layers) }; } } } exports.Exporter = Exporter;