@nmmty/lazycanvas 0.6.5 → 1.0.0-dev.10

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 (91) hide show
  1. package/ReadMe.md +1 -1
  2. package/dist/core/Interpolation.d.ts +30 -0
  3. package/dist/core/Interpolation.js +200 -0
  4. package/dist/core/Scene.d.ts +94 -0
  5. package/dist/core/Scene.js +157 -0
  6. package/dist/core/Signal.d.ts +133 -0
  7. package/dist/core/Signal.js +255 -0
  8. package/dist/core/SignalUtils.d.ts +133 -0
  9. package/dist/core/SignalUtils.js +333 -0
  10. package/dist/core/ThreadScheduler.d.ts +38 -0
  11. package/dist/core/ThreadScheduler.js +74 -0
  12. package/dist/core/index.d.ts +5 -0
  13. package/dist/core/index.js +21 -0
  14. package/dist/helpers/Filters.js +1 -1
  15. package/dist/helpers/FontsList.js +18 -18
  16. package/dist/helpers/Utlis.d.ts +3 -3
  17. package/dist/helpers/Utlis.js +25 -36
  18. package/dist/helpers/index.d.ts +3 -3
  19. package/dist/index.d.ts +4 -1
  20. package/dist/index.js +4 -1
  21. package/dist/jsx-runtime.d.ts +17 -0
  22. package/dist/jsx-runtime.js +111 -0
  23. package/dist/structures/LazyCanvas.d.ts +10 -48
  24. package/dist/structures/LazyCanvas.js +17 -78
  25. package/dist/structures/components/BaseLayer.d.ts +78 -32
  26. package/dist/structures/components/BaseLayer.js +106 -37
  27. package/dist/structures/components/BezierLayer.d.ts +25 -38
  28. package/dist/structures/components/BezierLayer.js +88 -53
  29. package/dist/structures/components/{Group.d.ts → Div.d.ts} +37 -18
  30. package/dist/structures/components/Div.js +202 -0
  31. package/dist/structures/components/ImageLayer.d.ts +1 -1
  32. package/dist/structures/components/ImageLayer.js +28 -29
  33. package/dist/structures/components/LineLayer.d.ts +18 -36
  34. package/dist/structures/components/LineLayer.js +43 -45
  35. package/dist/structures/components/MorphLayer.d.ts +4 -33
  36. package/dist/structures/components/MorphLayer.js +37 -51
  37. package/dist/structures/components/Path2DLayer.d.ts +7 -35
  38. package/dist/structures/components/Path2DLayer.js +32 -41
  39. package/dist/structures/components/PolygonLayer.d.ts +3 -32
  40. package/dist/structures/components/PolygonLayer.js +37 -42
  41. package/dist/structures/components/QuadraticLayer.d.ts +25 -34
  42. package/dist/structures/components/QuadraticLayer.js +83 -47
  43. package/dist/structures/components/TextLayer.d.ts +5 -34
  44. package/dist/structures/components/TextLayer.js +88 -71
  45. package/dist/structures/components/index.d.ts +10 -11
  46. package/dist/structures/components/index.js +1 -2
  47. package/dist/structures/helpers/Exporter.d.ts +13 -4
  48. package/dist/structures/helpers/Exporter.js +82 -46
  49. package/dist/structures/helpers/Font.js +1 -17
  50. package/dist/structures/helpers/Gradient.js +35 -49
  51. package/dist/structures/helpers/Link.js +2 -14
  52. package/dist/structures/helpers/Pattern.js +10 -18
  53. package/dist/structures/helpers/index.d.ts +7 -7
  54. package/dist/structures/helpers/readers/JSONReader.d.ts +4 -4
  55. package/dist/structures/helpers/readers/JSONReader.js +44 -48
  56. package/dist/structures/helpers/readers/YAMLReader.js +11 -11
  57. package/dist/structures/managers/FontsManager.js +9 -18
  58. package/dist/structures/managers/LayersManager.d.ts +18 -28
  59. package/dist/structures/managers/LayersManager.js +14 -36
  60. package/dist/structures/managers/LayoutManager.d.ts +23 -0
  61. package/dist/structures/managers/LayoutManager.js +409 -0
  62. package/dist/structures/managers/index.d.ts +3 -5
  63. package/dist/structures/managers/index.js +1 -3
  64. package/dist/structures/managers/{RenderManager.d.ts → piplines/ClassicRenderPipeline.d.ts} +4 -30
  65. package/dist/structures/managers/piplines/ClassicRenderPipeline.js +90 -0
  66. package/dist/structures/managers/piplines/ModernRenderPipeline.d.ts +44 -0
  67. package/dist/structures/managers/piplines/ModernRenderPipeline.js +123 -0
  68. package/dist/structures/managers/piplines/index.d.ts +24 -0
  69. package/dist/structures/managers/piplines/index.js +18 -0
  70. package/dist/types/enum.d.ts +1 -2
  71. package/dist/types/enum.js +1 -2
  72. package/dist/types/index.d.ts +1 -1
  73. package/dist/types/types.d.ts +257 -107
  74. package/dist/utils/APNGEncoder.d.ts +67 -0
  75. package/dist/utils/APNGEncoder.js +205 -0
  76. package/dist/utils/DrawUtils.d.ts +9 -0
  77. package/dist/utils/DrawUtils.js +42 -0
  78. package/dist/utils/LazyUtil.js +1 -2
  79. package/dist/utils/index.d.ts +4 -0
  80. package/dist/utils/index.js +20 -0
  81. package/dist/utils/utils.d.ts +4 -7
  82. package/dist/utils/utils.js +140 -78
  83. package/package.json +61 -59
  84. package/dist/structures/components/ClearLayer.d.ts +0 -147
  85. package/dist/structures/components/ClearLayer.js +0 -158
  86. package/dist/structures/components/Group.js +0 -153
  87. package/dist/structures/managers/AnimationManager.d.ts +0 -120
  88. package/dist/structures/managers/AnimationManager.js +0 -99
  89. package/dist/structures/managers/PluginManager.d.ts +0 -230
  90. package/dist/structures/managers/PluginManager.js +0 -182
  91. package/dist/structures/managers/RenderManager.js +0 -183
