@nmmty/lazycanvas 0.5.3 → 0.6.0-dev.1d5786

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 (34) hide show
  1. package/dist/helpers/Utlis.d.ts +28 -0
  2. package/dist/helpers/Utlis.js +78 -0
  3. package/dist/index.d.ts +2 -1
  4. package/dist/index.js +4 -1
  5. package/dist/structures/components/BaseLayer.d.ts +3 -2
  6. package/dist/structures/components/BaseLayer.js +15 -10
  7. package/dist/structures/components/BezierLayer.d.ts +6 -0
  8. package/dist/structures/components/BezierLayer.js +23 -4
  9. package/dist/structures/components/ClearLayer.d.ts +1 -0
  10. package/dist/structures/components/ClearLayer.js +14 -8
  11. package/dist/structures/components/ImageLayer.d.ts +6 -0
  12. package/dist/structures/components/ImageLayer.js +21 -5
  13. package/dist/structures/components/LineLayer.d.ts +6 -0
  14. package/dist/structures/components/LineLayer.js +27 -6
  15. package/dist/structures/components/MorphLayer.d.ts +6 -6
  16. package/dist/structures/components/MorphLayer.js +26 -15
  17. package/dist/structures/components/Path2DLayer.d.ts +6 -6
  18. package/dist/structures/components/Path2DLayer.js +24 -10
  19. package/dist/structures/components/QuadraticLayer.d.ts +6 -0
  20. package/dist/structures/components/QuadraticLayer.js +23 -4
  21. package/dist/structures/components/TextLayer.d.ts +11 -11
  22. package/dist/structures/components/TextLayer.js +36 -32
  23. package/dist/structures/helpers/Gradient.d.ts +48 -11
  24. package/dist/structures/helpers/Gradient.js +133 -8
  25. package/dist/structures/helpers/index.d.ts +1 -0
  26. package/dist/structures/helpers/index.js +1 -0
  27. package/dist/structures/helpers/readers/JSONReader.js +3 -3
  28. package/dist/structures/helpers/readers/YAMLReader.d.ts +22 -0
  29. package/dist/structures/helpers/readers/YAMLReader.js +73 -0
  30. package/dist/utils/utils.d.ts +13 -3
  31. package/dist/utils/utils.js +59 -92
  32. package/package.json +6 -4
  33. package/animation.gif +0 -0
  34. package/test.png +0 -0
