@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
@@ -2,21 +2,30 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TextLayer = void 0;
4
4
  const BaseLayer_1 = require("./BaseLayer");
5
- const enum_1 = require("../../types/enum");
5
+ const types_1 = require("../../types");
6
6
  const LazyUtil_1 = require("../../utils/LazyUtil");
7
- const Gradient_1 = require("../helpers/Gradient");
8
7
  const utils_1 = require("../../utils/utils");
9
- const Pattern_1 = require("../helpers/Pattern");
8
+ /**
9
+ * Class representing a Text Layer, extending the BaseLayer class.
10
+ */
10
11
  class TextLayer extends BaseLayer_1.BaseLayer {
12
+ /**
13
+ * The properties of the Text Layer.
14
+ */
11
15
  props;
12
- constructor(props) {
13
- super(enum_1.LayerType.Text, props || {});
16
+ /**
17
+ * Constructs a new TextLayer instance.
18
+ * @param props {ITextLayerProps} - The properties of the Text Layer.
19
+ * @param misc {IBaseLayerMisc} - Miscellaneous options for the layer.
20
+ */
21
+ constructor(props, misc) {
22
+ super(types_1.LayerType.Text, props || {}, misc);
14
23
  this.props = props ? props : {};
15
- this.props.align = enum_1.TextAlign.Left;
24
+ this.props.align = types_1.TextAlign.Left;
16
25
  this.props.font = {
17
26
  family: 'Geist',
18
27
  size: 16,
19
- weight: enum_1.FontWeight.Regular,
28
+ weight: types_1.FontWeight.Regular,
20
29
  };
21
30
  this.props.fillStyle = '#ffffff';
22
31
  this.props.filled = true;
@@ -28,23 +37,26 @@ class TextLayer extends BaseLayer_1.BaseLayer {
28
37
  width: 'vw',
29
38
  height: 0,
30
39
  };
31
- this.props.centring = enum_1.Centring.Center;
40
+ this.props.centring = types_1.Centring.Center;
32
41
  this.props.wordSpacing = 0;
33
42
  this.props.letterSpacing = 0;
34
43
  }
35
44
  /**
36
- * @description Sets the text of the text layer.
37
- * @param text {string} - The `text` of the text layer.
45
+ * Sets the text of the text layer.
46
+ * @param text {string} - The text content of the layer.
47
+ * @returns {this} The current instance for chaining.
38
48
  */
39
49
  setText(text) {
40
50
  this.props.text = text;
41
51
  return this;
42
52
  }
43
53
  /**
44
- * @description Set the font of the text layer. You can use `Geist` and `GeistMono`, or you can upload your own font from file/base64 buffer.
45
- * @param familyOrConfig {string | { font: string; size: number; weight: AnyWeight }} - The `family` of the font. If you want to use FontsList, you can use the object config.
46
- * @param size {number} - The `size` of the font.
47
- * @param weight {AnyWeight} - The `weight` of the font.
54
+ * Sets the font of the text layer.
55
+ * @param familyOrConfig {string | { family: string; size: number; weight: AnyWeight }} - The font family or configuration object.
56
+ * @param size {number} - The font size (required if `familyOrConfig` is a string).
57
+ * @param weight {AnyWeight} - The font weight (required if `familyOrConfig` is a string).
58
+ * @returns {this} The current instance for chaining.
59
+ * @throws {LazyError} If size or weight is not provided when `familyOrConfig` is a string.
48
60
  */
49
61
  setFont(familyOrConfig, size, weight) {
50
62
  if (typeof familyOrConfig === "string") {
@@ -68,11 +80,12 @@ class TextLayer extends BaseLayer_1.BaseLayer {
68
80
  return this;
69
81
  }
70
82
  /**
71
- * @description Set the multiline of the text layer. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
72
- * @param enabled {boolean} - Whether the text is multiline.
73
- * @param width {ScaleType} - width of "window" the multiline text. Can be used in one line text for text max width.
74
- * @param height {ScaleType} - height of "window" the multiline text.
75
- * @param spacing {number} - The space between the lines.
83
+ * Configures the multiline properties of the text layer.
84
+ * @param enabled {boolean} - Whether multiline is enabled.
85
+ * @param width {ScaleType} - The width of the multiline text area.
86
+ * @param height {ScaleType} - The height of the multiline text area.
87
+ * @param spacing {number} - The spacing between lines (optional).
88
+ * @returns {this} The current instance for chaining.
76
89
  */
77
90
  setMultiline(enabled, width, height, spacing) {
78
91
  this.props.multiline = {
@@ -86,57 +99,55 @@ class TextLayer extends BaseLayer_1.BaseLayer {
86
99
  return this;
87
100
  }
88
101
  /**
89
- * @description Sets the color of the layer. You can use `hex`, `rgb`, `rgba`, `hsl`, `hsla`, and `Gradient`color.
90
- * @param color {string} - The `color` of the layer.
102
+ * Sets the color of the text layer.
103
+ * @param color {ColorType} - The color of the text.
104
+ * @returns {this} The current instance for chaining.
105
+ * @throws {LazyError} If the color is not provided or invalid.
91
106
  */
92
107
  setColor(color) {
93
108
  if (!color)
94
109
  throw new LazyUtil_1.LazyError('The color of the layer must be provided');
95
110
  if (!(0, utils_1.isColor)(color))
96
111
  throw new LazyUtil_1.LazyError('The color of the layer must be a valid color');
97
- let fill = (0, utils_1.parseColor)(color);
98
- if (fill instanceof Gradient_1.Gradient || fill instanceof Pattern_1.Pattern) {
99
- this.props.fillStyle = fill;
100
- }
101
- else {
102
- let arr = fill.split(':');
103
- this.props.fillStyle = arr[0];
104
- this.props.opacity = parseFloat(arr[1]) || 1;
105
- }
112
+ this.props.fillStyle = color;
106
113
  return this;
107
114
  }
108
115
  /**
109
- * @description Set the align of the text layer.
110
- * @param align {AnyTextAlign} - The `align` of the text layer.
116
+ * Sets the alignment of the text layer.
117
+ * @param align {AnyTextAlign} - The alignment of the text.
118
+ * @returns {this} The current instance for chaining.
111
119
  */
112
120
  setAlign(align) {
113
121
  this.props.align = align;
114
122
  return this;
115
123
  }
116
124
  /**
117
- * @description Set the baseline of the text layer.
118
- * @param baseline {AnyTextBaseline} - The `baseline` of the text layer.
125
+ * Sets the baseline of the text layer.
126
+ * @param baseline {AnyTextBaseline} - The baseline of the text.
127
+ * @returns {this} The current instance for chaining.
119
128
  */
120
129
  setBaseline(baseline) {
121
130
  this.props.baseline = baseline;
122
131
  return this;
123
132
  }
124
133
  /**
125
- * @description Set the direction of the text layer.
126
- * @param direction {AnyTextDirection} - The `direction` of the text layer.
134
+ * Sets the direction of the text layer.
135
+ * @param direction {AnyTextDirection} - The direction of the text.
136
+ * @returns {this} The current instance for chaining.
127
137
  */
128
138
  setDirection(direction) {
129
139
  this.props.direction = direction;
130
140
  return this;
131
141
  }
132
142
  /**
133
- * @description Set the stroke of the layer.
134
- * @param width {number} - The `width` of the stroke.
135
- * @param cap {string} - The `cap` of the stroke.
136
- * @param join {string} - The `join` of the stroke.
137
- * @param dash {number[]} - The `dash` of the stroke.
138
- * @param dashOffset {number} - The `dashOffset` of the stroke.
139
- * @param miterLimit {number} - The `miterLimit` of the stroke.
143
+ * Configures the stroke properties of the text layer.
144
+ * @param width {number} - The width of the stroke.
145
+ * @param cap {string} - The cap style of the stroke (optional).
146
+ * @param join {string} - The join style of the stroke (optional).
147
+ * @param dash {number[]} - The dash pattern of the stroke (optional).
148
+ * @param dashOffset {number} - The dash offset of the stroke (optional).
149
+ * @param miterLimit {number} - The miter limit of the stroke (optional).
150
+ * @returns {this} The current instance for chaining.
140
151
  */
141
152
  setStroke(width, cap, join, dash, dashOffset, miterLimit) {
142
153
  this.props.stroke = {
@@ -150,29 +161,38 @@ class TextLayer extends BaseLayer_1.BaseLayer {
150
161
  return this;
151
162
  }
152
163
  /**
153
- * @description Sets the fills of the layer. If `true` the layer will be filled, if `false` the layer will be stroked.
154
- * @param filled {boolean} - The `filled` of the layer.
164
+ * Sets whether the text layer should be filled or stroked.
165
+ * @param filled {boolean} - If true, the layer will be filled; otherwise, it will be stroked.
166
+ * @returns {this} The current instance for chaining.
155
167
  */
156
168
  setFilled(filled) {
157
169
  this.props.filled = filled;
158
170
  return this;
159
171
  }
160
172
  /**
161
- * @description Sets the spacing between the words.
162
- * @param wordSpacing {number} - The `wordSpacing` of the text layer.
173
+ * Sets the spacing between words in the text layer.
174
+ * @param wordSpacing {number} - The spacing between words.
175
+ * @returns {this} The current instance for chaining.
163
176
  */
164
177
  setWordSpacing(wordSpacing) {
165
178
  this.props.wordSpacing = wordSpacing;
166
179
  return this;
167
180
  }
168
181
  /**
169
- * @description Sets the letter spacing.
170
- * @param letterSpacing {number} - The `letterSpacing` of the text layer.
182
+ * Sets the spacing between letters in the text layer.
183
+ * @param letterSpacing {number} - The spacing between letters.
184
+ * @returns {this} The current instance for chaining.
171
185
  */
172
186
  setLetterSpacing(letterSpacing) {
173
187
  this.props.letterSpacing = letterSpacing;
174
188
  return this;
175
189
  }
190
+ /**
191
+ * Measures the dimensions of the text.
192
+ * @param ctx {SKRSContext2D} - The canvas rendering context.
193
+ * @param canvas {Canvas | SvgCanvas} - The canvas instance.
194
+ * @returns {Object} The width and height of the text.
195
+ */
176
196
  measureText(ctx, canvas) {
177
197
  const w = (0, utils_1.parseToNormal)(this.props.size?.width, ctx, canvas);
178
198
  const h = (0, utils_1.parseToNormal)(this.props.size?.height, ctx, canvas, { width: w, height: 0 }, { vertical: true });
@@ -185,6 +205,13 @@ class TextLayer extends BaseLayer_1.BaseLayer {
185
205
  return { width: data.width, height: this.props.font.size };
186
206
  }
187
207
  }
208
+ /**
209
+ * Draws the text layer on the canvas.
210
+ * @param ctx {SKRSContext2D} - The canvas rendering context.
211
+ * @param canvas {Canvas | SvgCanvas} - The canvas instance.
212
+ * @param manager {LayersManager} - The layers manager.
213
+ * @param debug {boolean} - Whether to enable debug logging.
214
+ */
188
215
  async draw(ctx, canvas, manager, debug) {
189
216
  const parcer = (0, utils_1.parser)(ctx, canvas, manager);
190
217
  const { x, y, w } = parcer.parseBatch({
@@ -234,17 +261,27 @@ class TextLayer extends BaseLayer_1.BaseLayer {
234
261
  break;
235
262
  }
236
263
  for (let line of lines) {
237
- this.drawText(this.props, ctx, await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle), line.text, line.x, line.y, w, h);
264
+ this.drawText(this.props, ctx, await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle), line.text, line.x, line.y, w);
238
265
  }
239
266
  }
240
267
  else {
241
268
  ctx.font = `${this.props.font.weight} ${this.props.font.size}px ${this.props.font.family}`;
242
- this.drawText(this.props, ctx, await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle), this.props.text, x, y, w, h);
269
+ this.drawText(this.props, ctx, await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle), this.props.text, x, y, w);
243
270
  }
244
271
  ctx.closePath();
245
272
  ctx.restore();
246
273
  }
247
- drawText(props, ctx, fillStyle, text, x, y, w, h) {
274
+ /**
275
+ * Draws the text on the canvas.
276
+ * @param props {ITextLayerProps} - The properties of the text layer.
277
+ * @param ctx {SKRSContext2D} - The canvas rendering context.
278
+ * @param fillStyle {string | CanvasGradient | CanvasPattern} - The fill style for the text.
279
+ * @param text {string} - The text content.
280
+ * @param x {number} - The x-coordinate of the text.
281
+ * @param y {number} - The y-coordinate of the text.
282
+ * @param w {number} - The width of the text area.
283
+ */
284
+ drawText(props, ctx, fillStyle, text, x, y, w) {
248
285
  if (props.filled) {
249
286
  ctx.fillStyle = fillStyle;
250
287
  ctx.fillText(text, x, y, w);
@@ -261,12 +298,18 @@ class TextLayer extends BaseLayer_1.BaseLayer {
261
298
  }
262
299
  }
263
300
  /**
264
- * @returns {ITextLayer}
301
+ * Converts the Text Layer to a JSON representation.
302
+ * @returns {ITextLayer} The JSON representation of the Text Layer.
265
303
  */
266
304
  toJSON() {
267
305
  let data = super.toJSON();
268
- data.props = this.props;
269
- return { ...data };
306
+ let copy = { ...this.props };
307
+ for (const key of ['x', 'y', 'size.width', 'size.height', 'fillStyle']) {
308
+ if (copy[key] && typeof copy[key] === 'object' && 'toJSON' in copy[key]) {
309
+ copy[key] = copy[key].toJSON();
310
+ }
311
+ }
312
+ return { ...data, props: copy };
270
313
  }
271
314
  }
272
315
  exports.TextLayer = TextLayer;
@@ -0,0 +1,10 @@
1
+ export * from './BaseLayer';
2
+ export * from './BezierLayer';
3
+ export * from './ClearLayer';
4
+ export * from './ImageLayer';
5
+ export * from './TextLayer';
6
+ export * from './MorphLayer';
7
+ export * from './Group';
8
+ export * from './LineLayer';
9
+ export * from './QuadraticLayer';
10
+ export * from './Path2DLayer';
@@ -0,0 +1,26 @@
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("./BaseLayer"), exports);
18
+ __exportStar(require("./BezierLayer"), exports);
19
+ __exportStar(require("./ClearLayer"), exports);
20
+ __exportStar(require("./ImageLayer"), exports);
21
+ __exportStar(require("./TextLayer"), exports);
22
+ __exportStar(require("./MorphLayer"), exports);
23
+ __exportStar(require("./Group"), exports);
24
+ __exportStar(require("./LineLayer"), exports);
25
+ __exportStar(require("./QuadraticLayer"), exports);
26
+ __exportStar(require("./Path2DLayer"), exports);
@@ -0,0 +1,52 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import { IOLazyCanvas, LazyCanvas } from "../LazyCanvas";
4
+ import { AnyExport } from "../../types";
5
+ import { Canvas, SKRSContext2D, SvgCanvas } from "@napi-rs/canvas";
6
+ /**
7
+ * Class responsible for exporting a LazyCanvas instance to various formats.
8
+ */
9
+ export declare class Exporter {
10
+ /**
11
+ * The LazyCanvas instance to be exported.
12
+ */
13
+ canvas: LazyCanvas;
14
+ /**
15
+ * Constructs a new Exporter instance.
16
+ * @param canvas {LazyCanvas} - The LazyCanvas instance to be exported.
17
+ */
18
+ constructor(canvas: LazyCanvas);
19
+ /**
20
+ * Saves a file to the filesystem.
21
+ * @param buffer {any} - The data to be saved.
22
+ * @param extension {Extensions} - The file extension.
23
+ * @param name {string} - The name of the file (optional).
24
+ * @throws {LazyError} If the buffer or extension is not provided.
25
+ */
26
+ private saveFile;
27
+ /**
28
+ * Exports all layers from the LayersManager as an array of JSON objects.
29
+ * @param manager {LayersManager} - The LayersManager instance.
30
+ * @returns {any[]} An array of JSON representations of the layers.
31
+ */
32
+ private exportLayers;
33
+ /**
34
+ * Exports the canvas to the specified format.
35
+ * @param exportType {AnyExport} - The type of export (e.g., "png", "json").
36
+ * @param opts {Object} - Optional settings.
37
+ * @param opts.name {string} - The name of the file (optional).
38
+ * @param opts.saveAsFile {boolean} - Whether to save the export as a file (optional).
39
+ * @returns {Promise<Buffer | SKRSContext2D | Canvas | SvgCanvas | string>} The exported data.
40
+ * @throws {LazyError} If the export type is not supported.
41
+ */
42
+ export(exportType: AnyExport, opts?: {
43
+ name?: string;
44
+ saveAsFile?: boolean;
45
+ }): Promise<Buffer | SKRSContext2D | Canvas | SvgCanvas | string>;
46
+ /**
47
+ * Synchronously exports the canvas to the specified format.
48
+ * @param exportType {AnyExport} - The type of export (e.g., "json").
49
+ * @returns {IOLazyCanvas | void} The exported data or void if the export type is unsupported.
50
+ */
51
+ syncExport(exportType: AnyExport): IOLazyCanvas | void;
52
+ }
@@ -0,0 +1,168 @@
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.Exporter = void 0;
27
+ const types_1 = require("../../types");
28
+ const LazyUtil_1 = require("../../utils/LazyUtil");
29
+ const fs = __importStar(require("fs"));
30
+ const utils_1 = require("../../utils/utils");
31
+ const _yaml = __importStar(require("js-yaml"));
32
+ /**
33
+ * Class responsible for exporting a LazyCanvas instance to various formats.
34
+ */
35
+ class Exporter {
36
+ /**
37
+ * The LazyCanvas instance to be exported.
38
+ */
39
+ canvas;
40
+ /**
41
+ * Constructs a new Exporter instance.
42
+ * @param canvas {LazyCanvas} - The LazyCanvas instance to be exported.
43
+ */
44
+ constructor(canvas) {
45
+ this.canvas = canvas;
46
+ }
47
+ /**
48
+ * Saves a file to the filesystem.
49
+ * @param buffer {any} - The data to be saved.
50
+ * @param extension {Extensions} - The file extension.
51
+ * @param name {string} - The name of the file (optional).
52
+ * @throws {LazyError} If the buffer or extension is not provided.
53
+ */
54
+ async saveFile(buffer, extension, name) {
55
+ if (!buffer)
56
+ throw new LazyUtil_1.LazyError('Buffer must be provided');
57
+ if (!extension)
58
+ throw new LazyUtil_1.LazyError('Extension must be provided');
59
+ fs.writeFileSync(`${name === undefined ? (0, utils_1.generateRandomName)() : name}.${extension}`, buffer);
60
+ }
61
+ /**
62
+ * Exports all layers from the LayersManager as an array of JSON objects.
63
+ * @param manager {LayersManager} - The LayersManager instance.
64
+ * @returns {any[]} An array of JSON representations of the layers.
65
+ */
66
+ exportLayers(manager) {
67
+ let arr = [];
68
+ for (const layer of Array.from(manager.map.values())) {
69
+ arr.push(layer.toJSON());
70
+ }
71
+ return arr;
72
+ }
73
+ /**
74
+ * Exports the canvas to the specified format.
75
+ * @param exportType {AnyExport} - The type of export (e.g., "png", "json").
76
+ * @param opts {Object} - Optional settings.
77
+ * @param opts.name {string} - The name of the file (optional).
78
+ * @param opts.saveAsFile {boolean} - Whether to save the export as a file (optional).
79
+ * @returns {Promise<Buffer | SKRSContext2D | Canvas | SvgCanvas | string>} The exported data.
80
+ * @throws {LazyError} If the export type is not supported.
81
+ */
82
+ async export(exportType, opts) {
83
+ switch (exportType) {
84
+ case types_1.Export.CTX:
85
+ case "ctx":
86
+ return await this.canvas.manager.render.render(exportType);
87
+ case types_1.Export.SVG:
88
+ case "svg":
89
+ const svg = await this.canvas.manager.render.render('svg');
90
+ if (opts?.saveAsFile) {
91
+ await this.saveFile(svg, 'svg', opts.name);
92
+ }
93
+ return svg;
94
+ case types_1.Export.BUFFER:
95
+ case "buffer":
96
+ const buffer = await this.canvas.manager.render.render('buffer');
97
+ if (opts?.saveAsFile) {
98
+ await this.saveFile(buffer, 'png', opts.name);
99
+ }
100
+ return buffer;
101
+ case types_1.Export.GIF:
102
+ case "gif":
103
+ const gif = await this.canvas.manager.render.render('buffer');
104
+ if (opts?.saveAsFile) {
105
+ await this.saveFile(gif, 'gif', opts.name);
106
+ }
107
+ return gif;
108
+ case types_1.Export.WEBP:
109
+ case "webp":
110
+ const webp = await this.canvas.manager.render.render('buffer');
111
+ if (opts?.saveAsFile) {
112
+ await this.saveFile(webp, 'webp', opts.name);
113
+ }
114
+ return webp;
115
+ case types_1.Export.JPEG:
116
+ case "jpeg":
117
+ const jpeg = await this.canvas.manager.render.render('buffer');
118
+ await this.saveFile(jpeg, 'jpeg', opts?.name);
119
+ return jpeg;
120
+ case types_1.Export.JPG:
121
+ case "jpg":
122
+ const jpg = await this.canvas.manager.render.render('buffer');
123
+ await this.saveFile(jpg, 'jpg', opts?.name);
124
+ return jpg;
125
+ case types_1.Export.PNG:
126
+ case "png":
127
+ const png = await this.canvas.manager.render.render('buffer');
128
+ await this.saveFile(png, 'png', opts?.name);
129
+ return png;
130
+ case types_1.Export.JSON:
131
+ case "json":
132
+ const json = this.syncExport(exportType);
133
+ if (opts?.saveAsFile) {
134
+ await this.saveFile(JSON.stringify(json), 'json', opts.name);
135
+ }
136
+ return JSON.stringify(json);
137
+ case types_1.Export.CANVAS:
138
+ case "canvas":
139
+ return await this.canvas.manager.render.render(exportType);
140
+ case types_1.Export.YAML:
141
+ case "yaml":
142
+ const yaml = _yaml.dump(this.syncExport(types_1.Export.JSON));
143
+ if (opts?.saveAsFile) {
144
+ await this.saveFile(yaml, 'yaml', opts.name);
145
+ }
146
+ return yaml;
147
+ default:
148
+ throw new LazyUtil_1.LazyError(`Export type ${exportType} is not supported`);
149
+ }
150
+ }
151
+ /**
152
+ * Synchronously exports the canvas to the specified format.
153
+ * @param exportType {AnyExport} - The type of export (e.g., "json").
154
+ * @returns {IOLazyCanvas | void} The exported data or void if the export type is unsupported.
155
+ */
156
+ syncExport(exportType) {
157
+ switch (exportType) {
158
+ case types_1.Export.JSON:
159
+ case "json":
160
+ return {
161
+ options: this.canvas.options,
162
+ animation: this.canvas.manager.animation.options,
163
+ layers: this.exportLayers(this.canvas.manager.layers)
164
+ };
165
+ }
166
+ }
167
+ }
168
+ exports.Exporter = Exporter;
@@ -1,35 +1,89 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
3
  import { AnyWeight } from "../../types";
4
- import { IFont } from "../../types";
4
+ /**
5
+ * Interface representing a font.
6
+ */
7
+ export interface IFont {
8
+ /**
9
+ * The font family.
10
+ */
11
+ family: string;
12
+ /**
13
+ * The weight of the font.
14
+ */
15
+ weight: AnyWeight;
16
+ /**
17
+ * The file path of the font (optional).
18
+ */
19
+ path?: string;
20
+ /**
21
+ * The base64 representation of the font (optional).
22
+ */
23
+ base64?: Buffer;
24
+ }
25
+ /**
26
+ * Interface representing a collection of fonts.
27
+ * Each font family maps to a record of font weights and their corresponding buffers.
28
+ */
29
+ export interface IFonts {
30
+ [family: string]: Record<number, Buffer>;
31
+ }
32
+ /**
33
+ * Class representing a font with properties such as family, weight, path, and base64.
34
+ */
5
35
  export declare class Font implements IFont {
36
+ /**
37
+ * The font family.
38
+ */
6
39
  family: string;
40
+ /**
41
+ * The weight of the font.
42
+ */
7
43
  weight: AnyWeight;
44
+ /**
45
+ * The file path of the font (optional).
46
+ */
8
47
  path?: string;
48
+ /**
49
+ * The base64 representation of the font (optional).
50
+ */
9
51
  base64?: Buffer;
52
+ /**
53
+ * Constructs a new Font instance with default values.
54
+ */
10
55
  constructor();
11
56
  /**
12
- * Set the font family
13
- * @param family {string} - The `family` of the font
57
+ * Sets the font family.
58
+ * @param family {string} - The `family` of the font.
59
+ * @returns {this} The current instance for chaining.
60
+ * @throws {Error} If the family is not provided.
14
61
  */
15
62
  setFamily(family: string): this;
16
63
  /**
17
- * Set the font weight
18
- * @param weight {AnyWeight} - The `weight` of the font
64
+ * Sets the font weight.
65
+ * @param weight {AnyWeight} - The `weight` of the font.
66
+ * @returns {this} The current instance for chaining.
67
+ * @throws {Error} If the weight is not provided.
19
68
  */
20
69
  setWeight(weight: AnyWeight): this;
21
70
  /**
22
- * Set the path of the font
23
- * @param path {string} - The `path` of the font
71
+ * Sets the file path of the font.
72
+ * @param path {string} - The `path` of the font.
73
+ * @returns {this} The current instance for chaining.
74
+ * @throws {Error} If the path is not provided.
24
75
  */
25
76
  setPath(path: string): this;
26
77
  /**
27
- * Set the base64 of the font
28
- * @param base64 {string} - The `base64` of the font
78
+ * Sets the base64 representation of the font.
79
+ * @param base64 {Buffer} - The `base64` of the font.
80
+ * @returns {this} The current instance for chaining.
81
+ * @throws {Error} If the base64 is not provided.
29
82
  */
30
83
  setBase64(base64: Buffer): this;
31
84
  /**
32
- * @returns {IFont}
85
+ * Converts the Font instance to a JSON representation.
86
+ * @returns {IFont} The JSON representation of the font.
33
87
  */
34
88
  toJSON(): IFont;
35
89
  }