@nmmty/lazycanvas
Version:
A simple way to interact with @napi-rs/canvas in an advanced way!
96 lines (95 loc) • 2.4 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Group = void 0;
const enum_1 = require("../../types/enum");
const utils_1 = require("../../utils/utils");
class Group {
id;
visible;
zIndex;
components;
constructor() {
this.id = (0, utils_1.generateID)(enum_1.LayerType.Group);
this.visible = true;
this.zIndex = 1;
this.components = [];
}
/**
* Set the ID of the group
* @param id {string} - The `id` of the group
*/
setID(id) {
this.id = id;
return this;
}
/**
* Set the visibility of the group
* @param visible {boolean} - The `visibility` of the group
*/
setVisible(visible) {
this.visible = visible;
return this;
}
/**
* Set the zIndex of the group
* @param zIndex {number} - The `zIndex` of the group
*/
setZIndex(zIndex) {
this.zIndex = zIndex;
return this;
}
/**
* Add a component to the group
* @param components {any[]} - The `components` to add to the group
*/
add(...components) {
let layersArray = components.flat();
layersArray = layersArray.filter(l => l !== undefined);
layersArray = layersArray.sort((a, b) => a.zIndex - b.zIndex);
this.components.push(...layersArray);
return this;
}
/**
* Remove a component from the group
* @param id {any} - The `id` of the component to remove
*/
remove(id) {
this.components = this.components.filter(c => c.id !== id);
return this;
}
/**
* ClearLayer all components from the group
*/
clear() {
this.components = [];
return this;
}
/**
* Get a component from the group
* @param id {string} - The `id` of the component to get
*/
get(id) {
return this.components.find(c => c.id === id);
}
/**
* Get all components from the group
*/
getAll() {
return this.components;
}
/**
* Get the length of the components
*/
get length() {
return this.components.length;
}
toJSON() {
return {
id: this.id,
visible: this.visible,
zIndex: this.zIndex,
components: this.components.map(c => c.toJSON())
};
}
}
exports.Group = Group;