@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,81 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  Object.defineProperty(exports, "__esModule", { value: true });
26
3
  exports.Pattern = void 0;
27
- const enum_1 = require("../../types/enum");
4
+ const types_1 = require("../../types");
28
5
  const LazyCanvas_1 = require("../LazyCanvas");
29
6
  const canvas_1 = require("@napi-rs/canvas");
30
- const jimp = __importStar(require("jimp"));
7
+ const Exporter_1 = require("./Exporter");
8
+ const LazyUtil_1 = require("../../utils/LazyUtil");
9
+ /**
10
+ * Class representing a pattern with properties and methods to manipulate it.
11
+ */
31
12
  class Pattern {
13
+ /**
14
+ * The type of fill, which is always `Pattern`.
15
+ */
16
+ fillType = types_1.FillType.Pattern;
17
+ /**
18
+ * The type of the pattern (e.g., repeat, no-repeat, etc.).
19
+ */
32
20
  type;
21
+ /**
22
+ * The source of the pattern, which can be a string (URL or path) or a LazyCanvas instance.
23
+ */
33
24
  src;
34
- constructor() {
35
- this.type = enum_1.PatternType.Repeat;
36
- this.src = '';
25
+ /**
26
+ * Constructs a new Pattern instance.
27
+ * @param opts {Object} - Optional properties for the pattern.
28
+ * @param opts.props {IPattern} - The pattern properties.
29
+ */
30
+ constructor(opts) {
31
+ this.type = opts?.props?.type || types_1.PatternType.Repeat;
32
+ this.src = opts?.props?.src || '';
37
33
  }
38
34
  /**
39
- * Set the type of the pattern
40
- * @param type {AnyPatternType} - The `type` of the pattern
35
+ * Sets the type of the pattern.
36
+ * @param type {AnyPatternType} - The type of the pattern (e.g., repeat, no-repeat).
37
+ * @returns {this} The current instance for chaining.
41
38
  */
42
39
  setType(type) {
43
40
  this.type = type;
44
41
  return this;
45
42
  }
46
43
  /**
47
- * Set the source of the pattern
48
- * @param src {string | LazyCanvas} - The `src` of the pattern
44
+ * Sets the source of the pattern.
45
+ * @param src {string | LazyCanvas} - The source of the pattern, which can be a string (URL or path) or a LazyCanvas instance.
46
+ * @returns {this} The current instance for chaining.
49
47
  */
50
48
  setSrc(src) {
51
49
  this.src = src;
52
50
  return this;
53
51
  }
52
+ /**
53
+ * Draws the pattern on a canvas context.
54
+ * @param ctx {SKRSContext2D} - The canvas rendering context.
55
+ * @returns {Promise<CanvasPattern>} The created pattern.
56
+ */
54
57
  async draw(ctx) {
58
+ if (!this.src)
59
+ throw new LazyUtil_1.LazyError('Pattern source is not set');
55
60
  if (this.src instanceof LazyCanvas_1.LazyCanvas) {
56
- let jmp = await this.src.render.render();
57
- let image = await (0, canvas_1.loadImage)(jmp);
58
- return ctx.createPattern(image, this.type);
59
- }
60
- else {
61
- let jmp = await jimp.read(this.src);
62
- let image = await (0, canvas_1.loadImage)(await jmp.getBufferAsync('image/png'));
63
- return ctx.createPattern(image, this.type);
61
+ return ctx.createPattern((await this.src.manager.render.render('canvas')), this.type);
64
62
  }
63
+ return ctx.createPattern(await (0, canvas_1.loadImage)(this.src), this.type);
65
64
  }
66
65
  /**
67
- * @returns {IPattern}
66
+ * Converts the Pattern instance to a JSON representation.
67
+ * @returns {IPattern} The JSON representation of the pattern.
68
68
  */
69
69
  toJSON() {
70
+ let src = this.src;
71
+ if (this.src instanceof LazyCanvas_1.LazyCanvas) {
72
+ // @ts-ignore
73
+ src = new Exporter_1.Exporter(this.src).syncExport('json');
74
+ }
70
75
  return {
76
+ fillType: this.fillType,
71
77
  type: this.type,
72
- src: this.src
78
+ src: src
73
79
  };
74
80
  }
75
81
  }
@@ -0,0 +1,6 @@
1
+ export * from "./Exporter";
2
+ export * from "./Font";
3
+ export * from "./Gradient";
4
+ export * from "./Link";
5
+ export * from "./Pattern";
6
+ export * from "./readers/JSONReader";
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./Exporter"), exports);
18
+ __exportStar(require("./Font"), exports);
19
+ __exportStar(require("./Gradient"), exports);
20
+ __exportStar(require("./Link"), exports);
21
+ __exportStar(require("./Pattern"), exports);
22
+ __exportStar(require("./readers/JSONReader"), exports);
@@ -0,0 +1,49 @@
1
+ import { IOLazyCanvas, LazyCanvas } from "../../LazyCanvas";
2
+ /**
3
+ * Class responsible for reading and parsing JSON data into a LazyCanvas instance.
4
+ */
5
+ export declare class JSONReader {
6
+ /**
7
+ * Reads JSON data and converts it into a LazyCanvas instance.
8
+ * @param data {IOLazyCanvas} - The JSON data to read.
9
+ * @param opts {Object} - Optional settings.
10
+ * @param opts.debug {boolean} - Whether to enable debug logging.
11
+ * @returns {LazyCanvas} The created LazyCanvas instance.
12
+ * @throws {LazyError} If the data contains invalid options or no layers are found.
13
+ */
14
+ static read(data: IOLazyCanvas, opts?: {
15
+ debug?: boolean;
16
+ }): LazyCanvas;
17
+ /**
18
+ * Reads a JSON file and converts it into a LazyCanvas instance.
19
+ * @param file {string} - The path to the JSON file.
20
+ * @param opts {Object} - Optional settings.
21
+ * @param opts.debug {boolean} - Whether to enable debug logging.
22
+ * @returns {LazyCanvas} The created LazyCanvas instance.
23
+ * @throws {LazyError} If the file does not exist.
24
+ */
25
+ static readFile(file: string, opts?: {
26
+ debug?: boolean;
27
+ }): LazyCanvas;
28
+ /**
29
+ * Parses an array of JSON layers into an array of AnyLayer or Group instances.
30
+ * @param data {Array<JSONLayer | Group>} - The array of JSON layers to parse.
31
+ * @param opts {Object} - Optional settings.
32
+ * @param opts.debug {boolean} - Whether to enable debug logging.
33
+ * @returns {Array<AnyLayer | Group>} The parsed layers.
34
+ */
35
+ private static layersParse;
36
+ /**
37
+ * Parses a single JSON layer into an AnyLayer or Group instance.
38
+ * @param layer {JSONLayer | IGroup | Group} - The JSON layer to parse.
39
+ * @param misc {IBaseLayerMisc} - Miscellaneous options for the layer.
40
+ * @returns {AnyLayer | Group} The parsed layer.
41
+ */
42
+ private static layerParse;
43
+ /**
44
+ * Parses the fill style of a layer.
45
+ * @param layer {JSONLayer} - The layer whose fill style is to be parsed.
46
+ * @returns {string | Gradient | Pattern} The parsed fill style.
47
+ */
48
+ private static fillParse;
49
+ }
@@ -0,0 +1,172 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.JSONReader = void 0;
27
+ const types_1 = require("../../../types");
28
+ const components_1 = require("../../components");
29
+ const __1 = require("../");
30
+ const LazyCanvas_1 = require("../../LazyCanvas");
31
+ const fs = __importStar(require("fs"));
32
+ const LazyUtil_1 = require("../../../utils/LazyUtil");
33
+ const path = __importStar(require("path"));
34
+ /**
35
+ * Class responsible for reading and parsing JSON data into a LazyCanvas instance.
36
+ */
37
+ class JSONReader {
38
+ /**
39
+ * Reads JSON data and converts it into a LazyCanvas instance.
40
+ * @param data {IOLazyCanvas} - The JSON data to read.
41
+ * @param opts {Object} - Optional settings.
42
+ * @param opts.debug {boolean} - Whether to enable debug logging.
43
+ * @returns {LazyCanvas} The created LazyCanvas instance.
44
+ * @throws {LazyError} If the data contains invalid options or no layers are found.
45
+ */
46
+ static read(data, opts) {
47
+ if (data.options.width <= 0 || data.options.height <= 0) {
48
+ throw new LazyUtil_1.LazyError("Invalid width or height");
49
+ }
50
+ if (data.options.exportType === undefined) {
51
+ throw new LazyUtil_1.LazyError("Invalid export type");
52
+ }
53
+ if (data.options.flag === undefined) {
54
+ throw new LazyUtil_1.LazyError("Invalid export flag");
55
+ }
56
+ if (data.layers === undefined || data.layers.length === 0) {
57
+ throw new LazyUtil_1.LazyError("No layers found");
58
+ }
59
+ if (opts?.debug)
60
+ LazyUtil_1.LazyLog.log("info", "Reading JSON...\nOptions:", data.options, "\nAnimation:", data.animation, "\nLayers Number:", data.layers.length, "\nLayers:", data.layers);
61
+ const layers = JSONReader.layersParse(data.layers);
62
+ const canvas = new LazyCanvas_1.LazyCanvas({ settings: data, debug: opts?.debug })
63
+ .create(data.options.width, data.options.height);
64
+ canvas.manager.layers.add(...layers);
65
+ return canvas;
66
+ }
67
+ /**
68
+ * Reads a JSON file and converts it into a LazyCanvas instance.
69
+ * @param file {string} - The path to the JSON file.
70
+ * @param opts {Object} - Optional settings.
71
+ * @param opts.debug {boolean} - Whether to enable debug logging.
72
+ * @returns {LazyCanvas} The created LazyCanvas instance.
73
+ * @throws {LazyError} If the file does not exist.
74
+ */
75
+ static readFile(file, opts) {
76
+ const filePath = path.resolve(file);
77
+ if (!fs.existsSync(filePath))
78
+ throw new LazyUtil_1.LazyError("File not found");
79
+ const json = fs.readFileSync(filePath, "utf-8");
80
+ const data = JSON.parse(json);
81
+ if (opts?.debug)
82
+ LazyUtil_1.LazyLog.log("info", "Reading JSON file...\nFile:", filePath, "\nData:", data);
83
+ return JSONReader.read(data, opts);
84
+ }
85
+ /**
86
+ * Parses an array of JSON layers into an array of AnyLayer or Group instances.
87
+ * @param data {Array<JSONLayer | Group>} - The array of JSON layers to parse.
88
+ * @param opts {Object} - Optional settings.
89
+ * @param opts.debug {boolean} - Whether to enable debug logging.
90
+ * @returns {Array<AnyLayer | Group>} The parsed layers.
91
+ */
92
+ static layersParse(data, opts) {
93
+ return data.map((layer) => {
94
+ if (opts?.debug)
95
+ LazyUtil_1.LazyLog.log('info', `Parsing layer ${layer.id}...\nData:`, layer);
96
+ const misc = {
97
+ id: layer.id,
98
+ zIndex: layer.zIndex,
99
+ visible: layer.visible,
100
+ };
101
+ if (layer.type === types_1.LayerType.Group) {
102
+ return new components_1.Group(misc).add(...layer.layers.map((l) => this.layerParse(l)));
103
+ }
104
+ else {
105
+ return this.layerParse(layer, misc);
106
+ }
107
+ });
108
+ }
109
+ /**
110
+ * Parses a single JSON layer into an AnyLayer or Group instance.
111
+ * @param layer {JSONLayer | IGroup | Group} - The JSON layer to parse.
112
+ * @param misc {IBaseLayerMisc} - Miscellaneous options for the layer.
113
+ * @returns {AnyLayer | Group} The parsed layer.
114
+ */
115
+ static layerParse(layer, misc) {
116
+ if (layer instanceof components_1.Group) {
117
+ return new components_1.Group(misc).add(...layer.layers.map((l) => this.layerParse(l)));
118
+ }
119
+ else {
120
+ switch (layer.type) {
121
+ case types_1.LayerType.BezierCurve:
122
+ return new components_1.BezierLayer(layer.props, misc).setColor(this.fillParse(layer));
123
+ case types_1.LayerType.QuadraticCurve:
124
+ return new components_1.QuadraticLayer(layer.props, misc).setColor(this.fillParse(layer));
125
+ case types_1.LayerType.Image:
126
+ return new components_1.ImageLayer(layer.props, misc);
127
+ case types_1.LayerType.Text:
128
+ return new components_1.TextLayer(layer.props, misc).setColor(this.fillParse(layer));
129
+ case types_1.LayerType.Morph:
130
+ return new components_1.MorphLayer(layer.props, misc).setColor(this.fillParse(layer));
131
+ case types_1.LayerType.Line:
132
+ return new components_1.LineLayer(layer.props, misc).setColor(this.fillParse(layer));
133
+ case types_1.LayerType.Clear:
134
+ return new components_1.ClearLayer(layer.props, misc);
135
+ case types_1.LayerType.Path:
136
+ return new components_1.Path2DLayer(layer.props, misc).setColor(this.fillParse(layer));
137
+ case types_1.LayerType.Group:
138
+ return new components_1.Group(misc)
139
+ .add(...layer.layers.map((l) => this.layerParse(l)));
140
+ default:
141
+ return layer;
142
+ }
143
+ }
144
+ }
145
+ /**
146
+ * Parses the fill style of a layer.
147
+ * @param layer {JSONLayer} - The layer whose fill style is to be parsed.
148
+ * @returns {string | Gradient | Pattern} The parsed fill style.
149
+ */
150
+ static fillParse(layer) {
151
+ if ('fillStyle' in layer.props && layer.props.fillStyle && typeof layer.props.fillStyle !== 'string') {
152
+ switch (layer.props.fillStyle?.fillType) {
153
+ case 'gradient':
154
+ return new __1.Gradient({ props: layer.props.fillStyle });
155
+ case 'pattern':
156
+ console.log('Pattern:', layer.props.fillStyle);
157
+ return new __1.Pattern()
158
+ .setType(layer.props.fillStyle.type)
159
+ .setSrc(typeof layer.props.fillStyle.src === 'string' ? layer.props.fillStyle.src : this.read(layer.props.fillStyle.src));
160
+ default:
161
+ return layer.props.fillStyle;
162
+ }
163
+ }
164
+ else if ('fillStyle' in layer.props) {
165
+ return layer.props.fillStyle || '#000000';
166
+ }
167
+ else {
168
+ return '#000000';
169
+ }
170
+ }
171
+ }
172
+ exports.JSONReader = JSONReader;
@@ -0,0 +1,20 @@
1
+ export declare class SVGReader {
2
+ private static parseSVG;
3
+ private static createImageLayer;
4
+ private static createEllipseImageLayer;
5
+ private static createRectLayer;
6
+ private static createEllipseLayer;
7
+ private static createCircleLayer;
8
+ private static createLineLayer;
9
+ private static createGradient;
10
+ private static createPattern;
11
+ private static createPath2DLayer;
12
+ private static createClipPathLayer;
13
+ private static createGroupLayer;
14
+ private static applyTransform;
15
+ private static findElementById;
16
+ private static extractIdFromUrl;
17
+ private fillStyle;
18
+ private static handleUseElement;
19
+ static readSVG(svg: string): Promise<any>;
20
+ }