@nmmty/lazycanvas 0.4.0 → 0.5.1

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 (65) hide show
  1. package/ReadMe.md +1 -0
  2. package/dist/helpers/Filters.d.ts +10 -10
  3. package/dist/helpers/Fonts.d.ts +1 -1
  4. package/dist/helpers/FontsList.d.ts +1 -1
  5. package/dist/helpers/FontsList.js +19 -19
  6. package/dist/index.d.ts +11 -20
  7. package/dist/index.js +40 -47
  8. package/dist/structures/LazyCanvas.d.ts +126 -19
  9. package/dist/structures/LazyCanvas.js +100 -35
  10. package/dist/structures/components/BaseLayer.d.ts +188 -38
  11. package/dist/structures/components/BaseLayer.js +89 -43
  12. package/dist/structures/components/BezierLayer.d.ts +111 -33
  13. package/dist/structures/components/BezierLayer.js +72 -32
  14. package/dist/structures/components/ClearLayer.d.ts +120 -17
  15. package/dist/structures/components/ClearLayer.js +83 -22
  16. package/dist/structures/components/Group.d.ts +88 -20
  17. package/dist/structures/components/Group.js +69 -29
  18. package/dist/structures/components/ImageLayer.d.ts +76 -12
  19. package/dist/structures/components/ImageLayer.js +43 -39
  20. package/dist/structures/components/LineLayer.d.ts +111 -18
  21. package/dist/structures/components/LineLayer.js +57 -29
  22. package/dist/structures/components/MorphLayer.d.ts +109 -21
  23. package/dist/structures/components/MorphLayer.js +52 -33
  24. package/dist/structures/components/Path2DLayer.d.ts +164 -0
  25. package/dist/structures/components/Path2DLayer.js +293 -0
  26. package/dist/structures/components/QuadraticLayer.d.ts +108 -22
  27. package/dist/structures/components/QuadraticLayer.js +64 -38
  28. package/dist/structures/components/TextLayer.d.ts +201 -40
  29. package/dist/structures/components/TextLayer.js +98 -55
  30. package/dist/structures/components/index.d.ts +10 -0
  31. package/dist/structures/components/index.js +26 -0
  32. package/dist/structures/helpers/Exporter.d.ts +52 -0
  33. package/dist/structures/helpers/Exporter.js +168 -0
  34. package/dist/structures/helpers/Font.d.ts +64 -10
  35. package/dist/structures/helpers/Font.js +38 -11
  36. package/dist/structures/helpers/Gradient.d.ts +96 -9
  37. package/dist/structures/helpers/Gradient.js +49 -19
  38. package/dist/structures/helpers/Link.d.ts +52 -8
  39. package/dist/structures/helpers/Link.js +42 -11
  40. package/dist/structures/helpers/Pattern.d.ts +52 -7
  41. package/dist/structures/helpers/Pattern.js +48 -42
  42. package/dist/structures/helpers/index.d.ts +6 -0
  43. package/dist/structures/helpers/index.js +22 -0
  44. package/dist/structures/helpers/readers/JSONReader.d.ts +49 -0
  45. package/dist/structures/helpers/readers/JSONReader.js +172 -0
  46. package/dist/structures/helpers/readers/SVGReader.d.ts +20 -0
  47. package/dist/structures/helpers/readers/SVGReader.js +577 -0
  48. package/dist/structures/helpers/readers/YAMLReader.d.ts +0 -0
  49. package/dist/structures/helpers/readers/YAMLReader.js +1 -0
  50. package/dist/structures/managers/AnimationManager.d.ts +96 -20
  51. package/dist/structures/managers/AnimationManager.js +54 -26
  52. package/dist/structures/managers/FontsManager.d.ts +76 -32
  53. package/dist/structures/managers/FontsManager.js +70 -45
  54. package/dist/structures/managers/LayersManager.d.ts +84 -32
  55. package/dist/structures/managers/LayersManager.js +66 -28
  56. package/dist/structures/managers/RenderManager.d.ts +60 -6
  57. package/dist/structures/managers/RenderManager.js +120 -40
  58. package/dist/types/enum.d.ts +11 -6
  59. package/dist/types/enum.js +17 -12
  60. package/dist/types/index.d.ts +2 -19
  61. package/dist/types/index.js +17 -0
  62. package/dist/utils/LazyUtil.js +2 -2
  63. package/dist/utils/utils.d.ts +9 -11
  64. package/dist/utils/utils.js +163 -234
  65. package/package.json +4 -5
