@nmmty/lazycanvas 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/dist/helpers/Filters.d.ts +10 -10
  2. package/dist/helpers/Fonts.d.ts +1 -1
  3. package/dist/helpers/FontsList.d.ts +1 -1
  4. package/dist/helpers/FontsList.js +19 -19
  5. package/dist/index.d.ts +11 -20
  6. package/dist/index.js +40 -47
  7. package/dist/structures/LazyCanvas.d.ts +126 -19
  8. package/dist/structures/LazyCanvas.js +100 -35
  9. package/dist/structures/components/BaseLayer.d.ts +188 -38
  10. package/dist/structures/components/BaseLayer.js +88 -41
  11. package/dist/structures/components/BezierLayer.d.ts +108 -21
  12. package/dist/structures/components/BezierLayer.js +73 -24
  13. package/dist/structures/components/ClearLayer.d.ts +120 -17
  14. package/dist/structures/components/ClearLayer.js +83 -22
  15. package/dist/structures/components/Group.d.ts +86 -18
  16. package/dist/structures/components/Group.js +69 -29
  17. package/dist/structures/components/ImageLayer.d.ts +85 -12
  18. package/dist/structures/components/ImageLayer.js +52 -39
  19. package/dist/structures/components/LineLayer.d.ts +111 -18
  20. package/dist/structures/components/LineLayer.js +58 -21
  21. package/dist/structures/components/MorphLayer.d.ts +109 -21
  22. package/dist/structures/components/MorphLayer.js +53 -25
  23. package/dist/structures/components/Path2DLayer.d.ts +191 -0
  24. package/dist/structures/components/Path2DLayer.js +318 -0
  25. package/dist/structures/components/QuadraticLayer.d.ts +108 -22
  26. package/dist/structures/components/QuadraticLayer.js +65 -30
  27. package/dist/structures/components/TextLayer.d.ts +201 -40
  28. package/dist/structures/components/TextLayer.js +99 -47
  29. package/dist/structures/components/index.d.ts +10 -0
  30. package/dist/structures/components/index.js +26 -0
  31. package/dist/structures/helpers/Exporter.d.ts +52 -0
  32. package/dist/structures/helpers/Exporter.js +168 -0
  33. package/dist/structures/helpers/Font.d.ts +64 -10
  34. package/dist/structures/helpers/Font.js +38 -11
  35. package/dist/structures/helpers/Gradient.d.ts +96 -9
  36. package/dist/structures/helpers/Gradient.js +48 -17
  37. package/dist/structures/helpers/Link.d.ts +52 -8
  38. package/dist/structures/helpers/Link.js +42 -11
  39. package/dist/structures/helpers/Pattern.d.ts +52 -7
  40. package/dist/structures/helpers/Pattern.js +45 -40
  41. package/dist/structures/helpers/index.d.ts +6 -0
  42. package/dist/structures/helpers/index.js +22 -0
  43. package/dist/structures/helpers/readers/JSONReader.d.ts +49 -0
  44. package/dist/structures/helpers/readers/JSONReader.js +172 -0
  45. package/dist/structures/helpers/readers/SVGReader.d.ts +20 -0
  46. package/dist/structures/helpers/readers/SVGReader.js +577 -0
  47. package/dist/structures/helpers/readers/YAMLReader.d.ts +0 -0
  48. package/dist/structures/helpers/readers/YAMLReader.js +1 -0
  49. package/dist/structures/managers/AnimationManager.d.ts +96 -20
  50. package/dist/structures/managers/AnimationManager.js +54 -26
  51. package/dist/structures/managers/FontsManager.d.ts +76 -32
  52. package/dist/structures/managers/FontsManager.js +70 -45
  53. package/dist/structures/managers/LayersManager.d.ts +84 -32
  54. package/dist/structures/managers/LayersManager.js +66 -28
  55. package/dist/structures/managers/RenderManager.d.ts +60 -6
  56. package/dist/structures/managers/RenderManager.js +120 -40
  57. package/dist/types/enum.d.ts +11 -6
  58. package/dist/types/enum.js +17 -12
  59. package/dist/types/index.d.ts +2 -19
  60. package/dist/types/index.js +17 -0
  61. package/dist/utils/LazyUtil.js +2 -2
  62. package/dist/utils/utils.d.ts +10 -9
  63. package/dist/utils/utils.js +159 -164
  64. package/package.json +4 -5
