UNPKG

illustrator.js

Version:

JavaScript image processing library

119 lines (118 loc) 4.59 kB
"use strict"; var _Layer_instances, _Layer_canvas, _Layer_ctx, _Layer_locked, _Layer_hidden, _Layer_toolHistory, _Layer_throwIfLocked; Object.defineProperty(exports, "__esModule", { value: true }); exports.Layer = void 0; const tslib_1 = require("tslib"); const canvas_1 = require("@napi-rs/canvas"); const ToolBox_1 = require("../toolbox/base/ToolBox"); const LayerTools_1 = require("./LayerTools"); const LayerUtils_1 = require("./LayerUtils"); class Layer { constructor(manager, id, options) { this.manager = manager; this.id = id; _Layer_instances.add(this); _Layer_canvas.set(this, void 0); _Layer_ctx.set(this, void 0); _Layer_locked.set(this, false); _Layer_hidden.set(this, false); _Layer_toolHistory.set(this, []); this.tools = new LayerTools_1.LayerTools(this); this.coordinates = { x: 0, y: 0 }; this.height = options?.height ?? this.manager.illustrator.height; this.width = options?.width ?? this.manager.illustrator.width; tslib_1.__classPrivateFieldSet(this, _Layer_canvas, (0, canvas_1.createCanvas)(this.width, this.height), "f"); tslib_1.__classPrivateFieldSet(this, _Layer_ctx, tslib_1.__classPrivateFieldGet(this, _Layer_canvas, "f").getContext("2d"), "f"); this.utils = new LayerUtils_1.LayerUtils(tslib_1.__classPrivateFieldGet(this, _Layer_ctx, "f")); } get name() { return this.manager.resolve(this)?.name; } get illustrator() { return this.manager.illustrator; } get context() { return tslib_1.__classPrivateFieldGet(this, _Layer_ctx, "f"); } get position() { return this.manager.getLayerPosition(this); } setPosition(position) { return this.manager.setLayerPosition(this, position); } createTransformation(data) { if (data.coordinates) this.coordinates = data.coordinates; if (data.width) this.width = data.width; if (data.height) this.height = data.height; } get locked() { return tslib_1.__classPrivateFieldGet(this, _Layer_locked, "f"); } isLocked() { return this.locked; } lock() { tslib_1.__classPrivateFieldSet(this, _Layer_locked, true, "f"); return this; } unlock() { tslib_1.__classPrivateFieldSet(this, _Layer_locked, false, "f"); return this; } isHidden() { return this.hidden; } get hidden() { return tslib_1.__classPrivateFieldGet(this, _Layer_hidden, "f"); } hide() { tslib_1.__classPrivateFieldSet(this, _Layer_hidden, true, "f"); return this; } show() { tslib_1.__classPrivateFieldSet(this, _Layer_hidden, false, "f"); return this; } save() { tslib_1.__classPrivateFieldGet(this, _Layer_ctx, "f").save(); } restore() { tslib_1.__classPrivateFieldGet(this, _Layer_ctx, "f").restore(); } duplicate(name = `${this.name} Copy`) { return this.manager.duplicateLayer(this, tslib_1.__classPrivateFieldGet(this, _Layer_toolHistory, "f"), { name, config: { height: this.height, width: this.width }, position: this.position + 1 }); } applyTool(tool) { tslib_1.__classPrivateFieldGet(this, _Layer_instances, "m", _Layer_throwIfLocked).call(this); if (!(tool instanceof ToolBox_1.ToolBox)) throw new Error("tool must be a ToolBox instance"); tslib_1.__classPrivateFieldGet(this, _Layer_toolHistory, "f").push(tool.history); } setHistory(history) { tslib_1.__classPrivateFieldSet(this, _Layer_toolHistory, history, "f"); } async render() { if (tslib_1.__classPrivateFieldGet(this, _Layer_hidden, "f")) return null; await Promise.all(tslib_1.__classPrivateFieldGet(this, _Layer_toolHistory, "f").flat(2).map((m) => m(tslib_1.__classPrivateFieldGet(this, _Layer_ctx, "f")))); return tslib_1.__classPrivateFieldGet(this, _Layer_canvas, "f"); } } exports.Layer = Layer; _Layer_canvas = new WeakMap(), _Layer_ctx = new WeakMap(), _Layer_locked = new WeakMap(), _Layer_hidden = new WeakMap(), _Layer_toolHistory = new WeakMap(), _Layer_instances = new WeakSet(), _Layer_throwIfLocked = function _Layer_throwIfLocked() { if (tslib_1.__classPrivateFieldGet(this, _Layer_locked, "f")) throw new Error("Cannot perform operations on locked layer"); };