@@ -1,20 +1,52 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BaseLayer = void 0;
4
- const enum_1 = require("../../types/enum");
4
+ const types_1 = require("../../types");
5
5
  const utils_1 = require("../../utils/utils");
6
6
  const LazyUtil_1 = require("../../utils/LazyUtil");
7
+ /**
8
+ * Represents a base layer with generic properties and methods for managing
9
+ * its position, visibility, transformations, and other attributes.
10
+ *
11
+ * @template T - A type extending `IBaseLayerProps` that defines the properties of the layer.
12
+ */
7
13
  class BaseLayer {
14
+ /**
15
+ * The unique identifier of the layer.
16
+ * @type {string}
17
+ */
8
18
  id;
19
+ /**
20
+ * The type of the layer.
21
+ * @type {LayerType}
22
+ */
9
23
  type;
24
+ /**
25
+ * The z-index of the layer, determining its stacking order.
26
+ * @type {number}
27
+ */
10
28
  zIndex;
29
+ /**
30
+ * The visibility of the layer.
31
+ * @type {boolean}
32
+ */
11
33
  visible;
34
+ /**
35
+ * The properties of the layer, defined by the generic type `T`.
36
+ * @type {T}
37
+ */
12
38
  props;
13
- constructor(type, props) {
14
- this.id = (0, utils_1.generateID)(type ? type : enum_1.LayerType.Base);
15
- this.type = type ? type : enum_1.LayerType.Base;
16
- this.zIndex = 1;
17
- this.visible = true;
39
+ /**
40
+ * Constructs a new `BaseLayer` instance.
41
+ * @param {LayerType} [type] - The type of the layer.
42
+ * @param {T} [props] - The properties of the layer.
43
+ * @param {IBaseLayerMisc} [misc] - Miscellaneous options for the layer.
44
+ */
45
+ constructor(type, props, misc) {
46
+ this.id = misc?.id || (0, utils_1.generateID)(type ? type : types_1.LayerType.Base);
47
+ this.type = type ? type : types_1.LayerType.Base;
48
+ this.zIndex = misc?.zIndex || 1;
49
+ this.visible = misc?.visible || true;
18
50
  this.props = props ? props : {};
19
51
  if (!this.props.x)
20
52
  this.props.x = 0;
@@ -23,14 +55,15 @@ class BaseLayer {
23
55
  if (!this.props.opacity)
24
56
  this.props.opacity = 1;
25
57
  if (!this.props.centring)
26
- this.props.centring = enum_1.Centring.Center;
58
+ this.props.centring = types_1.Centring.Center;
27
59
  if (!this.props.transform)
28
60
  this.props.transform = {};
29
61
  }
30
62
  /**
31
- * @description Position of the layer in the 2D plane. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
32
- * @param x {ScaleType} - The `x` position of the layer
33
- * @param y {ScaleType} - The `y` position of the layer
63
+ * Sets the position of the layer in the 2D plane.
64
+ * @param {ScaleType} x - The x-coordinate of the layer.
65
+ * @param {ScaleType} y - The y-coordinate of the layer.
66
+ * @returns {this} The current instance for chaining.
34
67
  */
35
68
  setPosition(x, y) {
36
69
  this.props.x = x;
@@ -38,36 +71,40 @@ class BaseLayer {
38
71
  return this;
39
72
  }
40
73
  /**
41
- * @description Opacity of the layer. The value must be between `0` and `1`.
42
- * @param opacity {number} - The `opacity` of the layer
74
+ * Sets the opacity of the layer.
75
+ * @param {number} opacity - The opacity value, between 0 and 1.
76
+ * @returns {this} The current instance for chaining.
43
77
  */
44
78
  setOpacity(opacity) {
45
79
  this.props.opacity = opacity;
46
80
  return this;
47
81
  }
48
82
  /**
49
- * @description Sets the `id` of the layer.
50
- * @param id {string} - The `id` of the layer
83
+ * Sets the unique identifier of the layer.
84
+ *
85
+ * @param {string} id - The unique identifier.
86
+ * @returns {this} The current instance for chaining.
51
87
  */
52
88
  setID(id) {
53
89
  this.id = id;
54
90
  return this;
55
91
  }
56
92
  /**
57
- * @description Sets shadow of layer.
58
- * @param color {string} - The `color` of the filter
59
- * @param blur {number} - The `blur` of the filter
60
- * @param offsetX {number} - The `offsetX` of the filter
61
- * @param offsetY {number} - The `offsetY` of the filter
93
+ * Sets the shadow properties of the layer.
94
+ * @param {string} color - The color of the shadow.
95
+ * @param {number} [blur] - The blur radius of the shadow.
96
+ * @param {number} [offsetX] - The horizontal offset of the shadow.
97
+ * @param {number} [offsetY] - The vertical offset of the shadow.
98
+ * @returns {this} The current instance for chaining.
99
+ * @throws {LazyError} If the color is invalid or not provided.
62
100
  */
63
101
  setShadow(color, blur, offsetX, offsetY) {
64
102
  if (!color)
65
103
  throw new LazyUtil_1.LazyError('The color of the shadow must be provided');
66
104
  if (!(0, utils_1.isColor)(color))
67
105
  throw new LazyUtil_1.LazyError('The color of the shadow must be a valid color');
68
- let parse = (0, utils_1.parseColor)(color);
69
106
  this.props.shadow = {
70
- color: parse,
107
+ color: color,
71
108
  blur: blur || 0,
72
109
  offsetX: offsetX || 0,
73
110
  offsetY: offsetY || 0,
@@ -75,82 +112,91 @@ class BaseLayer {
75
112
  return this;
76
113
  }
77
114
  /**
78
- * @description Matrix of the layer in the 2D plane.
79
- * @param matrix {DOMMatrix2DInit} - The `matrix` of the layer
115
+ * Sets the transformation matrix of the layer.
116
+ * @param {DOMMatrix2DInit} matrix - The transformation matrix.
117
+ * @returns {this} The current instance for chaining.
80
118
  */
81
119
  setMatrix(matrix) {
82
120
  this.props.transform = { ...this.props.transform, matrix };
83
121
  return this;
84
122
  }
85
123
  /**
86
- * @description Scale of the layer in the x and y direction by the given amount from their current position.
87
- * @param x {number} - The `x` scale of the layer
88
- * @param y {number} - The `y` scale of the layer
124
+ * Sets the scale of the layer in the x and y directions.
125
+ * @param {number} x - The scale factor in the x direction.
126
+ * @param {number} y - The scale factor in the y direction.
127
+ * @returns {this} The current instance for chaining.
89
128
  */
90
129
  setScale(x, y) {
91
130
  this.props.transform = { ...this.props.transform, scale: { x, y } };
92
131
  return this;
93
132
  }
94
133
  /**
95
- * @description Rotate of the layer in the clockwise direction by the given amount from its current position.
96
- * @param rotate {number} - The `rotate` of the layer
134
+ * Sets the rotation of the layer.
135
+ * @param {number} rotate - The rotation angle in degrees.
136
+ * @returns {this} The current instance for chaining.
97
137
  */
98
138
  setRotate(rotate) {
99
139
  this.props.transform = { ...this.props.transform, rotate };
100
140
  return this;
101
141
  }
102
142
  /**
103
- * @description Translate of the layer in the x and y direction by the given amount from their current position.
104
- * @param x {number} - The `x` translation of the layer
105
- * @param y {number} - The `y` translation of the layer
143
+ * Sets the translation of the layer in the x and y directions.
144
+ * @param {number} x - The translation in the x direction.
145
+ * @param {number} y - The translation in the y direction.
146
+ * @returns {this} The current instance for chaining.
106
147
  */
107
148
  setTranslate(x, y) {
108
149
  this.props.transform = { ...this.props.transform, translate: { x, y } };
109
150
  return this;
110
151
  }
111
152
  /**
112
- * @description The **`CanvasRenderingContext2D`**. filter property of the Canvas 2D API provides filter effects such as blurring and grayscaling.
113
- * It is similar to the CSS [`filter`](https://developer.mozilla.org/en-US/docs/Web/CSS/filter) property and accepts the same values.
114
- * @param filter {string} - The `filter` of the layer. Multiple filters are supported.
153
+ * Sets the filter effects for the layer.
154
+ * @param {...AnyFilter} filter - The filter effects to apply.
155
+ * @returns {this} The current instance for chaining.
115
156
  */
116
157
  setFilters(...filter) {
117
158
  this.props.filter = filter.join(' ');
118
159
  return this;
119
160
  }
120
161
  /**
121
- * @description Sets type of centring of the layer.
122
- * @param centring {AnyCentring} - The `centring` of the layer
162
+ * Sets the centring type of the layer. **Don't affect on Bezier, Line, Quadratic and Text layers**.
163
+ * @param {AnyCentring} centring - The centring type.
164
+ * @returns {this} The current instance for chaining.
123
165
  */
124
166
  setCentring(centring) {
125
167
  this.props.centring = centring;
126
168
  return this;
127
169
  }
128
170
  /**
129
- * @description Sets the visibility of the layer.
130
- * @param visible {boolean} - The `visibility` of the layer
171
+ * Sets the visibility of the layer.
172
+ * @param {boolean} visible - The visibility state.
173
+ * @returns {this} The current instance for chaining.
131
174
  */
132
175
  setVisible(visible) {
133
176
  this.visible = visible;
134
177
  return this;
135
178
  }
136
179
  /**
137
- * @description Sets zIndex of the layer.
138
- * @param zIndex {number} - The `zIndex` of the layer
180
+ * Sets the z-index of the layer.
181
+ * @param {number} zIndex - The z-index value.
182
+ * @returns {this} The current instance for chaining.
139
183
  */
140
184
  setZIndex(zIndex) {
141
185
  this.zIndex = zIndex;
142
186
  return this;
143
187
  }
144
188
  /**
145
- * @description Sets global composite operation of the layer.
146
- * @param operation {AnyGlobalCompositeOperation} - The `operation` of the layer
189
+ * Sets the global composite operation for the layer.
190
+ * @param {AnyGlobalCompositeOperation} operation - The composite operation.
191
+ * @returns {this} The current instance for chaining.
147
192
  */
148
193
  setGlobalCompositeOperation(operation) {
149
194
  this.props.globalComposite = operation;
150
195
  return this;
151
196
  }
152
197
  /**
153
- * @returns {IBaseLayer}
198
+ * Converts the layer to a JSON representation.
199
+ * @returns {IBaseLayer} The JSON representation of the layer.
154
200
  */
155
201
  toJSON() {
156
202
  return {
@@ -1,58 +1,136 @@
1
- import { BaseLayer } from "./BaseLayer";
2
- import { ColorType, IBezierLayer, IBezierLayerProps, ScaleType } from "../../types";
3
- import { Canvas, SKRSContext2D } from "@napi-rs/canvas";
1
+ import { BaseLayer, IBaseLayer, IBaseLayerMisc, IBaseLayerProps } from "./BaseLayer";
2
+ import { ColorType, Point, ScaleType, LayerType } from "../../types";
3
+ import { Canvas, SKRSContext2D, SvgCanvas } from "@napi-rs/canvas";
4
4
  import { LayersManager } from "../managers/LayersManager";
5
+ /**
6
+ * Interface representing a Bezier layer.
7
+ */
8
+ export interface IBezierLayer extends IBaseLayer {
9
+ /**
10
+ * The type of the layer, which is a Bézier curve.
11
+ */
12
+ type: LayerType.BezierCurve;
13
+ /**
14
+ * The properties specific to the Bezier layer.
15
+ */
16
+ props: IBezierLayerProps;
17
+ }
18
+ /**
19
+ * Interface representing the properties of a Bezier layer.
20
+ */
21
+ export interface IBezierLayerProps extends IBaseLayerProps {
22
+ /**
23
+ * The control points of the Bézier curve.
24
+ */
25
+ controlPoints: Array<Point>;
26
+ /**
27
+ * The end point of the Bézier curve.
28
+ */
29
+ endPoint: Point;
30
+ /**
31
+ * The stroke properties of the Bézier 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 Bezier layer, extending the BaseLayer class.
62
+ */
5
63
  export declare class BezierLayer extends BaseLayer<IBezierLayerProps> {
64
+ /**
65
+ * The properties of the Bezier layer.
66
+ */
6
67
  props: IBezierLayerProps;
7
- constructor(props?: IBezierLayerProps);
8
68
  /**
9
- * @description Sets the control points of the bezier layer. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
10
- * @param controlPoints {Array<{ x: ScaleType, y: ScaleType }>} - The `controlPoints` of the bezier layer.
69
+ * Constructs a new BezierLayer instance.
70
+ * @param props {IBezierLayerProps} - The properties of the Bezier layer.
71
+ * @param misc {IBaseLayerMisc} - Miscellaneous options for the layer.
72
+ */
73
+ constructor(props?: IBezierLayerProps, misc?: IBaseLayerMisc);
74
+ /**
75
+ * Sets the control points of the Bezier layer.
76
+ * @param controlPoints {Array<{ x: ScaleType, y: ScaleType }>} - The control points of the Bezier layer.
77
+ * @returns {this} The current instance for chaining.
78
+ * @throws {LazyError} If the number of control points is not exactly 2.
11
79
  */
12
80
  setControlPoints(...controlPoints: {
13
81
  x: ScaleType;
14
82
  y: ScaleType;
15
83
  }[]): this;
16
84
  /**
17
- * @description Sets the end point of the bezier layer. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
18
- * @param x {ScaleType} - The end `x` of the bezier layer.
19
- * @param y {ScaleType} - The end `y` of the bezier layer.
85
+ * Sets the end position of the Bezier layer.
86
+ * @param x {ScaleType} - The x-coordinate of the end point.
87
+ * @param y {ScaleType} - The y-coordinate of the end point.
88
+ * @returns {this} The current instance for chaining.
20
89
  */
21
90
  setEndPosition(x: ScaleType, y: ScaleType): this;
22
91
  /**
23
- * @description Sets the color of the layer. You can use `hex`, `rgb`, `rgba`, `hsl`, `hsla`, and `Gradient`color.
24
- * @param color {string} - The `color` of the layer.
92
+ * Sets the color of the Bezier layer.
93
+ * @param color {ColorType} - The color of the layer.
94
+ * @returns {this} The current instance for chaining.
95
+ * @throws {LazyError} If the color is not provided or invalid.
25
96
  */
26
97
  setColor(color: ColorType): this;
27
98
  /**
28
- * @description Sets the stroke of the layer.
29
- * @param width {number} - The `width` of the stroke.
30
- * @param cap {string} - The `cap` of the stroke.
31
- * @param join {string} - The `join` of the stroke.
32
- * @param dash {number[]} - The `dash` of the stroke.
33
- * @param dashOffset {number} - The `dashOffset` of the stroke.
34
- * @param miterLimit {number} - The `miterLimit` of the stroke.
99
+ * Sets the stroke properties of the Bezier layer.
100
+ * @param width {number} - The width of the stroke.
101
+ * @param cap {string} - The cap style of the stroke.
102
+ * @param join {string} - The join style of the stroke.
103
+ * @param dash {number[]} - The dash pattern of the stroke.
104
+ * @param dashOffset {number} - The dash offset of the stroke.
105
+ * @param miterLimit {number} - The miter limit of the stroke.
106
+ * @returns {this} The current instance for chaining.
35
107
  */
36
108
  setStroke(width: number, cap?: CanvasLineCap, join?: CanvasLineJoin, dash?: number[], dashOffset?: number, miterLimit?: number): this;
37
- getBoundingBox(ctx: SKRSContext2D, canvas: Canvas, manager: LayersManager): {
38
- max: {
39
- x: number;
40
- y: number;
41
- };
42
- min: {
43
- x: number;
44
- y: number;
45
- };
46
- center: {
47
- x: number;
48
- y: number;
49
- };
109
+ /**
110
+ * Calculates the bounding box of the Bezier layer.
111
+ * @param ctx {SKRSContext2D} - The canvas rendering context.
112
+ * @param canvas {Canvas | SvgCanvas} - The canvas instance.
113
+ * @param manager {LayersManager} - The layers manager.
114
+ * @returns {Object} The bounding box details including max, min, center, width, and height.
115
+ */
116
+ getBoundingBox(ctx: SKRSContext2D, canvas: Canvas | SvgCanvas, manager: LayersManager): {
117
+ max: Point;
118
+ min: Point;
119
+ center: Point;
50
120
  width: number;
51
121
  height: number;
52
122
  };
53
- draw(ctx: SKRSContext2D, canvas: Canvas, manager: LayersManager, debug: boolean): Promise<void>;
54
123
  /**
55
- * @returns {IBezierLayer}
124
+ * Draws the Bezier layer on the canvas.
125
+ * @param ctx {SKRSContext2D} - The canvas rendering context.
126
+ * @param canvas {Canvas | SvgCanvas} - The canvas instance.
127
+ * @param manager {LayersManager} - The layers manager.
128
+ * @param debug {boolean} - Whether to enable debug logging.
129
+ */
130
+ draw(ctx: SKRSContext2D, canvas: Canvas | SvgCanvas, manager: LayersManager, debug: boolean): Promise<void>;
131
+ /**
132
+ * Converts the Bezier layer to a JSON representation.
133
+ * @returns {IBezierLayer} The JSON representation of the Bezier layer.
56
134
  */
57
135
  toJSON(): IBezierLayer;
58
136
  }
@@ -2,23 +2,34 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BezierLayer = 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
+ /**
9
+ * Class representing a Bezier layer, extending the BaseLayer class.
10
+ */
10
11
  class BezierLayer extends BaseLayer_1.BaseLayer {
12
+ /**
13
+ * The properties of the Bezier layer.
14
+ */
11
15
  props;
12
- constructor(props) {
13
- super(enum_1.LayerType.BezierCurve, props || {});
16
+ /**
17
+ * Constructs a new BezierLayer instance.
18
+ * @param props {IBezierLayerProps} - The properties of the Bezier layer.
19
+ * @param misc {IBaseLayerMisc} - Miscellaneous options for the layer.
20
+ */
21
+ constructor(props, misc) {
22
+ super(types_1.LayerType.BezierCurve, props || {}, misc);
14
23
  this.props = props ? props : {};
15
24
  if (!this.props.fillStyle)
16
25
  this.props.fillStyle = '#000000';
17
- this.props.centring = enum_1.Centring.None;
26
+ this.props.centring = types_1.Centring.None;
18
27
  }
19
28
  /**
20
- * @description Sets the control points of the bezier layer. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
21
- * @param controlPoints {Array<{ x: ScaleType, y: ScaleType }>} - The `controlPoints` of the bezier layer.
29
+ * Sets the control points of the Bezier layer.
30
+ * @param controlPoints {Array<{ x: ScaleType, y: ScaleType }>} - The control points of the Bezier layer.
31
+ * @returns {this} The current instance for chaining.
32
+ * @throws {LazyError} If the number of control points is not exactly 2.
22
33
  */
23
34
  setControlPoints(...controlPoints) {
24
35
  if (controlPoints.length !== 2)
@@ -27,42 +38,38 @@ class BezierLayer extends BaseLayer_1.BaseLayer {
27
38
  return this;
28
39
  }
29
40
  /**
30
- * @description Sets the end point of the bezier layer. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
31
- * @param x {ScaleType} - The end `x` of the bezier layer.
32
- * @param y {ScaleType} - The end `y` of the bezier layer.
41
+ * Sets the end position of the Bezier layer.
42
+ * @param x {ScaleType} - The x-coordinate of the end point.
43
+ * @param y {ScaleType} - The y-coordinate of the end point.
44
+ * @returns {this} The current instance for chaining.
33
45
  */
34
46
  setEndPosition(x, y) {
35
47
  this.props.endPoint = { x, y };
36
48
  return this;
37
49
  }
38
50
  /**
39
- * @description Sets the color of the layer. You can use `hex`, `rgb`, `rgba`, `hsl`, `hsla`, and `Gradient`color.
40
- * @param color {string} - The `color` of the layer.
51
+ * Sets the color of the Bezier layer.
52
+ * @param color {ColorType} - The color of the layer.
53
+ * @returns {this} The current instance for chaining.
54
+ * @throws {LazyError} If the color is not provided or invalid.
41
55
  */
42
56
  setColor(color) {
43
57
  if (!color)
44
58
  throw new LazyUtil_1.LazyError('The color of the layer must be provided');
45
59
  if (!(0, utils_1.isColor)(color))
46
60
  throw new LazyUtil_1.LazyError('The color of the layer must be a valid color');
47
- let fill = (0, utils_1.parseColor)(color);
48
- if (fill instanceof Gradient_1.Gradient || fill instanceof Pattern_1.Pattern) {
49
- this.props.fillStyle = fill;
50
- }
51
- else {
52
- let arr = fill.split(':');
53
- this.props.fillStyle = arr[0];
54
- this.props.opacity = parseFloat(arr[1]) || 1;
55
- }
61
+ this.props.fillStyle = color;
56
62
  return this;
57
63
  }
58
64
  /**
59
- * @description Sets the stroke of the layer.
60
- * @param width {number} - The `width` of the stroke.
61
- * @param cap {string} - The `cap` of the stroke.
62
- * @param join {string} - The `join` of the stroke.
63
- * @param dash {number[]} - The `dash` of the stroke.
64
- * @param dashOffset {number} - The `dashOffset` of the stroke.
65
- * @param miterLimit {number} - The `miterLimit` of the stroke.
65
+ * Sets the stroke properties of the Bezier layer.
66
+ * @param width {number} - The width of the stroke.
67
+ * @param cap {string} - The cap style of the stroke.
68
+ * @param join {string} - The join style of the stroke.
69
+ * @param dash {number[]} - The dash pattern of the stroke.
70
+ * @param dashOffset {number} - The dash offset of the stroke.
71
+ * @param miterLimit {number} - The miter limit of the stroke.
72
+ * @returns {this} The current instance for chaining.
66
73
  */
67
74
  setStroke(width, cap, join, dash, dashOffset, miterLimit) {
68
75
  this.props.stroke = {
@@ -75,6 +82,13 @@ class BezierLayer extends BaseLayer_1.BaseLayer {
75
82
  };
76
83
  return this;
77
84
  }
85
+ /**
86
+ * Calculates the bounding box of the Bezier layer.
87
+ * @param ctx {SKRSContext2D} - The canvas rendering context.
88
+ * @param canvas {Canvas | SvgCanvas} - The canvas instance.
89
+ * @param manager {LayersManager} - The layers manager.
90
+ * @returns {Object} The bounding box details including max, min, center, width, and height.
91
+ */
78
92
  getBoundingBox(ctx, canvas, manager) {
79
93
  const parcer = (0, utils_1.parser)(ctx, canvas, manager);
80
94
  const { xs, ys, cp1x, cp1y, cp2x, cp2y, xe, ye } = parcer.parseBatch({
@@ -90,6 +104,13 @@ class BezierLayer extends BaseLayer_1.BaseLayer {
90
104
  const { max, min, center, width, height } = (0, utils_1.getBoundingBoxBezier)([{ x: xs, y: ys }, { x: cp1x, y: cp1y }, { x: cp2x, y: cp2y }, { x: xe, y: ye }]);
91
105
  return { max, min, center, width, height };
92
106
  }
107
+ /**
108
+ * Draws the Bezier layer on the canvas.
109
+ * @param ctx {SKRSContext2D} - The canvas rendering context.
110
+ * @param canvas {Canvas | SvgCanvas} - The canvas instance.
111
+ * @param manager {LayersManager} - The layers manager.
112
+ * @param debug {boolean} - Whether to enable debug logging.
113
+ */
93
114
  async draw(ctx, canvas, manager, debug) {
94
115
  const parcer = (0, utils_1.parser)(ctx, canvas, manager);
95
116
  const { xs, ys, cp1x, cp1y, cp2x, cp2y, xe, ye } = parcer.parseBatch({
@@ -126,12 +147,31 @@ class BezierLayer extends BaseLayer_1.BaseLayer {
126
147
  ctx.restore();
127
148
  }
128
149
  /**
129
- * @returns {IBezierLayer}
150
+ * Converts the Bezier layer to a JSON representation.
151
+ * @returns {IBezierLayer} The JSON representation of the Bezier layer.
130
152
  */
131
153
  toJSON() {
132
154
  let data = super.toJSON();
133
- data.props = this.props;
134
- return { ...data };
155
+ let copy = { ...this.props };
156
+ for (const key of ['x', 'y', 'endPoint.x', 'endPoint.y', 'fillStyle']) {
157
+ if (copy[key] && typeof copy[key] === 'object' && 'toJSON' in copy[key]) {
158
+ copy[key] = copy[key].toJSON();
159
+ }
160
+ }
161
+ if (copy.controlPoints) {
162
+ copy.controlPoints = copy.controlPoints.map((point) => {
163
+ if (point.x && typeof point.x === 'object' && 'toJSON' in point.x) {
164
+ // @ts-ignore
165
+ point.x = point.x.toJSON();
166
+ }
167
+ if (point.y && typeof point.y === 'object' && 'toJSON' in point.y) {
168
+ // @ts-ignore
169
+ point.y = point.y.toJSON();
170
+ }
171
+ return point;
172
+ });
173
+ }
174
+ return { ...data, props: copy };
135
175
  }
136
176
  }
137
177
  exports.BezierLayer = BezierLayer;