@nmmty/lazycanvas 0.6.0-dev.f33019 → 0.6.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 (58) hide show
  1. package/dist/helpers/Utlis.d.ts +28 -0
  2. package/dist/helpers/Utlis.js +78 -0
  3. package/dist/helpers/index.d.ts +3 -0
  4. package/dist/helpers/index.js +19 -0
  5. package/dist/index.d.ts +5 -10
  6. package/dist/index.js +5 -29
  7. package/dist/structures/LazyCanvas.d.ts +46 -13
  8. package/dist/structures/LazyCanvas.js +66 -22
  9. package/dist/structures/components/BaseLayer.d.ts +16 -16
  10. package/dist/structures/components/BaseLayer.js +17 -17
  11. package/dist/structures/components/BezierLayer.d.ts +21 -21
  12. package/dist/structures/components/BezierLayer.js +20 -20
  13. package/dist/structures/components/ClearLayer.d.ts +15 -15
  14. package/dist/structures/components/ClearLayer.js +14 -14
  15. package/dist/structures/components/Group.d.ts +10 -11
  16. package/dist/structures/components/Group.js +9 -10
  17. package/dist/structures/components/ImageLayer.d.ts +12 -14
  18. package/dist/structures/components/ImageLayer.js +11 -11
  19. package/dist/structures/components/LineLayer.d.ts +20 -20
  20. package/dist/structures/components/LineLayer.js +19 -19
  21. package/dist/structures/components/MorphLayer.d.ts +18 -24
  22. package/dist/structures/components/MorphLayer.js +18 -26
  23. package/dist/structures/components/Path2DLayer.d.ts +4 -78
  24. package/dist/structures/components/Path2DLayer.js +2 -116
  25. package/dist/structures/components/QuadraticLayer.d.ts +22 -22
  26. package/dist/structures/components/QuadraticLayer.js +21 -21
  27. package/dist/structures/components/TextLayer.d.ts +37 -43
  28. package/dist/structures/components/TextLayer.js +37 -45
  29. package/dist/structures/helpers/Exporter.d.ts +9 -11
  30. package/dist/structures/helpers/Exporter.js +49 -32
  31. package/dist/structures/helpers/Font.d.ts +4 -6
  32. package/dist/structures/helpers/Font.js +4 -4
  33. package/dist/structures/helpers/Gradient.d.ts +15 -10
  34. package/dist/structures/helpers/Gradient.js +14 -9
  35. package/dist/structures/helpers/Link.d.ts +5 -5
  36. package/dist/structures/helpers/Link.js +5 -5
  37. package/dist/structures/helpers/Pattern.d.ts +5 -5
  38. package/dist/structures/helpers/Pattern.js +5 -5
  39. package/dist/structures/helpers/index.d.ts +7 -7
  40. package/dist/structures/helpers/readers/JSONReader.d.ts +12 -12
  41. package/dist/structures/helpers/readers/JSONReader.js +29 -19
  42. package/dist/structures/helpers/readers/YAMLReader.d.ts +4 -4
  43. package/dist/structures/helpers/readers/YAMLReader.js +21 -11
  44. package/dist/structures/managers/AnimationManager.d.ts +11 -11
  45. package/dist/structures/managers/AnimationManager.js +11 -11
  46. package/dist/structures/managers/FontsManager.d.ts +14 -15
  47. package/dist/structures/managers/FontsManager.js +14 -15
  48. package/dist/structures/managers/LayersManager.d.ts +22 -12
  49. package/dist/structures/managers/LayersManager.js +27 -12
  50. package/dist/structures/managers/PluginManager.d.ts +230 -0
  51. package/dist/structures/managers/PluginManager.js +182 -0
  52. package/dist/structures/managers/RenderManager.d.ts +10 -12
  53. package/dist/structures/managers/RenderManager.js +31 -16
  54. package/dist/structures/managers/index.d.ts +5 -0
  55. package/dist/structures/managers/index.js +21 -0
  56. package/dist/utils/utils.d.ts +1 -1
  57. package/dist/utils/utils.js +15 -16
  58. package/package.json +59 -56