@@ -0,0 +1,318 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Path2DLayer = void 0;
4
+ const canvas_1 = require("@napi-rs/canvas");
5
+ const types_1 = require("../../types");
6
+ const utils_1 = require("../../utils/utils");
7
+ const LazyUtil_1 = require("../../utils/LazyUtil");
8
+ const helpers_1 = require("../helpers");
9
+ class Path2DLayer {
10
+ id;
11
+ type = types_1.LayerType.Path;
12
+ zIndex;
13
+ visible;
14
+ props;
15
+ constructor(props, misc) {
16
+ this.id = misc?.id || (0, utils_1.generateID)(types_1.LayerType.Path);
17
+ this.zIndex = misc?.zIndex || 1;
18
+ this.visible = misc?.visible || true;
19
+ this.props = {
20
+ path2D: props?.path2D || new canvas_1.Path2D(),
21
+ filter: props?.filter || "",
22
+ opacity: props?.opacity || 1,
23
+ filled: props?.filled || true,
24
+ fillStyle: props?.fillStyle || "#000000",
25
+ transform: props?.transform || {},
26
+ globalComposite: props?.globalComposite || "source-over",
27
+ stroke: {
28
+ width: props?.stroke?.width || 1,
29
+ cap: props?.stroke?.cap || "butt",
30
+ join: props?.stroke?.join || "miter",
31
+ dashOffset: props?.stroke?.dashOffset || 0,
32
+ dash: props?.stroke?.dash || [],
33
+ miterLimit: props?.stroke?.miterLimit || 10,
34
+ },
35
+ loadFromSVG: props?.loadFromSVG || false,
36
+ clipPath: props?.clipPath || false,
37
+ };
38
+ }
39
+ /**
40
+ * Sets the unique identifier of the layer.
41
+ *
42
+ * @param {string} id - The unique identifier.
43
+ * @returns {this} The current instance for chaining.
44
+ */
45
+ setID(id) {
46
+ this.id = id;
47
+ return this;
48
+ }
49
+ /**
50
+ * Sets the visibility of the layer.
51
+ * @param visible {boolean} - The visibility state of the layer.
52
+ * @returns {this} The current instance for chaining.
53
+ */
54
+ setVisible(visible) {
55
+ this.visible = visible;
56
+ return this;
57
+ }
58
+ /**
59
+ * Sets the z-index of the layer.
60
+ * @param zIndex {number} - The z-index value of the layer.
61
+ * @returns {this} The current instance for chaining.
62
+ */
63
+ setZIndex(zIndex) {
64
+ this.zIndex = zIndex;
65
+ return this;
66
+ }
67
+ /**
68
+ * Sets the global composite operation for the layer.
69
+ * @param {AnyGlobalCompositeOperation} operation - The composite operation.
70
+ * @returns {this} The current instance for chaining.
71
+ */
72
+ setGlobalCompositeOperation(operation) {
73
+ this.props.globalComposite = operation;
74
+ return this;
75
+ }
76
+ /**
77
+ * Sets the filter effects for the layer.
78
+ * @param {...AnyFilter} filter - The filter effects to apply.
79
+ * @returns {this} The current instance for chaining.
80
+ */
81
+ setFilters(...filter) {
82
+ this.props.filter = filter.join(' ');
83
+ return this;
84
+ }
85
+ /**
86
+ * Sets the transformation matrix of the layer.
87
+ * @param {DOMMatrix2DInit} matrix - The transformation matrix.
88
+ * @returns {this} The current instance for chaining.
89
+ */
90
+ setMatrix(matrix) {
91
+ this.props.transform = { ...this.props.transform, matrix };
92
+ return this;
93
+ }
94
+ /**
95
+ * Sets the scale of the layer in the x and y directions.
96
+ * @param {number} x - The scale factor in the x direction.
97
+ * @param {number} y - The scale factor in the y direction.
98
+ * @returns {this} The current instance for chaining.
99
+ */
100
+ setScale(x, y) {
101
+ this.props.transform = { ...this.props.transform, scale: { x, y } };
102
+ return this;
103
+ }
104
+ /**
105
+ * Sets the translation of the layer in the x and y directions.
106
+ * @param {number} x - The translation in the x direction.
107
+ * @param {number} y - The translation in the y direction.
108
+ * @returns {this} The current instance for chaining.
109
+ */
110
+ setTranslate(x, y) {
111
+ this.props.transform = { ...this.props.transform, translate: { x, y } };
112
+ return this;
113
+ }
114
+ /**
115
+ * Sets the opacity of the layer.
116
+ * @param {number} opacity - The opacity value, between 0 and 1.
117
+ * @returns {this} The current instance for chaining.
118
+ */
119
+ setOpacity(opacity) {
120
+ this.props.opacity = opacity;
121
+ return this;
122
+ }
123
+ /**
124
+ * Sets the stroke properties of the Path2D Layer.
125
+ * @param width {number} - The width of the stroke.
126
+ * @param cap {string} - The cap style of the stroke.
127
+ * @param join {string} - The join style of the stroke.
128
+ * @param dash {number[]} - The dash pattern of the stroke.
129
+ * @param dashOffset {number} - The dash offset of the stroke.
130
+ * @param miterLimit {number} - The miter limit of the stroke.
131
+ * @returns {this} The current instance for chaining.
132
+ */
133
+ setStroke(width, cap, join, dash, dashOffset, miterLimit) {
134
+ this.props.stroke = {
135
+ width,
136
+ cap: cap || 'butt',
137
+ join: join || 'miter',
138
+ dash: dash || [],
139
+ dashOffset: dashOffset || 0,
140
+ miterLimit: miterLimit || 10,
141
+ };
142
+ return this;
143
+ }
144
+ /**
145
+ * Sets whether the Path2D Layer should be filled or stroked.
146
+ * @param filled {boolean} - If true, the layer will be filled; otherwise, it will be stroked.
147
+ * @returns {this} The current instance for chaining.
148
+ */
149
+ setFilled(filled) {
150
+ this.props.filled = filled;
151
+ return this;
152
+ }
153
+ /**
154
+ * Sets the color of the Path2D Layer.
155
+ * @param color {string} - The color of the Path2D Layer.
156
+ * @returns {this} The current instance for chaining.
157
+ * @throws {LazyError} If the color is not provided or invalid.
158
+ */
159
+ setColor(color) {
160
+ if (!color)
161
+ throw new LazyUtil_1.LazyError('The color of the layer must be provided');
162
+ if (!(0, utils_1.isColor)(color))
163
+ throw new LazyUtil_1.LazyError('The color of the layer must be a valid color');
164
+ let fill = (0, utils_1.parseColor)(color);
165
+ if (fill instanceof helpers_1.Gradient || fill instanceof helpers_1.Pattern) {
166
+ this.props.fillStyle = fill;
167
+ }
168
+ else {
169
+ let arr = fill.split(':');
170
+ this.props.fillStyle = arr[0];
171
+ this.props.opacity = parseFloat(arr[1]) || 1;
172
+ }
173
+ return this;
174
+ }
175
+ setPath(path) {
176
+ this.props.path2D = path instanceof canvas_1.Path2D ? path : new canvas_1.Path2D(path);
177
+ return this;
178
+ }
179
+ loadFromSVG(path) {
180
+ this.props.loadFromSVG = path;
181
+ return this;
182
+ }
183
+ setClipPath(clipPath) {
184
+ this.props.clipPath = clipPath;
185
+ return this;
186
+ }
187
+ toSVGString() {
188
+ return this.props.path2D.toSVGString();
189
+ }
190
+ addPath(path, transform) {
191
+ this.props.path2D.addPath(path, transform);
192
+ return this;
193
+ }
194
+ arc(x, y, radius, startAngle, endAngle, anticlockwise) {
195
+ this.props.path2D.arc(x, y, radius, startAngle, endAngle, anticlockwise);
196
+ return this;
197
+ }
198
+ arcTo(x1, y1, x2, y2, radius) {
199
+ this.props.path2D.arcTo(x1, y1, x2, y2, radius);
200
+ return this;
201
+ }
202
+ bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) {
203
+ this.props.path2D.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
204
+ return this;
205
+ }
206
+ closePath() {
207
+ this.props.path2D.closePath();
208
+ return this;
209
+ }
210
+ ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise) {
211
+ this.props.path2D.ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise);
212
+ return this;
213
+ }
214
+ lineTo(x, y) {
215
+ this.props.path2D.lineTo(x, y);
216
+ return this;
217
+ }
218
+ moveTo(x, y) {
219
+ this.props.path2D.moveTo(x, y);
220
+ return this;
221
+ }
222
+ quadraticCurveTo(cpx, cpy, x, y) {
223
+ this.props.path2D.quadraticCurveTo(cpx, cpy, x, y);
224
+ return this;
225
+ }
226
+ rect(x, y, width, height) {
227
+ this.props.path2D.rect(x, y, width, height);
228
+ return this;
229
+ }
230
+ stroke(stroke) {
231
+ this.props.path2D.stroke(stroke);
232
+ return this;
233
+ }
234
+ op(path, op) {
235
+ this.props.path2D.op(path, op);
236
+ return this;
237
+ }
238
+ getFillType() {
239
+ return this.props.path2D.getFillType();
240
+ }
241
+ getFillTypeString() {
242
+ return this.props.path2D.getFillTypeString();
243
+ }
244
+ setFillType(fillType) {
245
+ this.props.path2D.setFillType(fillType);
246
+ return this;
247
+ }
248
+ simplify() {
249
+ this.props.path2D.simplify();
250
+ return this;
251
+ }
252
+ asWinding() {
253
+ this.props.path2D.asWinding();
254
+ return this;
255
+ }
256
+ transform(matrix) {
257
+ this.props.path2D.transform(matrix);
258
+ return this;
259
+ }
260
+ getBounds() {
261
+ return this.props.path2D.getBounds();
262
+ }
263
+ computeTightBounds() {
264
+ return this.props.path2D.computeTightBounds();
265
+ }
266
+ trim(start, end, isComplement) {
267
+ this.props.path2D.trim(start, end, isComplement);
268
+ return this;
269
+ }
270
+ equals(path) {
271
+ return this.props.path2D.equals(path.props.path2D);
272
+ }
273
+ roundRect(x, y, width, height, radius) {
274
+ this.props.path2D.roundRect(x, y, width, height, radius);
275
+ return this;
276
+ }
277
+ async draw(ctx, canvas, manager, debug) {
278
+ ctx.beginPath();
279
+ ctx.save();
280
+ let fillStyle = await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle);
281
+ (0, utils_1.transform)(ctx, this.props.transform, { width: 0, height: 0, x: 0, y: 0, type: this.type });
282
+ (0, utils_1.opacity)(ctx, this.props.opacity);
283
+ ctx.globalCompositeOperation = this.props.globalComposite;
284
+ if (this.props.clipPath) {
285
+ ctx.clip(this.props.path2D);
286
+ }
287
+ else if (this.props.filled) {
288
+ ctx.fillStyle = fillStyle;
289
+ ctx.fill(this.props.path2D);
290
+ }
291
+ else {
292
+ ctx.strokeStyle = fillStyle;
293
+ ctx.lineWidth = this.props.stroke.width;
294
+ ctx.lineCap = this.props.stroke.cap;
295
+ ctx.lineJoin = this.props.stroke.join;
296
+ ctx.miterLimit = this.props.stroke.miterLimit;
297
+ ctx.lineDashOffset = this.props.stroke.dashOffset;
298
+ ctx.setLineDash(this.props.stroke.dash);
299
+ ctx.stroke(this.props.path2D);
300
+ }
301
+ ctx.restore();
302
+ ctx.closePath();
303
+ }
304
+ /**
305
+ * Converts the Path2D Layer to a JSON representation.
306
+ * @returns {IPath2DLayer} The JSON representation of the Path2D Layer.
307
+ */
308
+ toJSON() {
309
+ return {
310
+ id: this.id,
311
+ type: this.type,
312
+ zIndex: this.zIndex,
313
+ visible: this.visible,
314
+ props: this.props
315
+ };
316
+ }
317
+ }
318
+ exports.Path2DLayer = Path2DLayer;
@@ -1,38 +1,116 @@
1
- import { BaseLayer } from "./BaseLayer";
2
- import { IQuadraticLayerProps, IQuadraticLayer, ColorType, ScaleType } from "../../types";
3
- import { Canvas, SKRSContext2D } from "@napi-rs/canvas";
1
+ import { BaseLayer, IBaseLayer, IBaseLayerMisc, IBaseLayerProps } from "./BaseLayer";
2
+ import { ColorType, ScaleType, Point, LayerType } from "../../types";
3
+ import { Canvas, SKRSContext2D, SvgCanvas } from "@napi-rs/canvas";
4
4
  import { LayersManager } from "../managers/LayersManager";