@@ -0,0 +1,28 @@
1
+ import { Group } from "../structures/components";
2
+ import { ColorType } from "../types";
3
+ declare const Utils: {
4
+ grid(size: {
5
+ x: number;
6
+ y: number;
7
+ }, opts?: gridOptions): Group;
8
+ box(start: {
9
+ x: number;
10
+ y: number;
11
+ }, end: {
12
+ x: number;
13
+ y: number;
14
+ }, opts?: options): Group;
15
+ };
16
+ interface options {
17
+ color?: ColorType;
18
+ lineWidth?: number;
19
+ }
20
+ interface gridOptions extends options {
21
+ cellWith?: number;
22
+ cellHeight?: number;
23
+ startX?: number;
24
+ startY?: number;
25
+ endX?: number;
26
+ endY?: number;
27
+ }
28
+ export { Utils };
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Utils = void 0;
4
+ const components_1 = require("../structures/components");
5
+ const Utils = {
6
+ grid(size, opts) {
7
+ if (size.x === undefined || size.y === undefined) {
8
+ throw new Error("Size must have x and y properties");
9
+ }
10
+ if (opts === undefined)
11
+ opts = {};
12
+ if (opts.cellWith === undefined)
13
+ opts.cellWith = 10;
14
+ if (opts.cellHeight === undefined)
15
+ opts.cellHeight = 10;
16
+ if (opts.startX === undefined)
17
+ opts.startX = 0;
18
+ if (opts.startY === undefined)
19
+ opts.startY = 0;
20
+ if (opts.endX === undefined)
21
+ opts.endX = size.x;
22
+ if (opts.endY === undefined)
23
+ opts.endY = size.y;
24
+ if (opts.color === undefined)
25
+ opts.color = 'rgba(0, 0, 0, 0.5)';
26
+ if (opts.lineWidth === undefined)
27
+ opts.lineWidth = 1;
28
+ const options = { ...opts };
29
+ return new components_1.Group()
30
+ .setID(`grid-${options.cellWith}-${options.cellHeight}-${options.startX}-${options.startY}-${options.endX}-${options.endY}`)
31
+ .add(...Array.from({ length: Math.ceil((options.endX - options.startX) / options.cellWith) }, (_, i) => {
32
+ const x = options.startX + i * options.cellWith;
33
+ return new components_1.LineLayer()
34
+ .setPosition(x, options.startY)
35
+ .setEndPosition(x, options.endY)
36
+ .setColor(options.color)
37
+ .setStroke(options.lineWidth);
38
+ }), ...Array.from({ length: Math.ceil((options.endY - options.startY) / options.cellHeight) }, (_, i) => {
39
+ const y = options.startY + i * options.cellHeight;
40
+ return new components_1.LineLayer()
41
+ .setPosition(options.startX, y)
42
+ .setEndPosition(options.endX, y)
43
+ .setColor(options.color)
44
+ .setStroke(options.lineWidth);
45
+ }));
46
+ },
47
+ box(start, end, opts) {
48
+ if (start.x === undefined || start.y === undefined || end.x === undefined || end.y === undefined) {
49
+ throw new Error("Start and end must have x and y properties");
50
+ }
51
+ if (opts === undefined)
52
+ opts = {};
53
+ if (opts.color === undefined)
54
+ opts.color = 'rgba(0, 0, 0, 0.5)';
55
+ if (opts.lineWidth === undefined)
56
+ opts.lineWidth = 1;
57
+ return new components_1.Group()
58
+ .setID(`box-${start.x}-${start.y}-${end.x}-${end.y}`)
59
+ .add(new components_1.LineLayer()
60
+ .setPosition(start.x, start.y)
61
+ .setEndPosition(end.x, start.y)
62
+ .setColor(opts.color)
63
+ .setStroke(opts.lineWidth), new components_1.LineLayer()
64
+ .setPosition(end.x, start.y)
65
+ .setEndPosition(end.x, end.y)
66
+ .setColor(opts.color)
67
+ .setStroke(opts.lineWidth), new components_1.LineLayer()
68
+ .setPosition(end.x, end.y)
69
+ .setEndPosition(start.x, end.y)
70
+ .setColor(opts.color)
71
+ .setStroke(opts.lineWidth), new components_1.LineLayer()
72
+ .setPosition(start.x, end.y)
73
+ .setEndPosition(start.x, start.y)
74
+ .setColor(opts.color)
75
+ .setStroke(opts.lineWidth));
76
+ }
77
+ };
78
+ exports.Utils = Utils;
package/dist/index.d.ts CHANGED
@@ -5,7 +5,8 @@ export { IRenderManager } from "./structures/managers/RenderManager";
5
5
  export { ILayersManager } from "./structures/managers/LayersManager";
6
6
  export * from "./structures/components";
7
7
  export { LayerType, LayerScaleType, LineCap, LineJoin, TextAlign, TextDirection, TextBaseline, FontWeight, Export, Centring, PatternType, GradientType, LinkType } from "./types";
8
- export { Font, IFont, IFonts, Gradient, IGradient, GradientPoint, GradientColorStop, Pattern, IPattern, Link, ILink, Exporter, JSONReader } from "./structures/helpers";
8
+ export { Font, IFont, IFonts, Gradient, IGradient, GradientPoint, GradientColorStop, Pattern, IPattern, Link, ILink, Exporter, JSONReader, YAMLReader } from "./structures/helpers";
9
9
  export { Filters } from "./helpers/Filters";
10
10
  export { FontsList } from "./helpers/FontsList";
11
11
  export type { AnyLayer, AnyCentring, AnyPatternType, AnyGradientType, AnyTextAlign, AnyTextDirection, AnyTextBaseline, AnyWeight, AnyLineCap, AnyLineJoin, AnyExport, AnyLinkType, AnyGlobalCompositeOperation, AnyColorSpace, ScaleType, ColorType, Point, PointNumber, Transform, Extensions } from "./types";
12
+ export { Utils } from "./helpers/Utlis";
package/dist/index.js CHANGED
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.FontsList = exports.Filters = exports.JSONReader = exports.Exporter = exports.Link = exports.Pattern = exports.Gradient = exports.Font = exports.LinkType = exports.GradientType = exports.PatternType = exports.Centring = exports.Export = exports.FontWeight = exports.TextBaseline = exports.TextDirection = exports.TextAlign = exports.LineJoin = exports.LineCap = exports.LayerScaleType = exports.LayerType = exports.LazyCanvas = void 0;
17
+ exports.Utils = exports.FontsList = exports.Filters = exports.YAMLReader = exports.JSONReader = exports.Exporter = exports.Link = exports.Pattern = exports.Gradient = exports.Font = exports.LinkType = exports.GradientType = exports.PatternType = exports.Centring = exports.Export = exports.FontWeight = exports.TextBaseline = exports.TextDirection = exports.TextAlign = exports.LineJoin = exports.LineCap = exports.LayerScaleType = exports.LayerType = exports.LazyCanvas = void 0;
18
18
  var LazyCanvas_1 = require("./structures/LazyCanvas");
