@nmmty/lazycanvas
Version:
A simple way to interact with @napi-rs/canvas in an advanced way!
153 lines (152 loc) • 4.79 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClearLayer = void 0;
const types_1 = require("../../types");
const utils_1 = require("../../utils/utils");
const LazyUtil_1 = require("../../utils/LazyUtil");
const utils_2 = require("../../utils/utils");
/**
* Class representing a Clear Layer.
*/
class ClearLayer {
/**
* The unique identifier of the layer.
*/
id;
/**
* The type of the layer, which is `Clear`.
*/
type = types_1.LayerType.Clear;
/**
* The z-index of the layer, determining its stacking order.
*/
zIndex;
/**
* The visibility of the layer.
*/
visible;
/**
* The properties of the Clear Layer.
*/
props;
/**
* Constructs a new ClearLayer instance.
* @param props {IClearLayerProps} - The properties of the Clear Layer.
* @param misc {IBaseLayerMisc} - Miscellaneous options for the layer.
*/
constructor(props, misc) {
this.id = misc?.id || (0, utils_2.generateID)(types_1.LayerType.Clear);
this.type = types_1.LayerType.Clear;
this.zIndex = misc?.zIndex || 1;
this.visible = misc?.visible || true;
this.props = props ? props : {
x: 0,
y: 0,
size: {
width: 0,
height: 0
}
};
}
/**
* Sets the position of the layer in the 2D plane.
* @param x {ScaleType} - The x-coordinate of the layer.
* @param y {ScaleType} - The y-coordinate of the layer.
* @returns {this} The current instance for chaining.
*/
setPosition(x, y) {
this.props.x = x;
this.props.y = y;
return this;
}
/**
* Sets the size of the layer.
* @param width {ScaleType} - The width of the layer.
* @param height {ScaleType} - The height of the layer.
* @returns {this} The current instance for chaining.
*/
setSize(width, height) {
this.props.size = {
width,
height
};
return this;
}
/**
* Sets the unique identifier of the layer.
*
* @param {string} id - The unique identifier.
* @returns {this} The current instance for chaining.
*/
setID(id) {
this.id = id;
return this;
}
/**
* Sets the centring type of the layer.
* @param centring {AnyCentring} - The centring type of the layer.
* @returns {this} The current instance for chaining.
*/
setCentring(centring) {
this.props.centring = centring;
return this;
}
/**
* Sets the visibility of the layer.
* @param visible {boolean} - The visibility state of the layer.
* @returns {this} The current instance for chaining.
*/
setVisible(visible) {
this.visible = visible;
return this;
}
/**
* Sets the z-index of the layer.
* @param zIndex {number} - The z-index value of the layer.
* @returns {this} The current instance for chaining.
*/
setZIndex(zIndex) {
this.zIndex = zIndex;
return this;
}
/**
* Draws the Clear Layer on the canvas.
* @param ctx {SKRSContext2D} - The canvas rendering context.
* @param canvas {Canvas | SvgCanvas} - The canvas instance.
* @param manager {LayersManager} - The layers manager.
* @param debug {boolean} - Whether to enable debug logging.
*/
async draw(ctx, canvas, manager, debug) {
const parcer = (0, utils_1.parser)(ctx, canvas, manager);
const { xs, ys, w } = parcer.parseBatch({
xs: { v: this.props.x },
ys: { v: this.props.y, options: LazyUtil_1.defaultArg.vl(true) },
w: { v: this.props.size.width },
});
const h = parcer.parse(this.props.size.height, LazyUtil_1.defaultArg.wh(w), LazyUtil_1.defaultArg.vl(true));
let { x, y } = (0, utils_1.centring)(this.props.centring, this.type, w, h, xs, ys);
if (debug)
LazyUtil_1.LazyLog.log('none', `ClearLayer:`, { x, y, w, h });
ctx.clearRect(x, y, w, h);
}
/**
* Converts the Clear Layer to a JSON representation.
* @returns {IClearLayer} The JSON representation of the Clear Layer.
*/
toJSON() {
let copy = { ...this.props };
for (const key of ['x', 'y', 'size.width', 'size.height']) {
if (copy[key] && typeof copy[key] === 'object' && 'toJSON' in copy[key]) {
copy[key] = copy[key].toJSON();
}
}
return {
id: this.id,
type: this.type,
zIndex: this.zIndex,
visible: this.visible,
props: copy,
};
}
}
exports.ClearLayer = ClearLayer;