5
+ /**
6
+ * Interface representing a Quadratic Layer.
7
+ */
8
+ export interface IQuadraticLayer extends IBaseLayer {
9
+ /**
10
+ * The type of the layer, which is `QuadraticCurve`.
11
+ */
12
+ type: LayerType.QuadraticCurve;
13
+ /**
14
+ * The properties specific to the Quadratic Layer.
15
+ */
16
+ props: IQuadraticLayerProps;
17
+ }
18
+ /**
19
+ * Interface representing the properties of a Quadratic Layer.
20
+ */
21
+ export interface IQuadraticLayerProps extends IBaseLayerProps {
22
+ /**
23
+ * The control point of the quadratic curve, including x and y coordinates.
24
+ */
25
+ controlPoints: Array<Point>;
26
+ /**
27
+ * The end point of the quadratic curve, including x and y coordinates.
28
+ */
29
+ endPoint: Point;
30
+ /**
31
+ * The stroke properties of the quadratic curve.
32
+ */
33
+ stroke: {
34
+ /**
35
+ * The width of the stroke.
36
+ */
37
+ width: number;
38
+ /**
39
+ * The cap style of the stroke.
40
+ */
41
+ cap: CanvasLineCap;
42
+ /**
43
+ * The join style of the stroke.
44
+ */
45
+ join: CanvasLineJoin;
46
+ /**
47
+ * The dash offset of the stroke.
48
+ */
49
+ dashOffset: number;
50
+ /**
51
+ * The dash pattern of the stroke.
52
+ */
53
+ dash: number[];
54
+ /**
55
+ * The miter limit of the stroke.
56
+ */
57
+ miterLimit: number;
58
+ };
59
+ }
60
+ /**
61
+ * Class representing a Quadratic Layer, extending the BaseLayer class.
62
+ */
5
63
  export declare class QuadraticLayer extends BaseLayer<IQuadraticLayerProps> {
64
+ /**
65
+ * The properties of the Quadratic Layer.
66
+ */
6
67
  props: IQuadraticLayerProps;
7
- constructor(props?: IQuadraticLayerProps);
8
68
  /**
9
- * @description Sets the control point of the quadratic layer. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
10
- * @param x {ScaleType} - The control `x` of the quadratic layer.
11
- * @param y {ScaleType} - The control `y` of the quadratic layer.
69
+ * Constructs a new QuadraticLayer instance.
70
+ * @param props {IQuadraticLayerProps} - The properties of the Quadratic Layer.
71
+ * @param misc {IBaseLayerMisc} - Miscellaneous options for the layer.
72
+ */
73
+ constructor(props?: IQuadraticLayerProps, misc?: IBaseLayerMisc);
74
+ /**
75
+ * Sets the control point of the quadratic layer.
76
+ * @param x {ScaleType} - The x-coordinate of the control point.
77
+ * @param y {ScaleType} - The y-coordinate of the control point.
78
+ * @returns {this} The current instance for chaining.
12
79
  */
13
80
  setControlPoint(x: ScaleType, y: ScaleType): this;
14
81
  /**
15
- * @description Sets the end point of the quadratic layer. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
16
- * @param x {ScaleType} - The end `x` of the quadratic layer.
17
- * @param y {ScaleType} - The end `y` of the quadratic layer.
82
+ * Sets the end point of the quadratic layer.
83
+ * @param x {ScaleType} - The x-coordinate of the end point.
84
+ * @param y {ScaleType} - The y-coordinate of the end point.
85
+ * @returns {this} The current instance for chaining.
18
86
  */
19
87
  setEndPosition(x: ScaleType, y: ScaleType): this;
20
88
  /**
21
- * @description Sets the color of the layer. You can use `hex`, `rgb`, `rgba`, `hsl`, `hsla`, and `Gradient`color.
22
- * @param color {string} - The `color` of the layer.
89
+ * Sets the color of the layer.
90
+ * @param color {ColorType} - The color of the layer.
91
+ * @returns {this} The current instance for chaining.
92
+ * @throws {LazyError} If the color is not provided or invalid.
23
93
  */
24
94
  setColor(color: ColorType): this;
25
95
  /**
26
- * @description Sets the stroke of the layer.
27
- * @param width {number} - The `width` of the stroke.
28
- * @param cap {string} - The `cap` of the stroke.
29
- * @param join {string} - The `join` of the stroke.
30
- * @param dash {number[]} - The `dash` of the stroke.
31
- * @param dashOffset {number} - The `dashOffset` of the stroke.
32
- * @param miterLimit {number} - The `miterLimit` of the stroke.
96
+ * Sets the stroke properties of the layer.
97
+ * @param width {number} - The width of the stroke.
98
+ * @param cap {CanvasLineCap} - The cap style of the stroke.
99
+ * @param join {CanvasLineJoin} - The join style of the stroke.
100
+ * @param dash {number[]} - The dash pattern of the stroke.
101
+ * @param dashOffset {number} - The dash offset of the stroke.
102
+ * @param miterLimit {number} - The miter limit of the stroke.
103
+ * @returns {this} The current instance for chaining.
33
104
  */
34
105
  setStroke(width: number, cap?: CanvasLineCap, join?: CanvasLineJoin, dash?: number[], dashOffset?: number, miterLimit?: number): this;
35
- getBoundingBox(ctx: SKRSContext2D, canvas: Canvas, manager: LayersManager): {
106
+ /**
107
+ * Calculates the bounding box of the quadratic curve.
108
+ * @param ctx {SKRSContext2D} - The canvas rendering context.
109
+ * @param canvas {Canvas | SvgCanvas} - The canvas instance.
110
+ * @param manager {LayersManager} - The layers manager.
111
+ * @returns {Object} The bounding box details including max, min, center, width, and height.
112
+ */
113
+ getBoundingBox(ctx: SKRSContext2D, canvas: Canvas | SvgCanvas, manager: LayersManager): {
36
114
  max: {
37
115
  x: number;
38
116
  y: number;
@@ -48,9 +126,17 @@ export declare class QuadraticLayer extends BaseLayer<IQuadraticLayerProps> {
48
126
  width: number;
49
127
  height: number;
50
128
  };
51
- draw(ctx: SKRSContext2D, canvas: Canvas, manager: LayersManager, debug: boolean): Promise<void>;
52
129
  /**
53
- * @returns {IQuadraticLayer}
130
+ * Draws the quadratic curve on the canvas.
131
+ * @param ctx {SKRSContext2D} - The canvas rendering context.
132
+ * @param canvas {Canvas | SvgCanvas} - The canvas instance.
133
+ * @param manager {LayersManager} - The layers manager.
134
+ * @param debug {boolean} - Whether to enable debug logging.
135
+ */
136
+ draw(ctx: SKRSContext2D, canvas: Canvas | SvgCanvas, manager: LayersManager, debug: boolean): Promise<void>;
137
+ /**
138
+ * Converts the Quadratic Layer to a JSON representation.
139
+ * @returns {IQuadraticLayer} The JSON representation of the Quadratic Layer.
54
140
  */
55
141
  toJSON(): IQuadraticLayer;
56
142
  }
@@ -2,41 +2,55 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.QuadraticLayer = void 0;
4
4
  const BaseLayer_1 = require("./BaseLayer");
5
- const enum_1 = require("../../types/enum");
5
+ const types_1 = require("../../types");
6
6
  const utils_1 = require("../../utils/utils");
7
7
  const LazyUtil_1 = require("../../utils/LazyUtil");
8
- const Gradient_1 = require("../helpers/Gradient");
9
- const Pattern_1 = require("../helpers/Pattern");
8
+ const helpers_1 = require("../helpers");
9
+ /**
10
+ * Class representing a Quadratic Layer, extending the BaseLayer class.
11
+ */
10
12
  class QuadraticLayer extends BaseLayer_1.BaseLayer {
13
+ /**
14
+ * The properties of the Quadratic Layer.
15
+ */
11
16
  props;
12
- constructor(props) {
13
- super(enum_1.LayerType.QuadraticCurve, props || {});
17
+ /**
18
+ * Constructs a new QuadraticLayer instance.
19
+ * @param props {IQuadraticLayerProps} - The properties of the Quadratic Layer.
20
+ * @param misc {IBaseLayerMisc} - Miscellaneous options for the layer.
21
+ */
22
+ constructor(props, misc) {
23
+ super(types_1.LayerType.QuadraticCurve, props || {}, misc);
14
24
  this.props = props ? props : {};
15
25
  if (!this.props.fillStyle)
16
26
  this.props.fillStyle = '#000000';
17
- this.props.centring = enum_1.Centring.None;
27
+ this.props.centring = types_1.Centring.None;
18
28
  }
19
29
  /**
20
- * @description Sets the control point of the quadratic layer. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
21
- * @param x {ScaleType} - The control `x` of the quadratic layer.
22
- * @param y {ScaleType} - The control `y` of the quadratic layer.
30
+ * Sets the control point of the quadratic layer.
31
+ * @param x {ScaleType} - The x-coordinate of the control point.
32
+ * @param y {ScaleType} - The y-coordinate of the control point.
33
+ * @returns {this} The current instance for chaining.
23
34
  */
24
35
  setControlPoint(x, y) {
25
- this.props.controlPoint = { x, y };
36
+ this.props.controlPoints = [{ x, y }];
26
37
  return this;
27
38
  }
28
39
  /**
29
- * @description Sets the end point of the quadratic layer. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
30
- * @param x {ScaleType} - The end `x` of the quadratic layer.
31
- * @param y {ScaleType} - The end `y` of the quadratic layer.
40
+ * Sets the end point of the quadratic layer.
41
+ * @param x {ScaleType} - The x-coordinate of the end point.
42
+ * @param y {ScaleType} - The y-coordinate of the end point.
43
+ * @returns {this} The current instance for chaining.
32
44
  */
33
45
  setEndPosition(x, y) {
34
46
  this.props.endPoint = { x, y };
35
47
  return this;
36
48
  }
37
49
  /**
38
- * @description Sets the color of the layer. You can use `hex`, `rgb`, `rgba`, `hsl`, `hsla`, and `Gradient`color.
39
- * @param color {string} - The `color` of the layer.
50
+ * Sets the color of the layer.
51
+ * @param color {ColorType} - The color of the layer.
52
+ * @returns {this} The current instance for chaining.
53
+ * @throws {LazyError} If the color is not provided or invalid.
40
54
  */
41
55
  setColor(color) {
42
56
  if (!color)
@@ -44,7 +58,7 @@ class QuadraticLayer extends BaseLayer_1.BaseLayer {
44
58
  if (!(0, utils_1.isColor)(color))
45
59
  throw new LazyUtil_1.LazyError('The color of the layer must be a valid color');
46
60
  let fill = (0, utils_1.parseColor)(color);
47
- if (fill instanceof Gradient_1.Gradient || fill instanceof Pattern_1.Pattern) {
61
+ if (fill instanceof helpers_1.Gradient || fill instanceof helpers_1.Pattern) {
48
62
  this.props.fillStyle = fill;
49
63
  }
50
64
  else {
@@ -55,13 +69,14 @@ class QuadraticLayer extends BaseLayer_1.BaseLayer {
55
69
  return this;
56
70
  }
57
71
  /**
58
- * @description Sets the stroke of the layer.
59
- * @param width {number} - The `width` of the stroke.
60
- * @param cap {string} - The `cap` of the stroke.
61
- * @param join {string} - The `join` of the stroke.
62
- * @param dash {number[]} - The `dash` of the stroke.
63
- * @param dashOffset {number} - The `dashOffset` of the stroke.
64
- * @param miterLimit {number} - The `miterLimit` of the stroke.
72
+ * Sets the stroke properties of the layer.
73
+ * @param width {number} - The width of the stroke.
74
+ * @param cap {CanvasLineCap} - The cap style of the stroke.
75
+ * @param join {CanvasLineJoin} - The join style of the stroke.
76
+ * @param dash {number[]} - The dash pattern of the stroke.
77
+ * @param dashOffset {number} - The dash offset of the stroke.
78
+ * @param miterLimit {number} - The miter limit of the stroke.
79
+ * @returns {this} The current instance for chaining.
65
80
  */
66
81
  setStroke(width, cap, join, dash, dashOffset, miterLimit) {
67
82
  this.props.stroke = {
@@ -74,26 +89,40 @@ class QuadraticLayer extends BaseLayer_1.BaseLayer {
74
89
  };
75
90
  return this;
76
91
  }
92
+ /**
93
+ * Calculates the bounding box of the quadratic curve.
94
+ * @param ctx {SKRSContext2D} - The canvas rendering context.
95
+ * @param canvas {Canvas | SvgCanvas} - The canvas instance.
96
+ * @param manager {LayersManager} - The layers manager.
97
+ * @returns {Object} The bounding box details including max, min, center, width, and height.
98
+ */
77
99
  getBoundingBox(ctx, canvas, manager) {
78
100
  const parcer = (0, utils_1.parser)(ctx, canvas, manager);
79
101
  const { xs, ys, cx, cy, xe, ye } = parcer.parseBatch({
80
102
  xs: { v: this.props.x },
81
103
  ys: { v: this.props.y, options: LazyUtil_1.defaultArg.vl(true) },
82
- cx: { v: this.props.controlPoint.x },
83
- cy: { v: this.props.controlPoint.y, options: LazyUtil_1.defaultArg.vl(true) },
104
+ cx: { v: this.props.controlPoints[0].x },
105
+ cy: { v: this.props.controlPoints[0].y, options: LazyUtil_1.defaultArg.vl(true) },
84
106
  xe: { v: this.props.endPoint.x },
85
107
  ye: { v: this.props.endPoint.y, options: LazyUtil_1.defaultArg.vl(true) }
86
108
  });
87
109
  const { max, min, center, width, height } = (0, utils_1.getBoundingBoxBezier)([{ x: xs, y: ys }, { x: cx, y: cy }, { x: xe, y: ye }]);
88
110
  return { max, min, center, width, height };
89
111
  }
112
+ /**
113
+ * Draws the quadratic curve on the canvas.
114
+ * @param ctx {SKRSContext2D} - The canvas rendering context.
115
+ * @param canvas {Canvas | SvgCanvas} - The canvas instance.
116
+ * @param manager {LayersManager} - The layers manager.
117
+ * @param debug {boolean} - Whether to enable debug logging.
118
+ */
90
119
  async draw(ctx, canvas, manager, debug) {
91
120
  const parcer = (0, utils_1.parser)(ctx, canvas, manager);
92
121
  const { xs, ys, cx, cy, xe, ye } = parcer.parseBatch({
93
122
  xs: { v: this.props.x },
94
123
  ys: { v: this.props.y, options: LazyUtil_1.defaultArg.vl(true) },
95
- cx: { v: this.props.controlPoint.x },
96
- cy: { v: this.props.controlPoint.y, options: LazyUtil_1.defaultArg.vl(true) },
124
+ cx: { v: this.props.controlPoints[0].x },
125
+ cy: { v: this.props.controlPoints[0].y, options: LazyUtil_1.defaultArg.vl(true) },
97
126
  xe: { v: this.props.endPoint.x },
98
127
  ye: { v: this.props.endPoint.y, options: LazyUtil_1.defaultArg.vl(true) }
99
128
  });
@@ -121,12 +150,18 @@ class QuadraticLayer extends BaseLayer_1.BaseLayer {
121
150
  ctx.restore();
122
151
  }
123
152
  /**
124
- * @returns {IQuadraticLayer}
153
+ * Converts the Quadratic Layer to a JSON representation.
154
+ * @returns {IQuadraticLayer} The JSON representation of the Quadratic Layer.
125
155
  */
126
156
  toJSON() {
127
157
  let data = super.toJSON();
128
- data.props = this.props;
129
- return { ...data };
158
+ let copy = { ...this.props };
159
+ for (const key of ['x', 'y', 'endPoint.x', 'endPoint.y', 'controlPoint.x', 'controlPoint.y', 'fillStyle']) {
160
+ if (copy[key] && typeof copy[key] === 'object' && 'toJSON' in copy[key]) {
161
+ copy[key] = copy[key].toJSON();
162
+ }
163
+ }
164
+ return { ...data, props: copy };
130
165
  }
131
166
  }
132
167
  exports.QuadraticLayer = QuadraticLayer;