@nmmty/lazycanvas
Version:
A simple way to interact with @napi-rs/canvas in an advanced way!
119 lines (118 loc) • 4.88 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LineLayer = void 0;
const BaseLayer_1 = require("./BaseLayer");
const enum_1 = require("../../types/enum");
const LazyUtil_1 = require("../../utils/LazyUtil");
const utils_1 = require("../../utils/utils");
const Gradient_1 = require("../helpers/Gradient");
const Pattern_1 = require("../helpers/Pattern");
class LineLayer extends BaseLayer_1.BaseLayer {
props;
constructor(props) {
super(enum_1.LayerType.Line, props || {});
this.props = props ? props : {};
if (!this.props.fillStyle)
this.props.fillStyle = '#000000';
this.props.centring = enum_1.Centring.None;
}
/**
* @description Sets the end point of the line layer. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
* @param x {ScaleType} - The end `x` of the line layer.
* @param y {ScaleType} - The end `y` of the line layer.
*/
setEndPosition(x, y) {
this.props.endPoint = { x, y };
return this;
}
/**
* @description Sets the color of the layer. You can use `hex`, `rgb`, `rgba`, `hsl`, `hsla`, and `Gradient`color.
* @param color {string} - The `color` of the layer.
*/
setColor(color) {
if (!color)
throw new LazyUtil_1.LazyError('The color of the layer must be provided');
if (!(0, utils_1.isColor)(color))
throw new LazyUtil_1.LazyError('The color of the layer must be a valid color');
let fill = (0, utils_1.parseColor)(color);
if (fill instanceof Gradient_1.Gradient || fill instanceof Pattern_1.Pattern) {
this.props.fillStyle = fill;
}
else {
let arr = fill.split(':');
this.props.fillStyle = arr[0];
this.props.opacity = parseFloat(arr[1]) || 1;
}
return this;
}
/**
* @description Sets the stroke of the layer.
* @param width {number} - The `width` of the stroke.
* @param cap {string} - The `cap` of the stroke.
* @param join {string} - The `join` of the stroke.
* @param dash {number[]} - The `dash` of the stroke.
* @param dashOffset {number} - The `dashOffset` of the stroke.
* @param miterLimit {number} - The `miterLimit` of the stroke.
*/
setStroke(width, cap, join, dash, dashOffset, miterLimit) {
this.props.stroke = {
width,
cap: cap || 'butt',
join: join || 'miter',
dash: dash || [],
dashOffset: dashOffset || 0,
miterLimit: miterLimit || 10,
};
return this;
}
getBoundingBox(ctx, canvas, manager) {
const parcer = (0, utils_1.parser)(ctx, canvas, manager);
const { xs, ys, xe, ye } = parcer.parseBatch({
xs: { v: this.props.x },
ys: { v: this.props.y, options: LazyUtil_1.defaultArg.vl(true) },
xe: { v: this.props.endPoint.x },
ye: { v: this.props.endPoint.y, options: LazyUtil_1.defaultArg.vl(true) },
});
let width = xe - xs;
let height = ye - ys;
return { xs, ys, xe, ye, width, height };
}
async draw(ctx, canvas, manager, debug) {
const parcer = (0, utils_1.parser)(ctx, canvas, manager);
const { xs, ys, xe, ye } = parcer.parseBatch({
xs: { v: this.props.x },
ys: { v: this.props.y, options: LazyUtil_1.defaultArg.vl(true) },
xe: { v: this.props.endPoint.x },
ye: { v: this.props.endPoint.y, options: LazyUtil_1.defaultArg.vl(true) },
});
let width = xe - xs;
let height = ye - ys;
let fillStyle = await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle);
if (debug)
LazyUtil_1.LazyLog.log('none', `LineLayer:`, { xs, ys, xe, ye, width, height });
ctx.save();
(0, utils_1.transform)(ctx, this.props.transform, { x: xs, y: ys, width, height, type: this.type });
(0, utils_1.drawShadow)(ctx, this.props.shadow);
(0, utils_1.opacity)(ctx, this.props.opacity);
(0, utils_1.filters)(ctx, this.props.filter);
ctx.beginPath();
ctx.moveTo(xs, ys);
ctx.strokeStyle = fillStyle;
ctx.lineWidth = this.props.stroke?.width || 1;
ctx.lineCap = this.props.stroke?.cap || 'butt';
ctx.lineJoin = this.props.stroke?.join || 'miter';
ctx.miterLimit = this.props.stroke?.miterLimit || 10;
ctx.lineDashOffset = this.props.stroke?.dashOffset || 0;
ctx.setLineDash(this.props.stroke?.dash || []);
ctx.lineTo(xe, ye);
ctx.stroke();
ctx.closePath();
ctx.restore();
}
toJSON() {
let data = super.toJSON();
data.props = this.props;
return { ...data };
}
}
exports.LineLayer = LineLayer;