UNPKG

@nmmty/lazycanvas

Version:

A simple way to interact with @napi-rs/canvas in an advanced way!

165 lines (164 loc) 6.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MorphLayer = void 0; const BaseLayer_1 = require("./BaseLayer"); const types_1 = require("../../types"); const utils_1 = require("../../utils/utils"); const LazyUtil_1 = require("../../utils/LazyUtil"); const helpers_1 = require("../helpers"); /** * Class representing a Morph Layer, extending the BaseLayer class. */ class MorphLayer extends BaseLayer_1.BaseLayer { /** * The properties of the Morph Layer. */ props; /** * Constructs a new MorphLayer instance. * @param props {IMorphLayerProps} - The properties of the Morph Layer. * @param misc {IBaseLayerMisc} - Miscellaneous options for the layer. */ constructor(props, misc) { super(types_1.LayerType.Morph, props || {}, misc); this.props = props ? props : {}; if (!this.props.fillStyle) this.props.fillStyle = '#000000'; if (!this.props.filled && this.props.filled !== false) this.props.filled = true; this.props.centring = types_1.Centring.Center; } /** * Sets the size of the Morph Layer. * @param width {ScaleType} - The width of the Morph Layer. * @param height {ScaleType} - The height of the Morph Layer. * @param radius {{ [corner in radiusCorner]?: ScaleType }} - The radius of the Morph Layer (optional). * @returns {this} The current instance for chaining. */ setSize(width, height, radius) { this.props.size = { width: width, height: height, radius: radius || { all: 0 }, }; return this; } /** * Sets the color of the Morph Layer. * @param color {string} - The color of the Morph Layer. * @returns {this} The current instance for chaining. * @throws {LazyError} If the color is not provided or invalid. */ 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'); this.props.fillStyle = color; return this; } /** * Sets the stroke properties of the Morph Layer. * @param width {number} - The width of the stroke. * @param cap {string} - The cap style of the stroke. * @param join {string} - The join style of the stroke. * @param dash {number[]} - The dash pattern of the stroke. * @param dashOffset {number} - The dash offset of the stroke. * @param miterLimit {number} - The miter limit of the stroke. * @returns {this} The current instance for chaining. */ 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; } /** * Sets whether the Morph Layer should be filled or stroked. * @param filled {boolean} - If true, the layer will be filled; otherwise, it will be stroked. * @returns {this} The current instance for chaining. */ setFilled(filled) { this.props.filled = filled; return this; } /** * Draws the Morph 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)); const rad = {}; if (typeof this.props.size.radius === 'object' && this.props.size.radius !== helpers_1.Link) { for (const corner in this.props.size.radius) { // @ts-ignore rad[corner] = parcer.parse(this.props.size.radius[corner], LazyUtil_1.defaultArg.wh(w / 2, h / 2), LazyUtil_1.defaultArg.vl(false, true)); } } let { x, y } = (0, utils_1.centring)(this.props.centring, this.type, w, h, xs, ys); let fillStyle = await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle); if (debug) LazyUtil_1.LazyLog.log('none', `MorphLayer:`, { x, y, w, h, rad }); ctx.save(); (0, utils_1.transform)(ctx, this.props.transform, { width: w, height: h, x, y, type: this.type }); ctx.beginPath(); if (Object.keys(rad).length > 0) { ctx.moveTo(x + (w / 2), y); ctx.arcTo(x + w, y, x + w, y + (h / 2), rad.rightTop || rad.all || 0); ctx.arcTo(x + w, y + h, x + (w / 2), y + h, rad.rightBottom || rad.all || 0); ctx.arcTo(x, y + h, x, y + (h / 2), rad.leftBottom || rad.all || 0); ctx.arcTo(x, y, x + (w / 2), y, rad.leftTop || rad.all || 0); } else { ctx.rect(x, y, w, h); } ctx.closePath(); (0, utils_1.drawShadow)(ctx, this.props.shadow); (0, utils_1.opacity)(ctx, this.props.opacity); (0, utils_1.filters)(ctx, this.props.filter); if (this.props.filled) { ctx.fillStyle = fillStyle; ctx.fill(); } else { 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.stroke(); } ctx.restore(); } /** * Converts the Morph Layer to a JSON representation. * @returns {IMorphLayer} The JSON representation of the Morph Layer. */ toJSON() { let data = super.toJSON(); let copy = { ...this.props }; for (const key of ['x', 'y', 'size.width', 'size.height', 'size.radius', 'fillStyle']) { if (copy[key] && typeof copy[key] === 'object' && 'toJSON' in copy[key]) { copy[key] = copy[key].toJSON(); } } return { ...data, props: copy }; } } exports.MorphLayer = MorphLayer;