@nmmty/lazycanvas
Version:
A simple way to interact with @napi-rs/canvas in an advanced way!
92 lines (91 loc) • 2.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClearLayer = void 0;
const enum_1 = require("../../types/enum");
const utils_1 = require("../../utils/utils");
const LazyUtil_1 = require("../../utils/LazyUtil");
const utils_2 = require("../../utils/utils");
class ClearLayer {
id;
type;
zIndex;
visible;
props;
constructor(props) {
this.id = (0, utils_2.generateID)(enum_1.LayerType.Clear);
this.type = enum_1.LayerType.Clear;
this.zIndex = 1;
this.visible = true;
this.props = props ? props : {
x: 0,
y: 0,
size: {
width: 0,
height: 0
}
};
}
/**
* @description Position of the layer in the 2D plane. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
* @param x {ScaleType} - The `x` position of the layer
* @param y {ScaleType} - The `y` position of the layer
*/
setPosition(x, y) {
this.props.x = x;
this.props.y = y;
return this;
}
/**
* @description Sets the size of the layer.
* @param width {ScaleType} - The `width` of the layer
* @param height {ScaleType} - The `height` of the
*/
setSize(width, height) {
this.props.size = {
width,
height
};
return this;
}
/**
* @description Sets the visibility of the layer.
* @param visible {boolean} - The `visibility` of the layer
*/
setVisible(visible) {
this.visible = visible;
return this;
}
/**
* @description Sets zIndex of the layer.
* @param zIndex {number} - The `zIndex` of the layer
*/
setZIndex(zIndex) {
this.zIndex = zIndex;
return this;
}
async draw(ctx, canvas, manager, debug) {
const parcer = (0, utils_1.parser)(ctx, canvas, manager);
const { x, y, w } = parcer.parseBatch({
x: { v: this.props.x },
y: { 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));
if (debug)
LazyUtil_1.LazyLog.log('none', `ClearLayer:`, { x, y, w, h });
ctx.clearRect(x, y, w, h);
}
/**
* @returns {IClearLayer}
*/
toJSON() {
return {
id: this.id,
type: this.type,
zIndex: this.zIndex,
visible: this.visible,
props: this.props,
};
}
}
exports.ClearLayer = ClearLayer;