@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,75 +1,140 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LazyCanvas = void 0;
4
- const enum_1 = require("../types/enum");
4
+ const types_1 = require("../types");
5
5
  const canvas_1 = require("@napi-rs/canvas");
6
6
  const LayersManager_1 = require("./managers/LayersManager");
7
7
  const RenderManager_1 = require("./managers/RenderManager");
8
8
  const FontsManager_1 = require("./managers/FontsManager");
9
9
  const AnimationManager_1 = require("./managers/AnimationManager");
10
+ const LazyUtil_1 = require("../utils/LazyUtil");
11
+ const utils_1 = require("../utils/utils");
12
+ /**
13
+ * Class representing a LazyCanvas, which provides a structured way to manage canvas rendering.
14
+ */
10
15
  class LazyCanvas {
11
- width;
12
- height;
16
+ /**
17
+ * The canvas instance, which can be either a Canvas or SvgCanvas.
18
+ */
13
19
  canvas;
20
+ /**
21
+ * The 2D rendering context of the canvas.
22
+ */
14
23
  ctx;
15
- layers;
16
- render;
17
- fonts;
18
- animation;
19
- exportType;
20
- constructor(debug = false) {
21
- this.width = 0;
22
- this.height = 0;
24
+ /**
25
+ * The manager object containing various managers for layers, rendering, fonts, and animation.
26
+ */
27
+ manager;
28
+ /**
29
+ * The options for configuring the LazyCanvas instance.
30
+ */
31
+ options;
32
+ /**
33
+ * Constructs a new LazyCanvas instance.
34
+ * @param opts {Object} - Optional settings for the LazyCanvas instance.
35
+ * @param opts.debug {boolean} - Whether debugging is enabled.
36
+ * @param opts.settings {IOLazyCanvas} - The input settings for the LazyCanvas instance.
37
+ */
38
+ constructor(opts) {
23
39
  this.canvas = new canvas_1.Canvas(0, 0);
24
40
  this.ctx = this.canvas.getContext('2d');
25
- this.layers = new LayersManager_1.LayersManager(debug);
26
- this.render = new RenderManager_1.RenderManager(this, debug);
27
- this.fonts = new FontsManager_1.FontsManager(debug);
28
- this.animation = new AnimationManager_1.AnimationManager(debug);
29
- this.exportType = enum_1.Export.Buffer;
41
+ this.manager = {
42
+ layers: new LayersManager_1.LayersManager({ debug: opts?.debug }),
43
+ render: new RenderManager_1.RenderManager(this, { debug: opts?.debug }),
44
+ fonts: new FontsManager_1.FontsManager({ debug: opts?.debug }),
45
+ animation: new AnimationManager_1.AnimationManager({ debug: opts?.debug, settings: { options: opts?.settings?.animation } })
46
+ };
47
+ this.options = {
48
+ width: opts?.settings?.options.width || 0,
49
+ height: opts?.settings?.options.height || 0,
50
+ animated: opts?.settings?.options.animated || false,
51
+ exportType: opts?.settings?.options.exportType || types_1.Export.BUFFER,
52
+ flag: opts?.settings?.options.flag || canvas_1.SvgExportFlag.RelativePathEncoding
53
+ };
54
+ if (opts?.debug)
55
+ LazyUtil_1.LazyLog.log('info', 'LazyCanvas initialized with settings:', opts.settings);
30
56
  }
31
57
  /**
32
- * Set the export type
33
- * @param type {AnyExport} - The `export` type
58
+ * Sets the export type for the canvas.
59
+ * @param type {AnyExport} - The export type (e.g., buffer, SVG, etc.).
60
+ * @returns {this} The current instance for chaining.
34
61
  */
35
62
  setExportType(type) {
36
- this.exportType = type;
63
+ this.options.exportType = type;
37
64
  switch (type) {
38
- case enum_1.Export.Buffer:
39
- this.canvas = new canvas_1.Canvas(this.width, this.height);
65
+ case types_1.Export.BUFFER:
66
+ this.canvas = new canvas_1.Canvas(this.options.width, this.options.height);
40
67
  this.ctx = this.canvas.getContext('2d');
41
68
  break;
42
- case enum_1.Export.CTX:
69
+ case types_1.Export.CTX:
43
70
  break;
44
- case enum_1.Export.SVG:
45
- this.canvas = new canvas_1.Canvas(this.width, this.height, canvas_1.SvgExportFlag.RelativePathEncoding);
71
+ case types_1.Export.SVG:
72
+ this.canvas = new canvas_1.Canvas(this.options.width, this.options.height, this.options.flag || canvas_1.SvgExportFlag.RelativePathEncoding);
46
73
  this.ctx = this.canvas.getContext('2d');
47
74
  break;
48
75
  }
49
76
  return this;
50
77
  }
51
78
  /**
52
- * Set the SVG export flag. This method should be called after `setExportType` method.
53
- * @param flag {SvgExportFlag} - The `flag` of the SVG export
79
+ * Sets the SVG export flag. This method should be called after `setExportType`.
80
+ * @param flag {SvgExportFlag} - The SVG export flag.
81
+ * @returns {this} The current instance for chaining.
54
82
  */
55
83
  setSvgExportFlag(flag) {
56
- if (this.exportType === enum_1.Export.SVG) {
57
- this.canvas = new canvas_1.Canvas(this.width, this.height, flag);
84
+ if (this.options.exportType === types_1.Export.SVG) {
85
+ this.canvas = new canvas_1.Canvas(this.options.width, this.options.height, flag);
58
86
  this.ctx = this.canvas.getContext('2d');
87
+ this.options.flag = flag;
88
+ }
89
+ return this;
90
+ }
91
+ /**
92
+ * Enables animation for the canvas.
93
+ * @returns {this} The current instance for chaining.
94
+ */
95
+ animated() {
96
+ this.options.animated = true;
97
+ return this;
98
+ }
99
+ /**
100
+ * Resizes the canvas to the specified dimensions.
101
+ * @param ratio {number} - The ratio to resize the canvas.
102
+ * @returns {this} The current instance for chaining.
103
+ */
104
+ resize(ratio) {
105
+ if (this.options.width <= 0 || this.options.height <= 0) {
106
+ throw new Error('Canvas dimensions are not set.');
107
+ }
108
+ this.options.width = (0, utils_1.resize)(this.options.width, ratio);
109
+ this.options.height = (0, utils_1.resize)(this.options.height, ratio);
110
+ if (this.options.exportType === types_1.Export.SVG) {
111
+ this.canvas = new canvas_1.Canvas(this.options.width, this.options.height, this.options.flag || canvas_1.SvgExportFlag.RelativePathEncoding);
112
+ }
113
+ else {
114
+ this.canvas = new canvas_1.Canvas(this.options.width, this.options.height);
59
115
  }
116
+ this.ctx = this.canvas.getContext('2d');
117
+ const layers = (0, utils_1.resizeLayers)(this.manager.layers.toArray(), ratio);
118
+ this.manager.layers.fromArray(layers);
60
119
  return this;
61
120
  }
62
121
  /**
63
- * Create a new canvas. This method should be called before any other methods.
64
- * @param width {number} - The `width` of the canvas
65
- * @param height {number} - The `height` of the canvas
122
+ * Creates a new canvas with the specified dimensions.
123
+ * @param width {number} - The width of the canvas.
124
+ * @param height {number} - The height of the canvas.
125
+ * @returns {this} The current instance for chaining.
66
126
  */
67
127
  create(width, height) {
68
- this.width = width;
69
- this.height = height;
70
- this.canvas = new canvas_1.Canvas(width, height);
128
+ this.options.width = width;
129
+ this.options.height = height;
130
+ if (this.options.exportType === types_1.Export.SVG) {
131
+ this.canvas = new canvas_1.Canvas(width, height, this.options.flag || canvas_1.SvgExportFlag.RelativePathEncoding);
132
+ }
133
+ else {
134
+ this.canvas = new canvas_1.Canvas(width, height);
135
+ }
71
136
  this.ctx = this.canvas.getContext('2d');
72
- this.layers = new LayersManager_1.LayersManager();
137
+ this.manager.layers = new LayersManager_1.LayersManager();
73
138
  return this;
74
139
  }
75
140
  }
@@ -1,86 +1,236 @@
1
- import { ScaleType, IBaseLayer, IBaseLayerProps, AnyCentring, AnyGlobalCompositeOperation } from "../../types";
2
- import { LayerType } from "../../types/enum";
1
+ import { ScaleType, AnyCentring, AnyGlobalCompositeOperation, ColorType, Transform, AnyFilter, LayerType } from "../../types";
2
+ /**
3
+ * Interface representing the base structure of a layer.
4
+ */
5
+ export interface IBaseLayer {
6
+ /**
7
+ * The unique identifier of the layer.
8
+ */
9
+ id: string;
10
+ /**
11
+ * The type of the layer.
12
+ */
13
+ type: LayerType;
14
+ /**
15
+ * The z-index of the layer, determining its stacking order.
16
+ */
17
+ zIndex: number;
18
+ /**
19
+ * Whether the layer is visible.
20
+ */
21
+ visible: boolean;
22
+ /**
23
+ * The properties of the layer.
24
+ */
25
+ props: IBaseLayerProps;
26
+ }
27
+ /**
28
+ * Interface representing the properties of a base layer.
29
+ */
30
+ export interface IBaseLayerProps {
31
+ /**
32
+ * The x-coordinate of the layer.
33
+ */
34
+ x: ScaleType;
35
+ /**
36
+ * The y-coordinate of the layer.
37
+ */
38
+ y: ScaleType;
39
+ /**
40
+ * The centring type of the layer.
41
+ */
42
+ centring: AnyCentring;
43
+ /**
44
+ * The filter effects applied to the layer.
45
+ */
46
+ filter: string;
47
+ /**
48
+ * The opacity of the layer, ranging from 0 to 1.
49
+ */
50
+ opacity: number;
51
+ /**
52
+ * Whether the layer is filled.
53
+ */
54
+ filled: boolean;
55
+ /**
56
+ * The fill style (color or pattern) of the layer.
57
+ */
58
+ fillStyle: ColorType;
59
+ /**
60
+ * The shadow properties of the layer.
61
+ */
62
+ shadow: {
63
+ /**
64
+ * The color of the shadow.
65
+ */
66
+ color: string;
67
+ /**
68
+ * The blur radius of the shadow.
69
+ */
70
+ blur: number;
71
+ /**
72
+ * The horizontal offset of the shadow.
73
+ */
74
+ offsetX: number;
75
+ /**
76
+ * The vertical offset of the shadow.
77
+ */
78
+ offsetY: number;
79
+ };
80
+ /**
81
+ * The transformation properties of the layer.
82
+ */
83
+ transform: Transform;
84
+ /**
85
+ * The global composite operation applied to the layer.
86
+ */
87
+ globalComposite: AnyGlobalCompositeOperation;
88
+ }
89
+ /**
90
+ * Interface representing miscellaneous options for a base layer.
91
+ */
92
+ export interface IBaseLayerMisc {
93
+ /**
94
+ * The unique identifier of the layer (optional).
95
+ */
96
+ id?: string;
97
+ /**
98
+ * The z-index of the layer (optional).
99
+ */
100
+ zIndex?: number;
101
+ /**
102
+ * Whether the layer is visible (optional).
103
+ */
104
+ visible?: boolean;
105
+ }
106
+ /**
107
+ * Represents a base layer with generic properties and methods for managing
108
+ * its position, visibility, transformations, and other attributes.
109
+ *
110
+ * @template T - A type extending `IBaseLayerProps` that defines the properties of the layer.
111
+ */
3
112
  export declare class BaseLayer<T extends IBaseLayerProps> {
113
+ /**
114
+ * The unique identifier of the layer.
115
+ * @type {string}
116
+ */
4
117
  id: string;
118
+ /**
119
+ * The type of the layer.
120
+ * @type {LayerType}
121
+ */
5
122
  type: LayerType;
123
+ /**
124
+ * The z-index of the layer, determining its stacking order.
125
+ * @type {number}
126
+ */
6
127
  zIndex: number;
128
+ /**
129
+ * The visibility of the layer.
130
+ * @type {boolean}
131
+ */
7
132
  visible: boolean;
133
+ /**
134
+ * The properties of the layer, defined by the generic type `T`.
135
+ * @type {T}
136
+ */
8
137
  props: T;
9
- constructor(type?: LayerType, props?: T);
10
138
  /**
11
- * @description Position of the layer in the 2D plane. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
12
- * @param x {ScaleType} - The `x` position of the layer
13
- * @param y {ScaleType} - The `y` position of the layer
139
+ * Constructs a new `BaseLayer` instance.
140
+ * @param {LayerType} [type] - The type of the layer.
141
+ * @param {T} [props] - The properties of the layer.
142
+ * @param {IBaseLayerMisc} [misc] - Miscellaneous options for the layer.
143
+ */
144
+ constructor(type?: LayerType, props?: T, misc?: IBaseLayerMisc);
145
+ /**
146
+ * Sets the position of the layer in the 2D plane.
147
+ * @param {ScaleType} x - The x-coordinate of the layer.
148
+ * @param {ScaleType} y - The y-coordinate of the layer.
149
+ * @returns {this} The current instance for chaining.
14
150
  */
15
151
  setPosition(x: ScaleType, y: ScaleType): this;
16
152
  /**
17
- * @description Opacity of the layer. The value must be between `0` and `1`.
18
- * @param opacity {number} - The `opacity` of the layer
153
+ * Sets the opacity of the layer.
154
+ * @param {number} opacity - The opacity value, between 0 and 1.
155
+ * @returns {this} The current instance for chaining.
19
156
  */
20
157
  setOpacity(opacity: number): this;
21
158
  /**
22
- * @description Sets the `id` of the layer.
23
- * @param id {string} - The `id` of the layer
159
+ * Sets the unique identifier of the layer.
160
+ *
161
+ * @param {string} id - The unique identifier.
162
+ * @returns {this} The current instance for chaining.
24
163
  */
25
164
  setID(id: string): this;
26
165
  /**
27
- * @description Sets shadow of layer.
28
- * @param color {string} - The `color` of the filter
29
- * @param blur {number} - The `blur` of the filter
30
- * @param offsetX {number} - The `offsetX` of the filter
31
- * @param offsetY {number} - The `offsetY` of the filter
166
+ * Sets the shadow properties of the layer.
167
+ * @param {string} color - The color of the shadow.
168
+ * @param {number} [blur] - The blur radius of the shadow.
169
+ * @param {number} [offsetX] - The horizontal offset of the shadow.
170
+ * @param {number} [offsetY] - The vertical offset of the shadow.
171
+ * @returns {this} The current instance for chaining.
172
+ * @throws {LazyError} If the color is invalid or not provided.
32
173
  */
33
174
  setShadow(color: string, blur?: number, offsetX?: number, offsetY?: number): this;
34
175
  /**
35
- * @description Matrix of the layer in the 2D plane.
36
- * @param matrix {DOMMatrix2DInit} - The `matrix` of the layer
176
+ * Sets the transformation matrix of the layer.
177
+ * @param {DOMMatrix2DInit} matrix - The transformation matrix.
178
+ * @returns {this} The current instance for chaining.
37
179
  */
38
180
  setMatrix(matrix: DOMMatrix2DInit): this;
39
181
  /**
40
- * @description Scale of the layer in the x and y direction by the given amount from their current position.
41
- * @param x {number} - The `x` scale of the layer
42
- * @param y {number} - The `y` scale of the layer
182
+ * Sets the scale of the layer in the x and y directions.
183
+ * @param {number} x - The scale factor in the x direction.
184
+ * @param {number} y - The scale factor in the y direction.
185
+ * @returns {this} The current instance for chaining.
43
186
  */
44
187
  setScale(x: number, y: number): this;
45
188
  /**
46
- * @description Rotate of the layer in the clockwise direction by the given amount from its current position.
47
- * @param rotate {number} - The `rotate` of the layer
189
+ * Sets the rotation of the layer.
190
+ * @param {number} rotate - The rotation angle in degrees.
191
+ * @returns {this} The current instance for chaining.
48
192
  */
49
193
  setRotate(rotate: number): this;
50
194
  /**
51
- * @description Translate of the layer in the x and y direction by the given amount from their current position.
52
- * @param x {number} - The `x` translation of the layer
53
- * @param y {number} - The `y` translation of the layer
195
+ * Sets the translation of the layer in the x and y directions.
196
+ * @param {number} x - The translation in the x direction.
197
+ * @param {number} y - The translation in the y direction.
198
+ * @returns {this} The current instance for chaining.
54
199
  */
55
200
  setTranslate(x: number, y: number): this;
56
201
  /**
57
- * @description The **`CanvasRenderingContext2D`**. filter property of the Canvas 2D API provides filter effects such as blurring and grayscaling.
58
- * It is similar to the CSS [`filter`](https://developer.mozilla.org/en-US/docs/Web/CSS/filter) property and accepts the same values.
59
- * @param filter {string} - The `filter` of the layer. Multiple filters are supported.
202
+ * Sets the filter effects for the layer.
203
+ * @param {...AnyFilter} filter - The filter effects to apply.
204
+ * @returns {this} The current instance for chaining.
60
205
  */
61
- setFilters(...filter: string[]): this;
206
+ setFilters(...filter: AnyFilter[]): this;
62
207
  /**
63
- * @description Sets type of centring of the layer.
64
- * @param centring {AnyCentring} - The `centring` of the layer
208
+ * Sets the centring type of the layer. **Don't affect on Bezier, Line, Quadratic and Text layers**.
209
+ * @param {AnyCentring} centring - The centring type.
210
+ * @returns {this} The current instance for chaining.
65
211
  */
66
212
  setCentring(centring: AnyCentring): this;
67
213
  /**
68
- * @description Sets the visibility of the layer.
69
- * @param visible {boolean} - The `visibility` of the layer
214
+ * Sets the visibility of the layer.
215
+ * @param {boolean} visible - The visibility state.
216
+ * @returns {this} The current instance for chaining.
70
217
  */
71
218
  setVisible(visible: boolean): this;
72
219
  /**
73
- * @description Sets zIndex of the layer.
74
- * @param zIndex {number} - The `zIndex` of the layer
220
+ * Sets the z-index of the layer.
221
+ * @param {number} zIndex - The z-index value.
222
+ * @returns {this} The current instance for chaining.
75
223
  */
76
224
  setZIndex(zIndex: number): this;
77
225
  /**
78
- * @description Sets global composite operation of the layer.
79
- * @param operation {AnyGlobalCompositeOperation} - The `operation` of the layer
226
+ * Sets the global composite operation for the layer.
227
+ * @param {AnyGlobalCompositeOperation} operation - The composite operation.
228
+ * @returns {this} The current instance for chaining.
80
229
  */
81
230
  setGlobalCompositeOperation(operation: AnyGlobalCompositeOperation): this;
82
231
  /**
83
- * @returns {IBaseLayer}
232
+ * Converts the layer to a JSON representation.
233
+ * @returns {IBaseLayer} The JSON representation of the layer.
84
234
  */
85
235
  toJSON(): IBaseLayer;
86
236
  }