@nmmty/lazycanvas
Version:
A simple way to interact with @napi-rs/canvas in an advanced way!
79 lines (78 loc) • 3.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Utils = void 0;
const components_1 = require("../structures/components");
const Utils = {
grid(size, opts) {
if (size.x === undefined || size.y === undefined) {
throw new Error("Size must have x and y properties");
}
if (opts === undefined)
opts = {};
if (opts.cellWith === undefined)
opts.cellWith = 10;
if (opts.cellHeight === undefined)
opts.cellHeight = 10;
if (opts.startX === undefined)
opts.startX = 0;
if (opts.startY === undefined)
opts.startY = 0;
if (opts.endX === undefined)
opts.endX = size.x;
if (opts.endY === undefined)
opts.endY = size.y;
if (opts.color === undefined)
opts.color = 'rgba(0, 0, 0, 0.5)';
if (opts.lineWidth === undefined)
opts.lineWidth = 1;
const options = { ...opts };
return new components_1.Group()
.setID(`grid-${options.cellWith}-${options.cellHeight}-${options.startX}-${options.startY}-${options.endX}-${options.endY}`)
.add(...Array.from({ length: Math.ceil((options.endX - options.startX) / options.cellWith) }, (_, i) => {
const x = options.startX + i * options.cellWith;
return new components_1.LineLayer()
.setPosition(x, options.startY)
.setEndPosition(x, options.endY)
.setColor(options.color)
.setStroke(options.lineWidth);
}), ...Array.from({ length: Math.ceil((options.endY - options.startY) / options.cellHeight) }, (_, i) => {
const y = options.startY + i * options.cellHeight;
return new components_1.LineLayer()
.setPosition(options.startX, y)
.setEndPosition(options.endX, y)
.setColor(options.color)
.setStroke(options.lineWidth);
}));
},
box(start, end, opts) {
if (start.x === undefined || start.y === undefined || end.x === undefined || end.y === undefined) {
throw new Error("Start and end must have x and y properties");
}
if (opts === undefined)
opts = {};
if (opts.color === undefined)
opts.color = 'rgba(0, 0, 0, 0.5)';
if (opts.lineWidth === undefined)
opts.lineWidth = 1;
return new components_1.Group()
.setID(`box-${start.x}-${start.y}-${end.x}-${end.y}`)
.add(new components_1.LineLayer()
.setPosition(start.x, start.y)
.setEndPosition(end.x, start.y)
.setColor(opts.color)
.setStroke(opts.lineWidth), new components_1.LineLayer()
.setPosition(end.x, start.y)
.setEndPosition(end.x, end.y)
.setColor(opts.color)
.setStroke(opts.lineWidth), new components_1.LineLayer()
.setPosition(end.x, end.y)
.setEndPosition(start.x, end.y)
.setColor(opts.color)
.setStroke(opts.lineWidth), new components_1.LineLayer()
.setPosition(start.x, end.y)
.setEndPosition(start.x, start.y)
.setColor(opts.color)
.setStroke(opts.lineWidth));
}
};
exports.Utils = Utils;