@@ -0,0 +1,202 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Div = void 0;
4
+ const types_1 = require("../../types");
5
+ const utils_1 = require("../../utils");
6
+ const BaseLayer_1 = require("./BaseLayer");
7
+ /**
8
+ * Class representing a group of layer's.
9
+ */
10
+ class Div extends BaseLayer_1.BaseLayer {
11
+ /**
12
+ * Constructs a new Group instance.
13
+ * @param {IDivProps} [props] - The properties of the Div.
14
+ * @param {string} [opts.id] - The unique identifier of the group.
15
+ * @param {boolean} [opts.visible] - The visibility of the group.
16
+ * @param {number} [opts.zIndex] - The z-index of the group.
17
+ */
18
+ constructor(props, opts) {
19
+ super(types_1.LayerType.Group, props || {}, opts);
20
+ /**
21
+ * The type of the group, which is `Group`.
22
+ */
23
+ this.type = types_1.LayerType.Group;
24
+ // Extract id, visible, zIndex from props if provided (for JSX support)
25
+ const propsId = props?.id;
26
+ const propsVisible = props?.visible;
27
+ const propsZIndex = props?.zIndex;
28
+ this.id = opts?.id || propsId || (0, utils_1.generateID)(types_1.LayerType.Group);
29
+ this.visible = opts?.visible ?? propsVisible ?? true;
30
+ this.zIndex = opts?.zIndex ?? propsZIndex ?? 1;
31
+ this.layers = [];
32
+ this.props = props || {};
33
+ this.parent = null;
34
+ }
35
+ /**
36
+ * Sets the ID of the group.
37
+ * @param {string} [id] - The unique identifier of the group.
38
+ * @returns {this} The current instance for chaining.
39
+ */
40
+ setID(id) {
41
+ this.id = id;
42
+ return this;
43
+ }
44
+ /**
45
+ * Sets the visibility of the group.
46
+ * @param {boolean} [visible] - The visibility state of the group.
47
+ * @returns {this} The current instance for chaining.
48
+ */
49
+ setVisible(visible) {
50
+ this.visible = visible;
51
+ return this;
52
+ }
53
+ /**
54
+ * Sets the z-index of the group.
55
+ * @param {number} [zIndex] - The z-index value of the group.
56
+ * @returns {this} The current instance for chaining.
57
+ */
58
+ setZIndex(zIndex) {
59
+ this.zIndex = zIndex;
60
+ return this;
61
+ }
62
+ /**
63
+ * Adds components to the group.
64
+ * @param {AnyLayer[] | Div[]} [components] - The components to add to the group.
65
+ * @returns {this} The current instance for chaining.
66
+ */
67
+ add(...components) {
68
+ let layersArray = components.filter((l) => l !== undefined);
69
+ layersArray = layersArray.sort((a, b) => a.zIndex - b.zIndex);
70
+ for (const layer of layersArray) {
71
+ layer.parent = this;
72
+ }
73
+ this.layers.push(...layersArray);
74
+ return this;
75
+ }
76
+ /**
77
+ * Removes a component from the group by its ID.
78
+ * @param {string} [id] - The unique identifier of the component to remove.
79
+ * @returns {this} The current instance for chaining.
80
+ */
81
+ remove(id) {
82
+ this.layers = this.layers.filter((c) => c.id !== id);
83
+ return this;
84
+ }
85
+ /**
86
+ * Clears all components from the group.
87
+ * @returns {this} The current instance for chaining.
88
+ */
89
+ clear() {
90
+ this.layers = [];
91
+ return this;
92
+ }
93
+ /**
94
+ * Retrieves a component from the group by its ID.
95
+ * @param {string} [id] - The unique identifier of the component to retrieve.
96
+ * @returns {AnyLayer | Div | undefined} The component with the specified ID, or undefined if not found.
97
+ */
98
+ get(id) {
99
+ return this.layers.find((c) => c.id === id);
100
+ }
101
+ /**
102
+ * Retrieves all components from the group.
103
+ * @returns {AnyLayer[] | Div[]} An array of all components in the group.
104
+ */
105
+ getAll() {
106
+ return this.layers;
107
+ }
108
+ /**
109
+ * Gets the number of components in the group.
110
+ * @returns {number} The number of components in the group.
111
+ */
112
+ get length() {
113
+ return this.layers.length;
114
+ }
115
+ /**
116
+ * Update state for all child layers (for animation support)
117
+ * @param {number} time - Current time in seconds
118
+ */
119
+ updateState(time) {
120
+ for (const layer of this.layers) {
121
+ if ("updateState" in layer && typeof layer.updateState === "function") {
122
+ layer.updateState(time);
123
+ }
124
+ }
125
+ }
126
+ /**
127
+ * Renders a single layer or group of layers.
128
+ * @param {AnyLayer | Div} [layer] - The layer or group to render.
129
+ * @param {SKRSContext2D} [ctx] - The canvas rendering context.
130
+ * @param {Canvas | SvgCanvas} [canvas] - The canvas instance.
131
+ * @param {LayersManager} [manager] - The layer's manager.
132
+ * @param {boolean} [debug] - Whether to enable debug logging.
133
+ * @returns {Promise<SKRSContext2D>} The canvas rendering context after rendering.
134
+ */
135
+ async renderLayer(layer, ctx, canvas, manager, debug) {
136
+ if (debug)
137
+ utils_1.LazyLog.log("info", `Rendering ${layer.id}...\nData:`, layer.toJSON());
138
+ if (layer.visible) {
139
+ ctx.globalCompositeOperation = layer.props?.globalComposite || "source-over";
140
+ await layer.draw(ctx, canvas, manager, debug);
141
+ // Draw children if any (and not a Div, as Div handles its own children)
142
+ // Actually, if we want to support children on any layer, we should handle it here.
143
+ // But Div.draw already handles children.
144
+ // If we handle it here for Div, we get double rendering.
145
+ // So we skip Div.
146
+ const children = layer.children;
147
+ if (!(layer instanceof Div) && children && Array.isArray(children) && children.length > 0) {
148
+ ctx.save();
149
+ // Apply parent position offset
150
+ // LayoutManager sets position relative to parent.
151
+ // We need to translate context to parent's position so children are drawn relative to it.
152
+ // However, layer.draw() might have already drawn the layer at that position.
153
+ // And layer.draw() usually restores context.
154
+ // So we are back at parent's parent coordinate system.
155
+ // We need to translate to layer's position.
156
+ if (layer.props.position) {
157
+ const x = typeof layer.props.position.x === "number" ? layer.props.position.x : 0;
158
+ const y = typeof layer.props.position.y === "number" ? layer.props.position.y : 0;
159
+ ctx.translate(x, y);
160
+ }
161
+ for (const child of children) {
162
+ await this.renderLayer(child, ctx, canvas, manager, debug);
163
+ }
164
+ ctx.restore();
165
+ }
166
+ ctx.shadowColor = "transparent";
167
+ }
168
+ return ctx;
169
+ }
170
+ async draw(ctx, canvas, manager, debug) {
171
+ ctx.save();
172
+ // Apply position translation if available (from layout)
173
+ if (this.props.position) {
174
+ const x = typeof this.props.position.x === "number" ? this.props.position.x : 0;
175
+ const y = typeof this.props.position.y === "number" ? this.props.position.y : 0;
176
+ ctx.translate(x, y);
177
+ }
178
+ for (const subLayer of this.layers) {
179
+ if (debug)
180
+ utils_1.LazyLog.log("info", `Rendering ${subLayer.id}...\nData:`, subLayer.toJSON());
181
+ if (subLayer.visible) {
182
+ await this.renderLayer(subLayer, ctx, canvas, manager, debug);
183
+ }
184
+ }
185
+ ctx.restore();
186
+ }
187
+ /**
188
+ * Converts the group to a JSON representation.
189
+ * @returns {IDiv} The JSON representation of the group.
190
+ */
191
+ toJSON() {
192
+ return {
193
+ id: this.id,
194
+ type: this.type,
195
+ visible: this.visible,
196
+ zIndex: this.zIndex,
197
+ // @ts-ignore
198
+ layers: this.layers.map((c) => c.toJSON()),
199
+ };
200
+ }
201
+ }
202
+ exports.Div = Div;
@@ -38,7 +38,7 @@ export interface IImageLayerProps extends IBaseLayerProps {
38
38
  /**
39
39
  * The radius of the image.
40
40
  */
41
- radius: {
41
+ radius?: {
42
42
  [corner in RadiusCorner]?: ScaleType;
43
43
  };
44
44
  };
@@ -4,17 +4,12 @@ exports.ImageLayer = void 0;
4
4
  const BaseLayer_1 = require("./BaseLayer");
5
5
  const types_1 = require("../../types");
6
6
  const canvas_1 = require("@napi-rs/canvas");
7
- const utils_1 = require("../../utils/utils");
8
- const LazyUtil_1 = require("../../utils/LazyUtil");
7
+ const utils_1 = require("../../utils");
9
8
  const helpers_1 = require("../helpers");
10
9
  /**
11
10
  * Class representing an Image Layer, extending the BaseLayer class.
12
11
  */
13
12
  class ImageLayer extends BaseLayer_1.BaseLayer {
14
- /**
15
- * The properties of the Image Layer.
16
- */
17
- props;
18
13
  /**
19
14
  * Constructs a new ImageLayer instance.
20
15
  * @param {IImageLayerProps} [props] - The properties of the Image Layer.
@@ -33,7 +28,7 @@ class ImageLayer extends BaseLayer_1.BaseLayer {
33
28
  */
34
29
  setSrc(src) {
35
30
  if (!(0, utils_1.isImageUrlValid)(src))
36
- throw new LazyUtil_1.LazyError('The src of the image must be a valid URL');
31
+ throw new utils_1.LazyError("The src of the image must be a valid URL");
37
32
  this.props.src = src;
38
33
  return this;
39
34
  }
@@ -63,38 +58,42 @@ class ImageLayer extends BaseLayer_1.BaseLayer {
63
58
  async draw(ctx, canvas, manager, debug) {
64
59
  const parcer = (0, utils_1.parser)(ctx, canvas, manager);
65
60
  const { xs, ys, w } = parcer.parseBatch({
66
- xs: { v: this.props.x },
67
- ys: { v: this.props.y, options: LazyUtil_1.defaultArg.vl(true) },
68
- w: { v: this.props.size.width }
61
+ xs: { v: this.props.position?.x || 0 },
62
+ ys: { v: this.props.position?.y || 0, options: utils_1.defaultArg.vl(true) },
63
+ w: { v: this.props.size.width },
69
64
  });
70
- const h = parcer.parse(this.props.size.height, LazyUtil_1.defaultArg.wh(w), LazyUtil_1.defaultArg.vl(true));
65
+ const h = parcer.parse(this.props.size.height, utils_1.defaultArg.wh(w), utils_1.defaultArg.vl(true));
71
66
  let { x, y } = (0, utils_1.centring)(this.props.centring, this.type, w, h, xs, ys);
72
67
  const rad = {};
73
- if (typeof this.props.size.radius === 'object' && this.props.size.radius !== helpers_1.Link) {
68
+ if (typeof this.props.size.radius === "object" && this.props.size.radius !== helpers_1.Link) {
74
69
  for (const corner in this.props.size.radius) {
75
70
  // @ts-ignore
76
- rad[corner] = parcer.parse(this.props.size.radius[corner], LazyUtil_1.defaultArg.wh(w / 2, h / 2), LazyUtil_1.defaultArg.vl(false, true));
71
+ rad[corner] = parcer.parse(
72
+ // @ts-ignore
73
+ this.props.size.radius[corner], utils_1.defaultArg.wh(w / 2, h / 2), utils_1.defaultArg.vl(false, true));
77
74
  }
78
75
  }
79
76
  if (debug)
80
- LazyUtil_1.LazyLog.log('none', `ImageLayer:`, { x, y, w, h, rad });
77
+ utils_1.LazyLog.log("none", `ImageLayer:`, { x, y, w, h, rad });
81
78
  ctx.save();
82
79
  let image = await (0, canvas_1.loadImage)(this.props.src);
83
80
  image.width = w;
84
81
  image.height = h;
85
82
  if (!image)
86
- throw new LazyUtil_1.LazyError('The image could not be loaded');
87
- (0, utils_1.transform)(ctx, this.props.transform, { width: w, height: h, x, y, type: this.type });
88
- (0, utils_1.drawShadow)(ctx, this.props.shadow);
89
- (0, utils_1.opacity)(ctx, this.props.opacity);
90
- (0, utils_1.filters)(ctx, this.props.filter);
83
+ throw new utils_1.LazyError("The image could not be loaded");
84
+ if (this.props.transform) {
85
+ (0, utils_1.transform)(ctx, this.props.transform, { width: w, height: h, x, y, type: this.type });
86
+ }
87
+ utils_1.DrawUtils.drawShadow(ctx, this.props.shadow);
88
+ utils_1.DrawUtils.opacity(ctx, this.props.opacity);
89
+ utils_1.DrawUtils.filters(ctx, this.props.filter);
91
90
  if (Object.keys(rad).length > 0) {
92
91
  ctx.beginPath();
93
- ctx.moveTo(x + (w / 2), y);
94
- ctx.arcTo(x + w, y, x + w, y + (h / 2), rad.rightTop || rad.all || 0);
95
- ctx.arcTo(x + w, y + h, x + (w / 2), y + h, rad.rightBottom || rad.all || 0);
96
- ctx.arcTo(x, y + h, x, y + (h / 2), rad.leftBottom || rad.all || 0);
97
- ctx.arcTo(x, y, x + (w / 2), y, rad.leftTop || rad.all || 0);
92
+ ctx.moveTo(x + w / 2, y);
93
+ ctx.arcTo(x + w, y, x + w, y + h / 2, rad.rightTop || rad.all || 0);
94
+ ctx.arcTo(x + w, y + h, x + w / 2, y + h, rad.rightBottom || rad.all || 0);
95
+ ctx.arcTo(x, y + h, x, y + h / 2, rad.leftBottom || rad.all || 0);
96
+ ctx.arcTo(x, y, x + w / 2, y, rad.leftTop || rad.all || 0);
98
97
  ctx.closePath();
99
98
  ctx.clip();
100
99
  ctx.drawImage(image, x, y, w, h);
@@ -111,8 +110,8 @@ class ImageLayer extends BaseLayer_1.BaseLayer {
111
110
  toJSON() {
112
111
  let data = super.toJSON();
113
112
  let copy = { ...this.props };
114
- for (const key of ['x', 'y', 'size.width', 'size.height', 'size.radius']) {
115
- if (copy[key] && typeof copy[key] === 'object' && 'toJSON' in copy[key]) {
113
+ for (const key of ["x", "y", "size.width", "size.height", "size.radius"]) {
114
+ if (copy[key] && typeof copy[key] === "object" && "toJSON" in copy[key]) {
116
115
  copy[key] = copy[key].toJSON();
117
116
  }
118
117
  }
@@ -126,12 +125,12 @@ class ImageLayer extends BaseLayer_1.BaseLayer {
126
125
  validateProps(data) {
127
126
  return {
128
127
  ...super.validateProps(data),
129
- src: data.src || '',
128
+ src: data.src || "",
130
129
  size: {
131
130
  width: data.size?.width || 0,
132
131
  height: data.size?.height || 0,
133
- radius: data.size?.radius || { all: 0 }
134
- }
132
+ radius: data.size?.radius || { all: 0 },
133
+ },
135
134
  };
136
135
  }
137
136
  }
@@ -1,5 +1,5 @@
1
1
  import { BaseLayer, IBaseLayer, IBaseLayerMisc, IBaseLayerProps } from "./BaseLayer";
2
- import { ColorType, ScaleType, LayerType } from "../../types";
2
+ import { ColorType, ScaleType, LayerType, StrokeOptions } from "../../types";
3
3
  import { Canvas, SKRSContext2D, SvgCanvas } from "@napi-rs/canvas";
4
4
  import { LayersManager } from "../managers";
5
5
  /**
@@ -19,18 +19,23 @@ export interface ILineLayer extends IBaseLayer {
19
19
  * Interface representing the properties of a Line Layer.
20
20
  */
21
21
  export interface ILineLayerProps extends IBaseLayerProps {
22
- /**
23
- * The end point of the line, including x and y coordinates.
24
- */
25
- endPoint: {
22
+ position: {
26
23
  /**
27
- * The x-coordinate of the end point.
24
+ * The x-coordinate of the start point of the line.
28
25
  */
29
26
  x: ScaleType;
30
27
  /**
31
- * The y-coordinate of the end point.
28
+ * The y-coordinate of the start point of the line.
32
29
  */
33
30
  y: ScaleType;
31
+ /**
32
+ * The x-coordinate of the end point of the line.
33
+ */
34
+ endX: ScaleType;
35
+ /**
36
+ * The y-coordinate of the end point of the line.
37
+ */
38
+ endY: ScaleType;
34
39
  };
35
40
  /**
36
41
  * Whether the layer is filled.
@@ -39,36 +44,11 @@ export interface ILineLayerProps extends IBaseLayerProps {
39
44
  /**
40
45
  * The fill style (color or pattern) of the layer.
41
46
  */
42
- fillStyle: ColorType;
47
+ color: ColorType;
43
48
  /**
44
49
  * The stroke properties of the line.
45
50
  */
46
- stroke: {
47
- /**
48
- * The width of the stroke.
49
- */
50
- width: number;
51
- /**
52
- * The cap style of the stroke.
53
- */
54
- cap: CanvasLineCap;
55
- /**
56
- * The join style of the stroke.
57
- */
58
- join: CanvasLineJoin;
59
- /**
60
- * The dash offset of the stroke.
61
- */
62
- dashOffset: number;
63
- /**
64
- * The dash pattern of the stroke.
65
- */
66
- dash: number[];
67
- /**
68
- * The miter limit of the stroke.
69
- */
70
- miterLimit: number;
71
- };
51
+ stroke: StrokeOptions;
72
52
  }
73
53
  /**
74
54
  * Class representing a Line Layer, extending the BaseLayer class.
@@ -85,12 +65,14 @@ export declare class LineLayer extends BaseLayer<ILineLayerProps> {
85
65
  */
86
66
  constructor(props?: ILineLayerProps, misc?: IBaseLayerMisc);
87
67
  /**
88
- * Sets the end position of the line layer.
68
+ * Sets the position of the line layer.
89
69
  * @param {ScaleType} [x] - The x-coordinate of the end point.
90
70
  * @param {ScaleType} [y] - The y-coordinate of the end point.
71
+ * @param {ScaleType} [endX] - The x-coordinate of the end point.
72
+ * @param {ScaleType} [endY] - The y-coordinate of the end point.
91
73
  * @returns {this} The current instance for chaining.
92
74
  */
93
- setEndPosition(x: ScaleType, y: ScaleType): this;
75
+ setPosition(x: ScaleType, y: ScaleType, endX?: ScaleType, endY?: ScaleType): this;
94
76
  /**
95
77
  * Sets the color of the line layer.
96
78
  * @param {ColorType} [color] - The color of the layer.
@@ -3,16 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LineLayer = void 0;
4
4
  const BaseLayer_1 = require("./BaseLayer");
5
5
  const types_1 = require("../../types");
6
- const LazyUtil_1 = require("../../utils/LazyUtil");
7
- const utils_1 = require("../../utils/utils");
6
+ const utils_1 = require("../../utils");
8
7
  /**
9
8
  * Class representing a Line Layer, extending the BaseLayer class.
10
9
  */
11
10
  class LineLayer extends BaseLayer_1.BaseLayer {
12
- /**
13
- * The properties of the Line Layer.
14
- */
15
- props;
16
11
  /**
17
12
  * Constructs a new LineLayer instance.
18
13
  * @param {ILineLayerProps} [props] - The properties of the Line Layer.
@@ -24,13 +19,15 @@ class LineLayer extends BaseLayer_1.BaseLayer {
24
19
  this.props = this.validateProps(this.props);
25
20
  }
26
21
  /**
27
- * Sets the end position of the line layer.
22
+ * Sets the position of the line layer.
28
23
  * @param {ScaleType} [x] - The x-coordinate of the end point.
29
24
  * @param {ScaleType} [y] - The y-coordinate of the end point.
25
+ * @param {ScaleType} [endX] - The x-coordinate of the end point.
26
+ * @param {ScaleType} [endY] - The y-coordinate of the end point.
30
27
  * @returns {this} The current instance for chaining.
31
28
  */
32
- setEndPosition(x, y) {
33
- this.props.endPoint = { x, y };
29
+ setPosition(x, y, endX, endY) {
30
+ this.props.position = { x, y, endX: endX || 0, endY: endY || 0 };
34
31
  return this;
35
32
  }
36
33
  /**
@@ -41,10 +38,10 @@ class LineLayer extends BaseLayer_1.BaseLayer {
41
38
  */
42
39
  setColor(color) {
43
40
  if (!color)
44
- throw new LazyUtil_1.LazyError('The color of the layer must be provided');
41
+ throw new utils_1.LazyError("The color of the layer must be provided");
45
42
  if (!(0, utils_1.isColor)(color))
46
- throw new LazyUtil_1.LazyError('The color of the layer must be a valid color');
47
- this.props.fillStyle = color;
43
+ throw new utils_1.LazyError("The color of the layer must be a valid color");
44
+ this.props.color = color;
48
45
  return this;
49
46
  }
50
47
  /**
@@ -60,8 +57,8 @@ class LineLayer extends BaseLayer_1.BaseLayer {
60
57
  setStroke(width, cap, join, dash, dashOffset, miterLimit) {
61
58
  this.props.stroke = {
62
59
  width,
63
- cap: cap || 'butt',
64
- join: join || 'miter',
60
+ cap: cap || "butt",
61
+ join: join || "miter",
65
62
  dash: dash || [],
66
63
  dashOffset: dashOffset || 0,
67
64
  miterLimit: miterLimit || 10,
@@ -78,10 +75,10 @@ class LineLayer extends BaseLayer_1.BaseLayer {
78
75
  getBoundingBox(ctx, canvas, manager) {
79
76
  const parcer = (0, utils_1.parser)(ctx, canvas, manager);
80
77
  const { xs, ys, xe, ye } = parcer.parseBatch({
81
- xs: { v: this.props.x },
82
- ys: { v: this.props.y, options: LazyUtil_1.defaultArg.vl(true) },
83
- xe: { v: this.props.endPoint.x },
84
- ye: { v: this.props.endPoint.y, options: LazyUtil_1.defaultArg.vl(true) },
78
+ xs: { v: this.props.position.x },
79
+ ys: { v: this.props.position.y, options: utils_1.defaultArg.vl(true) },
80
+ xe: { v: this.props.position.endX },
81
+ ye: { v: this.props.position.endY, options: utils_1.defaultArg.vl(true) },
85
82
  });
86
83
  let width = xe - xs;
87
84
  let height = ye - ys;
@@ -97,30 +94,30 @@ class LineLayer extends BaseLayer_1.BaseLayer {
97
94
  async draw(ctx, canvas, manager, debug) {
98
95
  const parcer = (0, utils_1.parser)(ctx, canvas, manager);
99
96
  const { xs, ys, xe, ye } = parcer.parseBatch({
100
- xs: { v: this.props.x },
101
- ys: { v: this.props.y, options: LazyUtil_1.defaultArg.vl(true) },
102
- xe: { v: this.props.endPoint.x },
103
- ye: { v: this.props.endPoint.y, options: LazyUtil_1.defaultArg.vl(true) },
97
+ xs: { v: this.props.position.x },
98
+ ys: { v: this.props.position.y, options: utils_1.defaultArg.vl(true) },
99
+ xe: { v: this.props.position.endX },
100
+ ye: { v: this.props.position.endY, options: utils_1.defaultArg.vl(true) },
104
101
  });
105
102
  let width = Math.abs(xe - xs);
106
103
  let height = Math.abs(ye - ys);
107
- let fillStyle = await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle, { debug, layer: { width, height, x: Math.min(xs, xe), y: Math.min(ys, ye), align: 'none' }, manager });
104
+ let fillStyle = await (0, utils_1.parseFillStyle)(ctx, this.props.color, {
105
+ debug,
106
+ layer: { width, height, x: Math.min(xs, xe), y: Math.min(ys, ye), align: "none" },
107
+ manager,
108
+ });
108
109
  if (debug)
109
- LazyUtil_1.LazyLog.log('none', `LineLayer:`, { xs, ys, xe, ye, width, height });
110
+ utils_1.LazyLog.log("none", `LineLayer:`, { xs, ys, xe, ye, width, height });
110
111
  ctx.save();
111
- (0, utils_1.transform)(ctx, this.props.transform, { x: xs, y: ys, width, height, type: this.type });
112
- (0, utils_1.drawShadow)(ctx, this.props.shadow);
113
- (0, utils_1.opacity)(ctx, this.props.opacity);
114
- (0, utils_1.filters)(ctx, this.props.filter);
112
+ if (this.props.transform) {
113
+ (0, utils_1.transform)(ctx, this.props.transform, { x: xs, y: ys, width, height, type: this.type });
114
+ }
115
+ utils_1.DrawUtils.drawShadow(ctx, this.props.shadow);
116
+ utils_1.DrawUtils.opacity(ctx, this.props.opacity);
117
+ utils_1.DrawUtils.filters(ctx, this.props.filter);
118
+ utils_1.DrawUtils.fillStyle(ctx, fillStyle, this.props.stroke);
115
119
  ctx.beginPath();
116
120
  ctx.moveTo(xs, ys);
117
- ctx.strokeStyle = fillStyle;
118
- ctx.lineWidth = this.props.stroke?.width || 1;
119
- ctx.lineCap = this.props.stroke?.cap || 'butt';
120
- ctx.lineJoin = this.props.stroke?.join || 'miter';
121
- ctx.miterLimit = this.props.stroke?.miterLimit || 10;
122
- ctx.lineDashOffset = this.props.stroke?.dashOffset || 0;
123
- ctx.setLineDash(this.props.stroke?.dash || []);
124
121
  ctx.lineTo(xe, ye);
125
122
  ctx.stroke();
126
123
  ctx.closePath();
@@ -133,8 +130,8 @@ class LineLayer extends BaseLayer_1.BaseLayer {
133
130
  toJSON() {
134
131
  let data = super.toJSON();
135
132
  let copy = { ...this.props };
136
- for (const key of ['x', 'y', 'endPoint.x', 'endPoint.y', 'fillStyle']) {
137
- if (copy[key] && typeof copy[key] === 'object' && 'toJSON' in copy[key]) {
133
+ for (const key of ["x", "y", "endPoint.x", "endPoint.y", "fillStyle"]) {
134
+ if (copy[key] && typeof copy[key] === "object" && "toJSON" in copy[key]) {
138
135
  copy[key] = copy[key].toJSON();
139
136
  }
140
137
  }
@@ -148,17 +145,18 @@ class LineLayer extends BaseLayer_1.BaseLayer {
148
145
  validateProps(data) {
149
146
  return {
150
147
  ...super.validateProps(data),
151
- filled: data.filled || false,
152
- fillStyle: data.fillStyle || '#000000',
153
- centring: data.centring || types_1.Centring.None,
154
- endPoint: {
155
- x: data.endPoint?.x || 0,
156
- y: data.endPoint?.y || 0,
148
+ position: {
149
+ x: data.position?.x || 0,
150
+ y: data.position?.y || 0,
151
+ endX: data.position?.endX || 0,
152
+ endY: data.position?.endY || 0,
157
153
  },
154
+ color: data.color || "#000000",
155
+ centring: data.centring || types_1.Centring.None,
158
156
  stroke: {
159
157
  width: data.stroke?.width || 1,
160
- cap: data.stroke?.cap || 'butt',
161
- join: data.stroke?.join || 'miter',
158
+ cap: data.stroke?.cap || "butt",
159
+ join: data.stroke?.join || "miter",
162
160
  dashOffset: data.stroke?.dashOffset || 0,
163
161
  dash: data.stroke?.dash || [],
164
162
  miterLimit: data.stroke?.miterLimit || 10,
@@ -1,5 +1,5 @@
1
1
  import { BaseLayer, IBaseLayer, IBaseLayerMisc, IBaseLayerProps } from "./BaseLayer";
2
- import { ColorType, ScaleType, LayerType, RadiusCorner } from "../../types";
2
+ import { ColorType, ScaleType, LayerType, RadiusCorner, StrokeOptions } from "../../types";
3
3
  import { Canvas, SKRSContext2D, SvgCanvas } from "@napi-rs/canvas";
4
4
  import { LayersManager } from "../managers";
5
5
  /**
@@ -34,47 +34,18 @@ export interface IMorphLayerProps extends IBaseLayerProps {
34
34
  /**
35
35
  * The radius of the Morph Layer.
36
36
  */
37
- radius: {
37
+ radius?: {
38
38
  [corner in RadiusCorner]?: ScaleType;
39
39
  };
40
40
  };
41
- /**
42
- * Whether the layer is filled.
43
- */
44
- filled: boolean;
45
41
  /**
46
42
  * The fill style (color or pattern) of the layer.
47
43
  */
48
- fillStyle: ColorType;
44
+ color: ColorType;
49
45
  /**
50
46
  * The stroke properties of the morph.
51
47
  */
52
- stroke: {
53
- /**
54
- * The width of the stroke.
55
- */
56
- width: number;
57
- /**
58
- * The cap style of the stroke.
59
- */
60
- cap: CanvasLineCap;
61
- /**
62
- * The join style of the stroke.
63
- */
64
- join: CanvasLineJoin;
65
- /**
66
- * The dash offset of the stroke.
67
- */
68
- dashOffset: number;
69
- /**
70
- * The dash pattern of the stroke.
71
- */
72
- dash: number[];
73
- /**
74
- * The miter limit of the stroke.
75
- */
76
- miterLimit: number;
77
- };
48
+ stroke?: StrokeOptions;
78
49
  }
79
50
  /**
80
51
  * Class representing a Morph Layer, extending the BaseLayer class.