19
19
  Object.defineProperty(exports, "LazyCanvas", { enumerable: true, get: function () { return LazyCanvas_1.LazyCanvas; } });
20
20
  __exportStar(require("./structures/components"), exports);
@@ -39,7 +39,10 @@ Object.defineProperty(exports, "Pattern", { enumerable: true, get: function () {
39
39
  Object.defineProperty(exports, "Link", { enumerable: true, get: function () { return helpers_1.Link; } });
40
40
  Object.defineProperty(exports, "Exporter", { enumerable: true, get: function () { return helpers_1.Exporter; } });
41
41
  Object.defineProperty(exports, "JSONReader", { enumerable: true, get: function () { return helpers_1.JSONReader; } });
42
+ Object.defineProperty(exports, "YAMLReader", { enumerable: true, get: function () { return helpers_1.YAMLReader; } });
42
43
  var Filters_1 = require("./helpers/Filters");
43
44
  Object.defineProperty(exports, "Filters", { enumerable: true, get: function () { return Filters_1.Filters; } });
44
45
  var FontsList_1 = require("./helpers/FontsList");
45
46
  Object.defineProperty(exports, "FontsList", { enumerable: true, get: function () { return FontsList_1.FontsList; } });
47
+ var Utlis_1 = require("./helpers/Utlis");
48
+ Object.defineProperty(exports, "Utils", { enumerable: true, get: function () { return Utlis_1.Utils; } });
@@ -43,7 +43,7 @@ export interface IBaseLayerProps {
43
43
  /**
44
44
  * The filter effects applied to the layer.
45
45
  */
46
- filter: string;
46
+ filter?: string;
47
47
  /**
48
48
  * The opacity of the layer, ranging from 0 to 1.
49
49
  */
@@ -59,7 +59,7 @@ export interface IBaseLayerProps {
59
59
  /**
60
60
  * The shadow properties of the layer.
61
61
  */
62
- shadow: {
62
+ shadow?: {
63
63
  /**
64
64
  * The color of the shadow.
65
65
  */
@@ -233,4 +233,5 @@ export declare class BaseLayer<T extends IBaseLayerProps> {
233
233
  * @returns {IBaseLayer} The JSON representation of the layer.
234
234
  */
235
235
  toJSON(): IBaseLayer;
236
+ protected validateProps(data: T): T;
236
237
  }
@@ -48,16 +48,7 @@ class BaseLayer {
48
48
  this.zIndex = misc?.zIndex || 1;
49
49
  this.visible = misc?.visible || true;
50
50
  this.props = props ? props : {};
51
- if (!this.props.x)
52
- this.props.x = 0;
53
- if (!this.props.y)
54
- this.props.y = 0;
55
- if (!this.props.opacity)
56
- this.props.opacity = 1;
57
- if (!this.props.centring)
58
- this.props.centring = types_1.Centring.Center;
59
- if (!this.props.transform)
60
- this.props.transform = {};
51
+ this.props = this.validateProps(this.props);
61
52
  }
62
53
  /**
63
54
  * Sets the position of the layer in the 2D plane.
@@ -207,5 +198,19 @@ class BaseLayer {
207
198
  props: this.props,
208
199
  };
209
200
  }
201
+ validateProps(data) {
202
+ return {
203
+ ...data,
204
+ x: data.x || 0,
205
+ y: data.y || 0,
206
+ centring: data.centring || types_1.Centring.Center,
207
+ filter: data.filter || '',
208
+ opacity: data.opacity || 1,
209
+ filled: data.filled || true,
210
+ fillStyle: data.fillStyle || '#000000',
211
+ transform: data.transform || {},
212
+ globalComposite: data.globalComposite || 'source-over',
213
+ };
214
+ }
210
215
  }
211
216
  exports.BaseLayer = BaseLayer;
@@ -133,4 +133,10 @@ export declare class BezierLayer extends BaseLayer<IBezierLayerProps> {
133
133
  * @returns {IBezierLayer} The JSON representation of the Bezier layer.
134
134
  */
135
135
  toJSON(): IBezierLayer;
136
+ /**
137
+ * Validates the properties of the Bezier layer.
138
+ * @param data {IBezierLayerProps} - The properties to validate.
139
+ * @returns {IBezierLayerProps} The validated properties.
140
+ */
141
+ protected validateProps(data: IBezierLayerProps): IBezierLayerProps;
136
142
  }
@@ -21,9 +21,7 @@ class BezierLayer extends BaseLayer_1.BaseLayer {
21
21
  constructor(props, misc) {
22
22
  super(types_1.LayerType.BezierCurve, props || {}, misc);
23
23
  this.props = props ? props : {};
24
- if (!this.props.fillStyle)
25
- this.props.fillStyle = '#000000';
26
- this.props.centring = types_1.Centring.None;
24
+ this.props = this.validateProps(this.props);
27
25
  }
28
26
  /**
29
27
  * Sets the control points of the Bezier layer.
@@ -124,7 +122,7 @@ class BezierLayer extends BaseLayer_1.BaseLayer {
124
122
  ye: { v: this.props.endPoint.y, options: LazyUtil_1.defaultArg.vl(true) }
125
123
  });
126
124
  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 }]);
127
- let fillStyle = await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle);
125
+ let fillStyle = await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle, { debug, layer: { width, height, x: min.x, y: min.y, align: 'none' }, manager });
128
126
  if (debug)
129
127
  LazyUtil_1.LazyLog.log('none', `BezierLayer:`, { xs, ys, cp1x, cp1y, cp2x, cp2y, xe, ye, max, min, center, width, height, fillStyle });
130
128
  ctx.save();
@@ -173,5 +171,26 @@ class BezierLayer extends BaseLayer_1.BaseLayer {
173
171
  }
174
172
  return { ...data, props: copy };
175
173
  }
174
+ /**
175
+ * Validates the properties of the Bezier layer.
176
+ * @param data {IBezierLayerProps} - The properties to validate.
177
+ * @returns {IBezierLayerProps} The validated properties.
178
+ */
179
+ validateProps(data) {
180
+ return {
181
+ ...super.validateProps(data),
182
+ centring: data.centring || types_1.Centring.None,
183
+ controlPoints: data.controlPoints || [{ x: 0, y: 0 }, { x: 0, y: 0 }],
184
+ endPoint: data.endPoint || { x: 0, y: 0 },
185
+ stroke: {
186
+ width: data.stroke?.width || 1,
187
+ cap: data.stroke?.cap || 'butt',
188
+ join: data.stroke?.join || 'miter',
189
+ dashOffset: data.stroke?.dashOffset || 0,
190
+ dash: data.stroke?.dash || [],
191
+ miterLimit: data.stroke?.miterLimit || 10,
192
+ }
193
+ };
194
+ }
176
195
  }
177
196
  exports.BezierLayer = BezierLayer;
@@ -143,4 +143,5 @@ export declare class ClearLayer implements IClearLayer {
143
143
  * @returns {IClearLayer} The JSON representation of the Clear Layer.
144
144
  */
145
145
  toJSON(): IClearLayer;
146
+ protected validateProps(props: IClearLayerProps): IClearLayerProps;
146
147
  }
@@ -39,14 +39,8 @@ class ClearLayer {
39
39
  this.type = types_1.LayerType.Clear;
40
40
  this.zIndex = misc?.zIndex || 1;
41
41
  this.visible = misc?.visible || true;
42
- this.props = props ? props : {
43
- x: 0,
44
- y: 0,
45
- size: {
46
- width: 0,
47
- height: 0
48
- }
49
- };
42
+ this.props = props ? props : {};
43
+ this.props = this.validateProps(this.props);
50
44
  }
51
45
  /**
52
46
  * Sets the position of the layer in the 2D plane.
@@ -148,5 +142,17 @@ class ClearLayer {
148
142
  props: copy,
149
143
  };
150
144
  }
145
+ validateProps(props) {
146
+ return {
147
+ x: props.x || 0,
148
+ y: props.y || 0,
149
+ size: {
150
+ width: props.size?.width || 0,
151
+ height: props.size?.height || 0
152
+ },
153
+ centring: props.centring || 'none',
154
+ globalComposite: props.globalComposite || 'source-over'
155
+ };
156
+ }
151
157
  }
152
158
  exports.ClearLayer = ClearLayer;
@@ -90,4 +90,10 @@ export declare class ImageLayer extends BaseLayer<IImageLayerProps> {
90
90
  * @returns {IImageLayer} The JSON representation of the Image Layer.
91
91
  */
92
92
  toJSON(): IImageLayer;
93
+ /**
94
+ * Validates the properties of the Image Layer.
95
+ * @param data {IImageLayerProps} - The properties to validate.
96
+ * @returns {IImageLayerProps} The validated properties.
97
+ */
98
+ protected validateProps(data: IImageLayerProps): IImageLayerProps;
93
99
  }
@@ -23,7 +23,7 @@ class ImageLayer extends BaseLayer_1.BaseLayer {
23
23
  constructor(props, misc) {
24
24
  super(types_1.LayerType.Image, props || {}, misc);
25
25
  this.props = props ? props : {};
26
- this.props.centring = types_1.Centring.Center;
26
+ this.props = this.validateProps(this.props);
27
27
  }
28
28
  /**
29
29
  * Sets the source of the image.
@@ -79,15 +79,15 @@ class ImageLayer extends BaseLayer_1.BaseLayer {
79
79
  if (debug)
80
80
  LazyUtil_1.LazyLog.log('none', `ImageLayer:`, { x, y, w, h, rad });
81
81
  ctx.save();
82
- (0, utils_1.transform)(ctx, this.props.transform, { width: w, height: h, x, y, type: this.type });
83
- (0, utils_1.drawShadow)(ctx, this.props.shadow);
84
- (0, utils_1.opacity)(ctx, this.props.opacity);
85
- (0, utils_1.filters)(ctx, this.props.filter);
86
82
  let image = await (0, canvas_1.loadImage)(this.props.src);
87
83
  image.width = w;
88
84
  image.height = h;
89
85
  if (!image)
90
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);
91
91
  if (Object.keys(rad).length > 0) {
92
92
  ctx.beginPath();
93
93
  ctx.moveTo(x + (w / 2), y);
@@ -118,5 +118,21 @@ class ImageLayer extends BaseLayer_1.BaseLayer {
118
118
  }
119
119
  return { ...data };
120
120
  }
121
+ /**
122
+ * Validates the properties of the Image Layer.
123
+ * @param data {IImageLayerProps} - The properties to validate.
124
+ * @returns {IImageLayerProps} The validated properties.
125
+ */
126
+ validateProps(data) {
127
+ return {
128
+ ...super.validateProps(data),
129
+ src: data.src || '',
130
+ size: {
131
+ width: data.size?.width || 0,
132
+ height: data.size?.height || 0,
133
+ radius: data.size?.radius || { all: 0 }
134
+ }
135
+ };
136
+ }
121
137
  }
122
138
  exports.ImageLayer = ImageLayer;
@@ -129,4 +129,10 @@ export declare class LineLayer extends BaseLayer<ILineLayerProps> {
129
129
  * @returns {ILineLayer} The JSON representation of the Line Layer.
130
130
  */
131
131
  toJSON(): ILineLayer;
132
+ /**
133
+ * Validates the properties of the Line Layer.
134
+ * @param data {ILineLayerProps} - The properties to validate.
135
+ * @returns {ILineLayerProps} The validated properties.
136
+ */
137
+ protected validateProps(data: ILineLayerProps): ILineLayerProps;
132
138
  }
@@ -21,9 +21,7 @@ class LineLayer extends BaseLayer_1.BaseLayer {
21
21
  constructor(props, misc) {
22
22
  super(types_1.LayerType.Line, props || {}, misc);
23
23
  this.props = props ? props : {};
24
- if (!this.props.fillStyle)
25
- this.props.fillStyle = '#000000';
26
- this.props.centring = types_1.Centring.None;
24
+ this.props = this.validateProps(this.props);
27
25
  }
28
26
  /**
29
27
  * Sets the end position of the line layer.
@@ -104,9 +102,9 @@ class LineLayer extends BaseLayer_1.BaseLayer {
104
102
  xe: { v: this.props.endPoint.x },
105
103
  ye: { v: this.props.endPoint.y, options: LazyUtil_1.defaultArg.vl(true) },
106
104
  });
107
- let width = xe - xs;
108
- let height = ye - ys;
109
- let fillStyle = await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle);
105
+ let width = Math.abs(xe - xs);
106
+ 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 });
110
108
  if (debug)
111
109
  LazyUtil_1.LazyLog.log('none', `LineLayer:`, { xs, ys, xe, ye, width, height });
112
110
  ctx.save();
@@ -142,5 +140,28 @@ class LineLayer extends BaseLayer_1.BaseLayer {
142
140
  }
143
141
  return { ...data, props: copy };
144
142
  }
143
+ /**
144
+ * Validates the properties of the Line Layer.
145
+ * @param data {ILineLayerProps} - The properties to validate.
146
+ * @returns {ILineLayerProps} The validated properties.
147
+ */
148
+ validateProps(data) {
149
+ return {
150
+ ...super.validateProps(data),
151
+ centring: data.centring || types_1.Centring.None,
152
+ endPoint: {
153
+ x: data.endPoint?.x || 0,
154
+ y: data.endPoint?.y || 0,
155
+ },
156
+ stroke: {
157
+ width: data.stroke?.width || 1,
158
+ cap: data.stroke?.cap || 'butt',
159
+ join: data.stroke?.join || 'miter',
160
+ dashOffset: data.stroke?.dashOffset || 0,
161
+ dash: data.stroke?.dash || [],
162
+ miterLimit: data.stroke?.miterLimit || 10,
163
+ },
164
+ };
165
+ }
145
166
  }
146
167
  exports.LineLayer = LineLayer;
@@ -110,12 +110,6 @@ export declare class MorphLayer extends BaseLayer<IMorphLayerProps> {
110
110
  * @returns {this} The current instance for chaining.
111
111
  */
112
112
  setStroke(width: number, cap?: CanvasLineCap, join?: CanvasLineJoin, dash?: number[], dashOffset?: number, miterLimit?: number): this;
113
- /**
114
- * Sets whether the Morph Layer should be filled or stroked.
115
- * @param filled {boolean} - If true, the layer will be filled; otherwise, it will be stroked.
116
- * @returns {this} The current instance for chaining.
117
- */
118
- setFilled(filled: boolean): this;
119
113
  /**
120
114
  * Draws the Morph Layer on the canvas.
121
115
  * @param ctx {SKRSContext2D} - The canvas rendering context.
@@ -129,4 +123,10 @@ export declare class MorphLayer extends BaseLayer<IMorphLayerProps> {
129
123
  * @returns {IMorphLayer} The JSON representation of the Morph Layer.
130
124
  */
131
125
  toJSON(): IMorphLayer;
126
+ /**
127
+ * Validates the properties of the Morph Layer.
128
+ * @param data {IMorphLayerProps} - The properties to validate.
129
+ * @returns {IMorphLayerProps} The validated properties.
130
+ */
131
+ protected validateProps(data: IMorphLayerProps): IMorphLayerProps;
132
132
  }
@@ -22,11 +22,7 @@ class MorphLayer extends BaseLayer_1.BaseLayer {
22
22
  constructor(props, misc) {
23
23
  super(types_1.LayerType.Morph, props || {}, misc);
24
24
  this.props = props ? props : {};
25
- if (!this.props.fillStyle)
26
- this.props.fillStyle = '#000000';
27
- if (!this.props.filled && this.props.filled !== false)
28
- this.props.filled = true;
29
- this.props.centring = types_1.Centring.Center;
25
+ this.props = this.validateProps(this.props);
30
26
  }
31
27
  /**
32
28
  * Sets the size of the Morph Layer.
@@ -76,15 +72,7 @@ class MorphLayer extends BaseLayer_1.BaseLayer {
76
72
  dashOffset: dashOffset || 0,
77
73
  miterLimit: miterLimit || 10,
78
74
  };
79
- return this;
80
- }
81
- /**
82
- * Sets whether the Morph Layer should be filled or stroked.
83
- * @param filled {boolean} - If true, the layer will be filled; otherwise, it will be stroked.
84
- * @returns {this} The current instance for chaining.
85
- */
86
- setFilled(filled) {
87
- this.props.filled = filled;
75
+ this.props.filled = false; // Ensure filled is false when stroke is set
88
76
  return this;
89
77
  }
90
78
  /**
@@ -110,7 +98,7 @@ class MorphLayer extends BaseLayer_1.BaseLayer {
110
98
  }
111
99
  }
112
100
  let { x, y } = (0, utils_1.centring)(this.props.centring, this.type, w, h, xs, ys);
113
- let fillStyle = await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle);
101
+ let fillStyle = await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle, { debug, layer: { width: w, height: h, x: xs, y: ys, align: this.props.centring }, manager });
114
102
  if (debug)
115
103
  LazyUtil_1.LazyLog.log('none', `MorphLayer:`, { x, y, w, h, rad });
116
104
  ctx.save();
@@ -160,5 +148,28 @@ class MorphLayer extends BaseLayer_1.BaseLayer {
160
148
  }
161
149
  return { ...data, props: copy };
162
150
  }
151
+ /**
152
+ * Validates the properties of the Morph Layer.
153
+ * @param data {IMorphLayerProps} - The properties to validate.
154
+ * @returns {IMorphLayerProps} The validated properties.
155
+ */
156
+ validateProps(data) {
157
+ return {
158
+ ...super.validateProps(data),
159
+ size: {
160
+ width: data.size?.width || 100,
161
+ height: data.size?.height || 100,
162
+ radius: data.size?.radius || { all: 0 },
163
+ },
164
+ stroke: {
165
+ width: data.stroke?.width || 1,
166
+ cap: data.stroke?.cap || 'butt',
167
+ join: data.stroke?.join || 'miter',
168
+ dashOffset: data.stroke?.dashOffset || 0,
169
+ dash: data.stroke?.dash || [],
170
+ miterLimit: data.stroke?.miterLimit || 10,
171
+ },
172
+ };
173
+ }
163
174
  }
164
175
  exports.MorphLayer = MorphLayer;
@@ -115,12 +115,6 @@ export declare class Path2DLayer extends BaseLayer<IPath2DLayerProps> {
115
115
  * @returns {this} The current instance for chaining.
116
116
  */
117
117
  setStroke(width: number, cap?: CanvasLineCap, join?: CanvasLineJoin, dash?: number[], dashOffset?: number, miterLimit?: number): this;
118
- /**
119
- * Sets whether the Path2D Layer should be filled or stroked.
120
- * @param filled {boolean} - If true, the layer will be filled; otherwise, it will be stroked.
121
- * @returns {this} The current instance for chaining.
122
- */
123
- setFilled(filled: boolean): this;
124
118
  /**
125
119
  * Sets the color of the Path2D Layer.
126
120
  * @param color {string} - The color of the Path2D Layer.
@@ -161,4 +155,10 @@ export declare class Path2DLayer extends BaseLayer<IPath2DLayerProps> {
161
155
  * @returns {IPath2DLayer} The JSON representation of the Path2D Layer.
162
156
  */
163
157
  toJSON(): IPath2DLayer;
158
+ /**
159
+ * Validates the properties of the Path2D Layer.
160
+ * @param data {IPath2DLayerProps} - The properties to validate.
161
+ * @returns {IPath2DLayerProps} The validated properties.
162
+ */
163
+ protected validateProps(data: IPath2DLayerProps): IPath2DLayerProps;
164
164
  }
@@ -18,6 +18,7 @@ class Path2DLayer extends BaseLayer_1.BaseLayer {
18
18
  this.zIndex = misc?.zIndex || 1;
19
19
  this.visible = misc?.visible || true;
20
20
  this.props = props ? props : {};
21
+ this.props = this.validateProps(this.props);
21
22
  }
22
23
  /**
23
24
  * Sets the unique identifier of the layer.
@@ -122,15 +123,7 @@ class Path2DLayer extends BaseLayer_1.BaseLayer {
122
123
  dashOffset: dashOffset || 0,
123
124
  miterLimit: miterLimit || 10,
124
125
  };
125
- return this;
126
- }
127
- /**
128
- * Sets whether the Path2D Layer should be filled or stroked.
129
- * @param filled {boolean} - If true, the layer will be filled; otherwise, it will be stroked.
130
- * @returns {this} The current instance for chaining.
131
- */
132
- setFilled(filled) {
133
- this.props.filled = filled;
126
+ this.props.filled = false; // Ensure filled is false when stroke is set
134
127
  return this;
135
128
  }
136
129
  /**
@@ -252,7 +245,7 @@ class Path2DLayer extends BaseLayer_1.BaseLayer {
252
245
  async draw(ctx, canvas, manager, debug) {
253
246
  ctx.beginPath();
254
247
  ctx.save();
255
- let fillStyle = await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle);
248
+ let fillStyle = await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle, { debug, manager });
256
249
  (0, utils_1.transform)(ctx, this.props.transform, { width: 0, height: 0, x: 0, y: 0, type: this.type });
257
250
  (0, utils_1.opacity)(ctx, this.props.opacity);
258
251
  ctx.globalCompositeOperation = this.props.globalComposite;
@@ -289,5 +282,26 @@ class Path2DLayer extends BaseLayer_1.BaseLayer {
289
282
  props: this.props
290
283
  };
291
284
  }
285
+ /**
286
+ * Validates the properties of the Path2D Layer.
287
+ * @param data {IPath2DLayerProps} - The properties to validate.
288
+ * @returns {IPath2DLayerProps} The validated properties.
289
+ */
290
+ validateProps(data) {
291
+ return {
292
+ ...super.validateProps(data),
293
+ path2D: data.path2D || new canvas_1.Path2D(),
294
+ stroke: {
295
+ width: data.stroke?.width || 1,
296
+ cap: data.stroke?.cap || 'butt',
297
+ join: data.stroke?.join || 'miter',
298
+ dashOffset: data.stroke?.dashOffset || 0,
299
+ dash: data.stroke?.dash || [],
300
+ miterLimit: data.stroke?.miterLimit || 10
301
+ },
302
+ loadFromSVG: data.loadFromSVG || false,
303
+ clipPath: data.clipPath || false
304
+ };
305
+ }
292
306
  }
293
307
  exports.Path2DLayer = Path2DLayer;
@@ -139,4 +139,10 @@ export declare class QuadraticLayer extends BaseLayer<IQuadraticLayerProps> {
139
139
  * @returns {IQuadraticLayer} The JSON representation of the Quadratic Layer.
140
140
  */
141
141
  toJSON(): IQuadraticLayer;
142
+ /**
143
+ * Validates the properties of the Quadratic Layer.
144
+ * @param data {IQuadraticLayerProps} - The properties to validate.
145
+ * @returns {IQuadraticLayerProps} The validated properties.
146
+ */
147
+ protected validateProps(data: IQuadraticLayerProps): IQuadraticLayerProps;
142
148
  }
@@ -21,9 +21,7 @@ class QuadraticLayer extends BaseLayer_1.BaseLayer {
21
21
  constructor(props, misc) {
22
22
  super(types_1.LayerType.QuadraticCurve, props || {}, misc);
23
23
  this.props = props ? props : {};
24
- if (!this.props.fillStyle)
25
- this.props.fillStyle = '#000000';
26
- this.props.centring = types_1.Centring.None;
24
+ this.props = this.validateProps(this.props);
27
25
  }
28
26
  /**
29
27
  * Sets the control point of the quadratic layer.
@@ -118,7 +116,7 @@ class QuadraticLayer extends BaseLayer_1.BaseLayer {
118
116
  ye: { v: this.props.endPoint.y, options: LazyUtil_1.defaultArg.vl(true) }
119
117
  });
120
118
  const { max, min, center, width, height } = (0, utils_1.getBoundingBoxBezier)([{ x: xs, y: ys }, { x: cx, y: cy }, { x: xe, y: ye }]);
121
- let fillStyle = await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle);
119
+ let fillStyle = await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle, { debug, layer: { width, height, x: min.x, y: min.y, align: 'none' }, manager });
122
120
  if (debug)
123
121
  LazyUtil_1.LazyLog.log('none', `BezierLayer:`, { xs, ys, cx, cy, xe, ye, max, min, center, width, height, fillStyle });
124
122
  ctx.save();
@@ -154,5 +152,26 @@ class QuadraticLayer extends BaseLayer_1.BaseLayer {
154
152
  }
155
153
  return { ...data, props: copy };
156
154
  }
155
+ /**
156
+ * Validates the properties of the Quadratic Layer.
157
+ * @param data {IQuadraticLayerProps} - The properties to validate.
158
+ * @returns {IQuadraticLayerProps} The validated properties.
159
+ */
160
+ validateProps(data) {
161
+ return {
162
+ ...super.validateProps(data),
163
+ centring: data.centring || types_1.Centring.None,
164
+ controlPoints: data.controlPoints || [{ x: 0, y: 0 }],
165
+ endPoint: data.endPoint || { x: 0, y: 0 },
166
+ stroke: {
167
+ width: data.stroke?.width || 1,
168
+ cap: data.stroke?.cap || 'butt',
169
+ join: data.stroke?.join || 'miter',
170
+ dashOffset: data.stroke?.dashOffset || 0,
171
+ dash: data.stroke?.dash || [],
172
+ miterLimit: data.stroke?.miterLimit || 10,
173
+ }
174
+ };
175
+ }
157
176
  }
158
177
  exports.QuadraticLayer = QuadraticLayer;