@@ -19,9 +19,9 @@ class RenderManager {
19
19
  debug;
20
20
  /**
21
21
  * Constructs a new RenderManager instance.
22
- * @param lazyCanvas {LazyCanvas} - The LazyCanvas instance to use for rendering.
23
- * @param opts {Object} - Optional settings for the RenderManager.
24
- * @param opts.debug {boolean} - Whether debugging is enabled.
22
+ * @param {LazyCanvas} [lazyCanvas] - The LazyCanvas instance to use for rendering.
23
+ * @param {Object} [opts] - Optional settings for the RenderManager.
24
+ * @param {boolean} [opts.debug] - Whether debugging is enabled.
25
25
  */
26
26
  constructor(lazyCanvas, opts) {
27
27
  this.lazyCanvas = lazyCanvas;
@@ -29,10 +29,10 @@ class RenderManager {
29
29
  }
30
30
  /**
31
31
  * Merges multiple ImageData objects into a single ImageData object.
32
- * @param ctx {SKRSContext2D} - The canvas rendering context.
33
- * @param imageDataList {ImageData[]} - The list of ImageData objects to merge.
34
- * @param width {number} - The width of the resulting ImageData.
35
- * @param height {number} - The height of the resulting ImageData.
32
+ * @param {SKRSContext2D} [ctx] - The canvas rendering context.
33
+ * @param {ImageData[]} [imageDataList] - The list of ImageData objects to merge.
34
+ * @param {number} [width] - The width of the resulting ImageData.
35
+ * @param {number} [height] - The height of the resulting ImageData.
36
36
  * @returns {ImageData} The merged ImageData object.
37
37
  */
38
38
  mergeImageData(ctx, imageDataList, width, height) {
@@ -59,7 +59,7 @@ class RenderManager {
59
59
  }
60
60
  /**
61
61
  * Renders a single layer or group of layers.
62
- * @param layer {AnyLayer | Group} - The layer or group to render.
62
+ * @param {AnyLayer | Group} [layer] - The layer or group to render.
63
63
  * @returns {Promise<SKRSContext2D>} The canvas rendering context after rendering.
64
64
  */
65
65
  async renderLayer(layer) {
@@ -74,7 +74,7 @@ class RenderManager {
74
74
  }
75
75
  /**
76
76
  * Renders all layers statically and exports the result in the specified format.
77
- * @param exportType {AnyExport} - The export format (e.g., buffer, SVG, or context).
77
+ * @param {AnyExport} [exportType] - The export format (e.g., buffer, SVG, or context).
78
78
  * @returns {Promise<Buffer | SKRSContext2D | string>} The rendered output in the specified format.
79
79
  */
80
80
  async renderStatic(exportType) {
@@ -114,7 +114,10 @@ class RenderManager {
114
114
  const { width, height } = this.lazyCanvas.options;
115
115
  const delay = 1000 / this.lazyCanvas.manager.animation.options.frameRate;
116
116
  const { loop, colorSpace, maxColors, transparency, utils } = this.lazyCanvas.manager.animation.options;
117
+ let frameNumber = 0;
117
118
  for (const layer of this.lazyCanvas.manager.layers.toArray()) {
119
+ // onAnimationFrame hook
120
+ this.lazyCanvas.manager.plugins.executeHook('onAnimationFrame', this.lazyCanvas, frameNumber);
118
121
  const ctx = await this.renderLayer(layer);
119
122
  frameBuffer.push(ctx.getImageData(0, 0, width, height));
120
123
  if (frameBuffer.length > utils.buffer.size) {
@@ -131,38 +134,50 @@ class RenderManager {
131
134
  });
132
135
  if (utils.clear)
133
136
  ctx.clearRect(0, 0, width, height);
137
+ frameNumber++;
134
138
  }
135
139
  encoder.finish();
136
140
  return encoder.bytesView();
137
141
  }
138
142
  /**
139
143
  * Renders all layers and exports the result in the specified format.
140
- * @param format {AnyExport} - The export format (e.g., buffer, context, SVG, or canvas).
144
+ * @param {AnyExport} [format] - The export format (e.g., buffer, context, SVG, or canvas).
141
145
  * @returns {Promise<Buffer | SKRSContext2D | Canvas | SvgCanvas | string>} The rendered output in the specified format.
142
146
  */
143
147
  async render(format) {
148
+ let result;
149
+ // beforeRender hook
150
+ this.lazyCanvas.manager.plugins.executeHook('beforeRender', this.lazyCanvas);
144
151
  switch (format) {
145
152
  case types_1.Export.BUFFER:
146
153
  case "buffer":
147
154
  if (this.lazyCanvas.options.animated) {
148
- return await this.renderAnimation();
155
+ result = await this.renderAnimation();
149
156
  }
150
157
  else {
151
- return await this.renderStatic(types_1.Export.BUFFER);
158
+ result = await this.renderStatic(types_1.Export.BUFFER);
152
159
  }
160
+ break;
153
161
  case types_1.Export.CTX:
154
162
  case "ctx":
155
- return this.lazyCanvas.ctx;
163
+ result = this.lazyCanvas.ctx;
164
+ break;
156
165
  case types_1.Export.SVG:
157
166
  case "svg":
158
- return this.renderStatic(types_1.Export.SVG);
167
+ result = await this.renderStatic(types_1.Export.SVG);
168
+ break;
159
169
  case types_1.Export.CANVAS:
160
170
  case "canvas":
161
171
  await this.renderStatic(this.lazyCanvas.options.exportType === 'svg' ? types_1.Export.SVG : types_1.Export.BUFFER);
162
- return this.lazyCanvas.canvas;
172
+ result = this.lazyCanvas.canvas;
173
+ break;
163
174
  default:
164
- return this.renderStatic(types_1.Export.BUFFER);
175
+ result = await this.renderStatic(types_1.Export.BUFFER);
176
+ break;
165
177
  }
178
+ // afterRender hook
179
+ this.lazyCanvas.manager.plugins.executeHook('afterRender', this.lazyCanvas);
180
+ return result;
166
181
  }
167
182
  }
168
183
  exports.RenderManager = RenderManager;
@@ -0,0 +1,5 @@
1
+ export * from './LayersManager';
2
+ export * from './PluginManager';
3
+ export * from './FontsManager';
4
+ export * from './RenderManager';
5
+ export * from './AnimationManager';
@@ -0,0 +1,21 @@
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("./LayersManager"), exports);
18
+ __exportStar(require("./PluginManager"), exports);
19
+ __exportStar(require("./FontsManager"), exports);
20
+ __exportStar(require("./RenderManager"), exports);
21
+ __exportStar(require("./AnimationManager"), exports);
@@ -1,7 +1,7 @@
1
1
  import type { AnyCentring, AnyLayer, AnyTextAlign, ColorType, PointNumber, ScaleType, Transform } from "../types";
2
2
  import { LayerType } from "../types";
3
3
  import { Canvas, SKRSContext2D, SvgCanvas } from "@napi-rs/canvas";
4
- import { LayersManager } from "../structures/managers/LayersManager";
4
+ import { LayersManager } from "../structures/managers";
5
5
  import { Group } from "../structures/components";
6
6
  export declare function generateID(type: string): string;
7
7
  export declare function isColor(v: ColorType): "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
@@ -1,6 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resizeLayers = exports.resize = exports.getBoundingBoxBezier = exports.centring = exports.isImageUrlValid = exports.generateRandomName = exports.transform = exports.parseFillStyle = exports.filters = exports.opacity = exports.drawShadow = exports.parser = exports.parseToNormal = exports.isColor = exports.generateID = void 0;
3
+ exports.generateID = generateID;
4
+ exports.isColor = isColor;
5
+ exports.parseToNormal = parseToNormal;
6
+ exports.parser = parser;
7
+ exports.drawShadow = drawShadow;
8
+ exports.opacity = opacity;
9
+ exports.filters = filters;
10
+ exports.parseFillStyle = parseFillStyle;
11
+ exports.transform = transform;
12
+ exports.generateRandomName = generateRandomName;
13
+ exports.isImageUrlValid = isImageUrlValid;
14
+ exports.centring = centring;
15
+ exports.getBoundingBoxBezier = getBoundingBoxBezier;
16
+ exports.resize = resize;
17
+ exports.resizeLayers = resizeLayers;
4
18
  const types_1 = require("../types");
5
19
  const helpers_1 = require("../structures/helpers");
6
20
  const canvas_1 = require("@napi-rs/canvas");
@@ -9,7 +23,6 @@ const components_1 = require("../structures/components");
9
23
  function generateID(type) {
10
24
  return `${type}-${Math.random().toString(36).substr(2, 9)}`;
11
25
  }
12
- exports.generateID = generateID;
13
26
  let percentReg = /^(\d+)%$/;
14
27
  let pxReg = /^(\d+)px$/;
15
28
  let canvasReg = /^(vw|vh|vmin|vmax)$/;
@@ -22,7 +35,6 @@ let hslaReg = /^hsla\((\d+),\s*(\d+)%,\s*(\d+)%,\s*(0|0?\.\d+|1(\.0)?)\)$/;
22
35
  function isColor(v) {
23
36
  return typeof (v === 'string' && (hexReg.test(v) || rgbReg.test(v) || rgbaReg.test(v) || hslReg.test(v) || hslaReg.test(v))) || v instanceof helpers_1.Gradient || v instanceof helpers_1.Pattern;
24
37
  }
25
- exports.isColor = isColor;
26
38
  function parseToNormal(v, ctx, canvas, layer = { width: 0, height: 0 }, options = { vertical: false, layer: false }, manager) {
27
39
  if (typeof v === 'number')
28
40
  return v;
@@ -78,7 +90,6 @@ function parseToNormal(v, ctx, canvas, layer = { width: 0, height: 0 }, options
78
90
  }
79
91
  return 0;
80
92
  }
81
- exports.parseToNormal = parseToNormal;
82
93
  function getLayerWidth(anyLayer, ctx, canvas, manager, parserInstance) {
83
94
  if (anyLayer instanceof components_1.LineLayer || anyLayer instanceof components_1.BezierLayer || anyLayer instanceof components_1.QuadraticLayer) {
84
95
  return anyLayer.getBoundingBox(ctx, canvas, manager).width;
@@ -112,7 +123,6 @@ function parser(ctx, canvas, manager) {
112
123
  }
113
124
  };
114
125
  }
115
- exports.parser = parser;
116
126
  function drawShadow(ctx, shadow) {
117
127
  if (shadow) {
118
128
  ctx.shadowColor = shadow.color;
@@ -121,19 +131,16 @@ function drawShadow(ctx, shadow) {
121
131
  ctx.shadowOffsetY = shadow.offsetY || 0;
122
132
  }
123
133
  }
124
- exports.drawShadow = drawShadow;
125
134
  function opacity(ctx, opacity = 1) {
126
135
  if (opacity < 1) {
127
136
  ctx.globalAlpha = opacity;
128
137
  }
129
138
  }
130
- exports.opacity = opacity;
131
139
  function filters(ctx, filters) {
132
140
  if (filters) {
133
141
  ctx.filter = filters;
134
142
  }
135
143
  }
136
- exports.filters = filters;
137
144
  function parseFillStyle(ctx, color, opts) {
138
145
  if (!ctx)
139
146
  throw new LazyUtil_1.LazyError('The context is not defined');
@@ -144,7 +151,6 @@ function parseFillStyle(ctx, color, opts) {
144
151
  }
145
152
  return color;
146
153
  }
147
- exports.parseFillStyle = parseFillStyle;
148
154
  function transform(ctx, transform, layer = { width: 0, height: 0, x: 0, y: 0, type: types_1.LayerType.Morph }, extra = { text: '', textAlign: types_1.TextAlign.Left, fontSize: 0, multiline: false }) {
149
155
  if (transform) {
150
156
  if (transform.translate) {
@@ -197,11 +203,9 @@ function transform(ctx, transform, layer = { width: 0, height: 0, x: 0, y: 0, ty
197
203
  }
198
204
  }
199
205
  }
200
- exports.transform = transform;
201
206
  function generateRandomName() {
202
207
  return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
203
208
  }
204
- exports.generateRandomName = generateRandomName;
205
209
  function isImageUrlValid(src) {
206
210
  try {
207
211
  (0, canvas_1.loadImage)(src);
@@ -211,7 +215,6 @@ function isImageUrlValid(src) {
211
215
  return false;
212
216
  }
213
217
  }
214
- exports.isImageUrlValid = isImageUrlValid;
215
218
  function centring(centring, type, width, height, x, y) {
216
219
  switch (centring) {
217
220
  case types_1.Centring.Center:
@@ -307,7 +310,6 @@ function centring(centring, type, width, height, x, y) {
307
310
  return { x, y };
308
311
  }
309
312
  }
310
- exports.centring = centring;
311
313
  function quadraticBezier(p0, p1, p2, t) {
312
314
  const x = (1 - t) ** 2 * p0.x + 2 * (1 - t) * t * p1.x + t ** 2 * p2.x;
313
315
  const y = (1 - t) ** 2 * p0.y + 2 * (1 - t) * t * p1.y + t ** 2 * p2.y;
@@ -339,7 +341,6 @@ function getBoundingBoxBezier(points, steps = 100) {
339
341
  }
340
342
  return { min: { x: minX, y: minY }, max: { x: maxX, y: maxY }, center: { x: (minX + maxX) / 2, y: (minY + maxY) / 2 }, width: maxX - minX, height: maxY - minY };
341
343
  }
342
- exports.getBoundingBoxBezier = getBoundingBoxBezier;
343
344
  function resize(value, ratio) {
344
345
  if (typeof value === 'number') {
345
346
  return value * ratio;
@@ -358,7 +359,6 @@ function resize(value, ratio) {
358
359
  }
359
360
  return value;
360
361
  }
361
- exports.resize = resize;
362
362
  function resizeLayers(layers, ratio) {
363
363
  let newLayers = [];
364
364
  if (layers.length > 0) {
@@ -411,4 +411,3 @@ function resizeLayers(layers, ratio) {
411
411
  }
412
412
  return newLayers;
413
413
  }
414
- exports.resizeLayers = resizeLayers;
package/package.json CHANGED
@@ -1,56 +1,59 @@
1
- {
2
- "name": "@nmmty/lazycanvas",
3
- "version": "0.6.0-dev.f33019",
4
- "description": "A simple way to interact with @napi-rs/canvas in an advanced way!",
5
- "main": "./dist/index.js",
6
- "types": "./dist/index.d.ts",
7
- "scripts": {
8
- "test": "tsc ./test/test.ts && node ./test/test.js",
9
- "centring": "tsc ./test/centring.ts && node ./test/centring.js",
10
- "logo": "tsc ./test/logo.ts && node ./test/logo.js",
11
- "text": "tsc ./test/text.ts && node ./test/text.js",
12
- "animation": "tsc ./test/animation.ts && node ./test/animation.js",
13
- "iotest": "tsc ./test/iotest.ts && node ./test/iotest.js",
14
- "gradient": "tsc ./test/gradient.ts && node ./test/gradient.js",
15
- "doc": "tsx docgen.ts",
16
- "font": "tsc ./scripts/font-gen.ts && node ./scripts/font-gen.js",
17
- "build": "tsc"
18
- },
19
- "repository": {
20
- "type": "git",
21
- "url": "git+https://github.com/NMMTY/LazyCanvas.git"
22
- },
23
- "keywords": [
24
- "canvas",
25
- "@napi-rs/canvas",
26
- "node-canvas",
27
- "easy",
28
- "simple"
29
- ],
30
- "author": "NMMTY",
31
- "license": "MIT",
32
- "bugs": {
33
- "url": "https://github.com/NMMTY/LazyCanvas/issues"
34
- },
35
- "homepage": "https://github.com/NMMTY/LazyCanvas#readme",
36
- "dependencies": {
37
- "@napi-rs/canvas": "^0.1.72",
38
- "gifenc": "^1.0.3",
39
- "js-yaml": "^4.1.0",
40
- "path": "^0.12.7",
41
- "svgson": "^5.3.1"
42
- },
43
- "devDependencies": {
44
- "@hitomihiumi/colors.ts": "^1.0.3",
45
- "@hitomihiumi/micro-docgen": "^0.3.0",
46
- "@types/js-yaml": "^4.0.9",
47
- "@types/node": "^22.10.2",
48
- "ava": "^6.2.0",
49
- "eslint": "^9.23.0",
50
- "eslint-config-neon": "^0.2.7",
51
- "lodash.merge": "^4.6.2",
52
- "tslib": "^2.8.1",
53
- "tsx": "^4.19.2",
54
- "typescript": "^5.4.5"
55
- }
56
- }
1
+ {
2
+ "name": "@nmmty/lazycanvas",
3
+ "version": "0.6.1",
4
+ "description": "A simple way to interact with @napi-rs/canvas in an advanced way!",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/NMMTY/LazyCanvas.git"
10
+ },
11
+ "keywords": [
12
+ "canvas",
13
+ "@napi-rs/canvas",
14
+ "node-canvas",
15
+ "easy",
16
+ "simple"
17
+ ],
18
+ "author": "NMMTY",
19
+ "license": "MIT",
20
+ "bugs": {
21
+ "url": "https://github.com/NMMTY/LazyCanvas/issues"
22
+ },
23
+ "homepage": "https://github.com/NMMTY/LazyCanvas#readme",
24
+ "dependencies": {
25
+ "@napi-rs/canvas": "^0.1.72",
26
+ "gifenc": "^1.0.3",
27
+ "js-yaml": "^4.1.0",
28
+ "path": "^0.12.7",
29
+ "svgson": "^5.3.1"
30
+ },
31
+ "devDependencies": {
32
+ "@hitomihiumi/colors.ts": "^1.0.3",
33
+ "@types/js-yaml": "^4.0.9",
34
+ "@types/node": "^22.10.2",
35
+ "@typescript-eslint/utils": "^8.39.1",
36
+ "ava": "^6.2.0",
37
+ "eslint": "^9.23.0",
38
+ "eslint-config-neon": "^0.2.7",
39
+ "lodash.merge": "^4.6.2",
40
+ "tslib": "^2.8.1",
41
+ "tsx": "^4.19.2",
42
+ "typescript": "^5.4.5",
43
+ "@hitomihiumi/micro-docgen": "0.4.2"
44
+ },
45
+ "scripts": {
46
+ "test": "tsc ./test/test.ts && node ./test/test.js",
47
+ "centring": "tsc ./test/centring.ts && node ./test/centring.js",
48
+ "logo": "tsc ./test/logo.ts && node ./test/logo.js",
49
+ "text": "tsc ./test/text.ts && node ./test/text.js",
50
+ "animation": "tsc ./test/animation.ts && node ./test/animation.js",
51
+ "iotest": "tsc ./test/iotest.ts && node ./test/iotest.js",
52
+ "gradient": "tsc ./test/gradient.ts && node ./test/gradient.js",
53
+ "docgen": "tsx docgen.ts && tsx ./scripts/post-docgen.ts",
54
+ "lint": "eslint ./src --ext .ts",
55
+ "lint:fix": "eslint ./src --ext .ts --fix",
56
+ "font": "tsx ./scripts/font-gen.ts",
57
+ "build": "tsc && tsx ./scripts/post-build.ts"
58
+ }
59
+ }