@nmmty/lazycanvas 0.2.0 → 0.3.0

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 (40) hide show
  1. package/dist/index.d.ts +4 -2
  2. package/dist/index.js +5 -1
  3. package/dist/structures/LazyCanvas.d.ts +2 -10
  4. package/dist/structures/LazyCanvas.js +4 -15
  5. package/dist/structures/components/BezierLayer.d.ts +41 -0
  6. package/dist/structures/components/BezierLayer.js +119 -0
  7. package/dist/structures/components/Group.d.ts +1 -0
  8. package/dist/structures/components/Group.js +8 -0
  9. package/dist/structures/components/ImageLayer.d.ts +1 -1
  10. package/dist/structures/components/ImageLayer.js +3 -1
  11. package/dist/structures/components/MorphLayer.d.ts +1 -1
  12. package/dist/structures/components/MorphLayer.js +5 -10
  13. package/dist/structures/components/QuadraticLayer.d.ts +39 -0
  14. package/dist/structures/components/QuadraticLayer.js +116 -0
  15. package/dist/structures/components/TextLayer.d.ts +1 -1
  16. package/dist/structures/components/TextLayer.js +3 -2
  17. package/dist/structures/helpers/Font.js +2 -1
  18. package/dist/structures/managers/FontsManager.d.ts +10 -1
  19. package/dist/structures/managers/FontsManager.js +19 -2
  20. package/dist/structures/managers/LayersManager.d.ts +2 -1
  21. package/dist/structures/managers/LayersManager.js +9 -2
  22. package/dist/structures/managers/RenderManager.d.ts +2 -1
  23. package/dist/structures/managers/RenderManager.js +8 -3
  24. package/dist/types/components/BaseLayer.d.ts +6 -6
  25. package/dist/types/components/BezierLayer.d.ts +11 -0
  26. package/dist/types/components/MorphLayer.d.ts +1 -1
  27. package/dist/types/components/QuadraticLayer.d.ts +11 -0
  28. package/dist/types/components/TextLayer.d.ts +0 -1
  29. package/dist/types/enum.d.ts +9 -1
  30. package/dist/types/enum.js +8 -0
  31. package/dist/types/index.d.ts +2 -0
  32. package/dist/types/managers/FontsManager.d.ts +1 -0
  33. package/dist/types/managers/LayersManager.d.ts +1 -0
  34. package/dist/types/managers/RenderManager.d.ts +1 -0
  35. package/dist/types/types.d.ts +14 -2
  36. package/dist/utils/LazyUtil.d.ts +1 -1
  37. package/dist/utils/LazyUtil.js +4 -4
  38. package/dist/utils/utils.d.ts +17 -1
  39. package/dist/utils/utils.js +108 -13
  40. package/package.json +49 -51
package/dist/index.d.ts CHANGED
@@ -2,13 +2,15 @@ import { LazyCanvas } from "./structures/LazyCanvas";
2
2
  import { ImageLayer } from "./structures/components/ImageLayer";
3
3
  import { MorphLayer } from "./structures/components/MorphLayer";
4
4
  import { TextLayer } from "./structures/components/TextLayer";
5
+ import { BezierLayer } from "./structures/components/BezierLayer";
6
+ import { QuadraticLayer } from "./structures/components/QuadraticLayer";
5
7
  import { BaseLayer } from "./structures/components/BaseLayer";
6
8
  import { Group } from "./structures/components/Group";
7
9
  import { LayerType, LayerScaleType, LineCap, LineJoin, TextAlign, TextDirection, TextBaseline, FontWeight, Export, Centring, PatternType, SaveFormat, GradientType } from "./types/enum";
8
- import type { AnyLayer, ScaleType, ColorType, IFont, IFonts, IGradient, IPattern, IImageLayer, IImageLayerProps, IMorphLayer, IMorphLayerProps, ITextLayer, ITextLayerProps, IBaseLayer, IBaseLayerProps, IGroup } from "./types";
10
+ import type { AnyLayer, ScaleType, ColorType, IFont, IFonts, IGradient, IPattern, IImageLayer, IImageLayerProps, IMorphLayer, IMorphLayerProps, ITextLayer, ITextLayerProps, IBezierLayer, IBezierLayerProps, IQuadraticLayer, IQuadraticLayerProps, IBaseLayer, IBaseLayerProps, IGroup } from "./types";
9
11
  import { Font } from "./structures/helpers/Font";
10
12
  import { Gradient } from "./structures/helpers/Gradient";
11
13
  import { Pattern } from "./structures/helpers/Pattern";
12
14
  import { saveFile } from "./utils/utils";
13
15
  import { Filters } from "./helpers/filters";
14
- export { LazyCanvas, ImageLayer, MorphLayer, TextLayer, BaseLayer, Group, Font, Gradient, Pattern, LayerScaleType, LayerType, FontWeight, GradientType, Export, LineCap, LineJoin, TextAlign, TextDirection, TextBaseline, SaveFormat, Centring, PatternType, saveFile, Filters, IFont, IFonts, IGradient, IPattern, IImageLayer, IImageLayerProps, IMorphLayer, IMorphLayerProps, ITextLayer, ITextLayerProps, IBaseLayer, IBaseLayerProps, IGroup, AnyLayer, ScaleType, ColorType, };
16
+ export { LazyCanvas, ImageLayer, MorphLayer, TextLayer, BezierLayer, QuadraticLayer, BaseLayer, Group, Font, Gradient, Pattern, LayerScaleType, LayerType, FontWeight, GradientType, Export, LineCap, LineJoin, TextAlign, TextDirection, TextBaseline, SaveFormat, Centring, PatternType, saveFile, Filters, IFont, IFonts, IGradient, IPattern, IImageLayer, IImageLayerProps, IMorphLayer, IMorphLayerProps, ITextLayer, ITextLayerProps, IBaseLayer, IBaseLayerProps, IBezierLayer, IBezierLayerProps, IQuadraticLayer, IQuadraticLayerProps, IGroup, AnyLayer, ScaleType, ColorType, };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Filters = exports.saveFile = exports.PatternType = exports.Centring = exports.SaveFormat = exports.TextBaseline = exports.TextDirection = exports.TextAlign = exports.LineJoin = exports.LineCap = exports.Export = exports.GradientType = exports.FontWeight = exports.LayerType = exports.LayerScaleType = exports.Pattern = exports.Gradient = exports.Font = exports.Group = exports.BaseLayer = exports.TextLayer = exports.MorphLayer = exports.ImageLayer = exports.LazyCanvas = void 0;
3
+ exports.Filters = exports.saveFile = exports.PatternType = exports.Centring = exports.SaveFormat = exports.TextBaseline = exports.TextDirection = exports.TextAlign = exports.LineJoin = exports.LineCap = exports.Export = exports.GradientType = exports.FontWeight = exports.LayerType = exports.LayerScaleType = exports.Pattern = exports.Gradient = exports.Font = exports.Group = exports.BaseLayer = exports.QuadraticLayer = exports.BezierLayer = exports.TextLayer = exports.MorphLayer = exports.ImageLayer = exports.LazyCanvas = void 0;
4
4
  const LazyCanvas_1 = require("./structures/LazyCanvas");
5
5
  Object.defineProperty(exports, "LazyCanvas", { enumerable: true, get: function () { return LazyCanvas_1.LazyCanvas; } });
6
6
  const ImageLayer_1 = require("./structures/components/ImageLayer");
@@ -9,6 +9,10 @@ const MorphLayer_1 = require("./structures/components/MorphLayer");
9
9
  Object.defineProperty(exports, "MorphLayer", { enumerable: true, get: function () { return MorphLayer_1.MorphLayer; } });
10
10
  const TextLayer_1 = require("./structures/components/TextLayer");
11
11
  Object.defineProperty(exports, "TextLayer", { enumerable: true, get: function () { return TextLayer_1.TextLayer; } });
12
+ const BezierLayer_1 = require("./structures/components/BezierLayer");
13
+ Object.defineProperty(exports, "BezierLayer", { enumerable: true, get: function () { return BezierLayer_1.BezierLayer; } });
14
+ const QuadraticLayer_1 = require("./structures/components/QuadraticLayer");
15
+ Object.defineProperty(exports, "QuadraticLayer", { enumerable: true, get: function () { return QuadraticLayer_1.QuadraticLayer; } });
12
16
  const BaseLayer_1 = require("./structures/components/BaseLayer");
13
17
  Object.defineProperty(exports, "BaseLayer", { enumerable: true, get: function () { return BaseLayer_1.BaseLayer; } });
14
18
  const Group_1 = require("./structures/components/Group");
@@ -1,5 +1,5 @@
1
1
  import { Export } from "../types/enum";
2
- import { IFonts, ILazyCanvas } from "../types";
2
+ import { ILazyCanvas } from "../types";
3
3
  import { Canvas, SKRSContext2D, SvgExportFlag } from "@napi-rs/canvas";
4
4
  import { LayersManager } from "./managers/LayersManager";
5
5
  import { RenderManager } from "./managers/RenderManager";
@@ -13,20 +13,12 @@ export declare class LazyCanvas implements ILazyCanvas {
13
13
  render: RenderManager;
14
14
  fonts: FontsManager;
15
15
  exportType: Export;
16
- constructor();
16
+ constructor(debug?: boolean);
17
17
  /**
18
18
  * Set the export type
19
19
  * @param type {Export} - The `export` type
20
20
  */
21
21
  setExportType(type: Export): this;
22
- /**
23
- * Replace base fonts with custom fonts by special file.
24
- * Use this method before loading fonts by `FontManager`.
25
- * The file should be generated by the following instructions:
26
- * @see https://github.com/NMMTY/LazyCanvas/blob/main/scripts/FontsGenerate.md
27
- * @param fonts {IFonts[]} - The `fonts` to set
28
- */
29
- setFonts(fonts: IFonts): this;
30
22
  /**
31
23
  * Set the SVG export flag. This method should be called after `setExportType` method.
32
24
  * @param flag {SvgExportFlag} - The `flag` of the SVG export
@@ -15,14 +15,14 @@ class LazyCanvas {
15
15
  render;
16
16
  fonts;
17
17
  exportType;
18
- constructor() {
18
+ constructor(debug = false) {
19
19
  this.width = 0;
20
20
  this.height = 0;
21
21
  this.canvas = new canvas_1.Canvas(0, 0);
22
22
  this.ctx = this.canvas.getContext('2d');
23
- this.layers = new LayersManager_1.LayersManager();
24
- this.render = new RenderManager_1.RenderManager(this);
25
- this.fonts = new FontsManager_1.FontsManager();
23
+ this.layers = new LayersManager_1.LayersManager(debug);
24
+ this.render = new RenderManager_1.RenderManager(this, debug);
25
+ this.fonts = new FontsManager_1.FontsManager(debug);
26
26
  this.exportType = enum_1.Export.Buffer;
27
27
  }
28
28
  /**
@@ -45,17 +45,6 @@ class LazyCanvas {
45
45
  }
46
46
  return this;
47
47
  }
48
- /**
49
- * Replace base fonts with custom fonts by special file.
50
- * Use this method before loading fonts by `FontManager`.
51
- * The file should be generated by the following instructions:
52
- * @see https://github.com/NMMTY/LazyCanvas/blob/main/scripts/FontsGenerate.md
53
- * @param fonts {IFonts[]} - The `fonts` to set
54
- */
55
- setFonts(fonts) {
56
- this.fonts = new FontsManager_1.FontsManager(fonts);
57
- return this;
58
- }
59
48
  /**
60
49
  * Set the SVG export flag. This method should be called after `setExportType` method.
61
50
  * @param flag {SvgExportFlag} - The `flag` of the SVG export
@@ -0,0 +1,41 @@
1
+ import { BaseLayer } from "./BaseLayer";
2
+ import { ColorType, IBezierLayer, IBezierLayerProps, ScaleType } from "../../types";
3
+ import { Canvas, SKRSContext2D } from "@napi-rs/canvas";
4
+ export declare class BezierLayer extends BaseLayer<IBezierLayerProps> {
5
+ props: IBezierLayerProps;
6
+ constructor(props?: IBezierLayerProps);
7
+ /**
8
+ * @description Sets the control points of the bezier layer. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
9
+ * @param controlPoints {Array<{ x: ScaleType, y: ScaleType }>} - The `controlPoints` of the bezier layer.
10
+ */
11
+ setControlPoints(...controlPoints: {
12
+ x: ScaleType;
13
+ y: ScaleType;
14
+ }[]): this;
15
+ /**
16
+ * @description Sets the end point of the bezier layer. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
17
+ * @param x {ScaleType} - The end `x` of the bezier layer.
18
+ * @param y {ScaleType} - The end `y` of the bezier layer.
19
+ */
20
+ setEndPoint(x: ScaleType, y: ScaleType): this;
21
+ /**
22
+ * @description Sets the color of the layer. You can use `hex`, `rgb`, `rgba`, `hsl`, `hsla`, and `Gradient`color.
23
+ * @param color {string} - The `color` of the layer.
24
+ */
25
+ setColor(color: ColorType): this;
26
+ /**
27
+ * @description Sets the stroke of the layer.
28
+ * @param width {number} - The `width` of the stroke.
29
+ * @param cap {string} - The `cap` of the stroke.
30
+ * @param join {string} - The `join` of the stroke.
31
+ * @param dash {number[]} - The `dash` of the stroke.
32
+ * @param dashOffset {number} - The `dashOffset` of the stroke.
33
+ * @param miterLimit {number} - The `miterLimit` of the stroke.
34
+ */
35
+ setStroke(width: number, cap?: CanvasLineCap, join?: CanvasLineJoin, dash?: number[], dashOffset?: number, miterLimit?: number): this;
36
+ draw(ctx: SKRSContext2D, canvas: Canvas, debug: boolean): Promise<void>;
37
+ /**
38
+ * @returns {IBezierLayer}
39
+ */
40
+ toJSON(): IBezierLayer;
41
+ }
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BezierLayer = void 0;
4
+ const BaseLayer_1 = require("./BaseLayer");
5
+ const enum_1 = require("../../types/enum");
6
+ const utils_1 = require("../../utils/utils");
7
+ const LazyUtil_1 = require("../../utils/LazyUtil");
8
+ const Gradient_1 = require("../helpers/Gradient");
9
+ const Pattern_1 = require("../helpers/Pattern");
10
+ class BezierLayer extends BaseLayer_1.BaseLayer {
11
+ props;
12
+ constructor(props) {
13
+ super(enum_1.LayerType.BezierCurve, props);
14
+ this.props = props ? props : {};
15
+ if (!this.props.fillStyle)
16
+ this.props.fillStyle = '#000000';
17
+ this.props.centring = enum_1.Centring.None;
18
+ }
19
+ /**
20
+ * @description Sets the control points of the bezier layer. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
21
+ * @param controlPoints {Array<{ x: ScaleType, y: ScaleType }>} - The `controlPoints` of the bezier layer.
22
+ */
23
+ setControlPoints(...controlPoints) {
24
+ if (controlPoints.length !== 2)
25
+ throw new LazyUtil_1.LazyError('The control points of the layer must be provided');
26
+ this.props.controlPoints = controlPoints.flat();
27
+ return this;
28
+ }
29
+ /**
30
+ * @description Sets the end point of the bezier layer. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
31
+ * @param x {ScaleType} - The end `x` of the bezier layer.
32
+ * @param y {ScaleType} - The end `y` of the bezier layer.
33
+ */
34
+ setEndPoint(x, y) {
35
+ this.props.endPoint = { x, y };
36
+ return this;
37
+ }
38
+ /**
39
+ * @description Sets the color of the layer. You can use `hex`, `rgb`, `rgba`, `hsl`, `hsla`, and `Gradient`color.
40
+ * @param color {string} - The `color` of the layer.
41
+ */
42
+ setColor(color) {
43
+ if (!color)
44
+ throw new LazyUtil_1.LazyError('The color of the layer must be provided');
45
+ if (!(0, utils_1.isColor)(color))
46
+ throw new LazyUtil_1.LazyError('The color of the layer must be a valid color');
47
+ let fill = (0, utils_1.parseColor)(color);
48
+ if (fill instanceof Gradient_1.Gradient || fill instanceof Pattern_1.Pattern) {
49
+ this.props.fillStyle = fill;
50
+ }
51
+ else {
52
+ let arr = fill.split(':');
53
+ this.props.fillStyle = arr[0];
54
+ this.props.opacity = parseFloat(arr[1]) || 1;
55
+ }
56
+ return this;
57
+ }
58
+ /**
59
+ * @description Sets the stroke of the layer.
60
+ * @param width {number} - The `width` of the stroke.
61
+ * @param cap {string} - The `cap` of the stroke.
62
+ * @param join {string} - The `join` of the stroke.
63
+ * @param dash {number[]} - The `dash` of the stroke.
64
+ * @param dashOffset {number} - The `dashOffset` of the stroke.
65
+ * @param miterLimit {number} - The `miterLimit` of the stroke.
66
+ */
67
+ setStroke(width, cap, join, dash, dashOffset, miterLimit) {
68
+ this.props.stroke = {
69
+ width,
70
+ cap: cap || 'butt',
71
+ join: join || 'miter',
72
+ dash: dash || [],
73
+ dashOffset: dashOffset || 0,
74
+ miterLimit: miterLimit || 10,
75
+ };
76
+ return this;
77
+ }
78
+ async draw(ctx, canvas, debug) {
79
+ const xs = (0, utils_1.parseToNormal)(this.props.x, canvas);
80
+ const ys = (0, utils_1.parseToNormal)(this.props.y, canvas, { width: 0, height: 0 }, { vertical: true });
81
+ const cp1x = (0, utils_1.parseToNormal)(this.props.controlPoints[0].x, canvas);
82
+ const cp1y = (0, utils_1.parseToNormal)(this.props.controlPoints[0].y, canvas, { width: 0, height: 0 }, { vertical: true });
83
+ const cp2x = (0, utils_1.parseToNormal)(this.props.controlPoints[1].x, canvas);
84
+ const cp2y = (0, utils_1.parseToNormal)(this.props.controlPoints[1].y, canvas, { width: 0, height: 0 }, { vertical: true });
85
+ const xe = (0, utils_1.parseToNormal)(this.props.endPoint.x, canvas);
86
+ const ye = (0, utils_1.parseToNormal)(this.props.endPoint.y, canvas, { width: 0, height: 0 }, { vertical: true });
87
+ const { max, min, center, width, height } = (0, utils_1.getBoundingBoxBezier)([{ x: xs, y: ys }, { x: cp1x, y: cp1y }, { x: cp2x, y: cp2y }, { x: xe, y: ye }]);
88
+ let fillStyle = await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle);
89
+ if (debug)
90
+ LazyUtil_1.LazyLog.log('none', `BezierLayer:`, { xs, ys, cp1x, cp1y, cp2x, cp2y, xe, ye, max, min, center, width, height, fillStyle });
91
+ ctx.save();
92
+ (0, utils_1.transform)(ctx, this.props.transform, { x: center.x, y: center.y, width, height, type: this.type });
93
+ (0, utils_1.drawShadow)(ctx, this.props.shadow);
94
+ (0, utils_1.opacity)(ctx, this.props.opacity);
95
+ (0, utils_1.filters)(ctx, this.props.filter);
96
+ ctx.beginPath();
97
+ ctx.moveTo(xs, ys);
98
+ ctx.strokeStyle = fillStyle;
99
+ ctx.lineWidth = this.props.stroke?.width || 1;
100
+ ctx.lineCap = this.props.stroke?.cap || 'butt';
101
+ ctx.lineJoin = this.props.stroke?.join || 'miter';
102
+ ctx.miterLimit = this.props.stroke?.miterLimit || 10;
103
+ ctx.lineDashOffset = this.props.stroke?.dashOffset || 0;
104
+ ctx.setLineDash(this.props.stroke?.dash || []);
105
+ ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, xe, ye);
106
+ ctx.stroke();
107
+ ctx.closePath();
108
+ ctx.restore();
109
+ }
110
+ /**
111
+ * @returns {IBezierLayer}
112
+ */
113
+ toJSON() {
114
+ let data = super.toJSON();
115
+ data.props = this.props;
116
+ return { ...data };
117
+ }
118
+ }
119
+ exports.BezierLayer = BezierLayer;
@@ -47,4 +47,5 @@ export declare class Group implements IGroup {
47
47
  * Get the length of the components
48
48
  */
49
49
  get length(): number;
50
+ toJSON(): IGroup;
50
51
  }
@@ -83,5 +83,13 @@ class Group {
83
83
  get length() {
84
84
  return this.components.length;
85
85
  }
86
+ toJSON() {
87
+ return {
88
+ id: this.id,
89
+ visible: this.visible,
90
+ zIndex: this.zIndex,
91
+ components: this.components.map(c => c.toJSON())
92
+ };
93
+ }
86
94
  }
87
95
  exports.Group = Group;
@@ -16,7 +16,7 @@ export declare class ImageLayer extends BaseLayer<IImageLayerProps> {
16
16
  * @param radius {ScaleType} - The `radius` of the image. (optional)
17
17
  */
18
18
  setSize(width: ScaleType, height: ScaleType, radius?: ScaleType): this;
19
- draw(ctx: SKRSContext2D, canvas: Canvas): Promise<void>;
19
+ draw(ctx: SKRSContext2D, canvas: Canvas, debug: boolean): Promise<void>;
20
20
  /**
21
21
  * @returns {IImageLayer}
22
22
  */
@@ -61,13 +61,15 @@ class ImageLayer extends BaseLayer_1.BaseLayer {
61
61
  };
62
62
  return this;
63
63
  }
64
- async draw(ctx, canvas) {
64
+ async draw(ctx, canvas, debug) {
65
65
  let xs = (0, utils_1.parseToNormal)(this.props.x, canvas);
66
66
  let ys = (0, utils_1.parseToNormal)(this.props.y, canvas, { width: 0, height: 0 }, { vertical: true });
67
67
  let w = (0, utils_1.parseToNormal)(this.props.size.width, canvas);
68
68
  let h = (0, utils_1.parseToNormal)(this.props.size.height, canvas, { width: w, height: 0 }, { vertical: true });
69
69
  let r = (0, utils_1.parseToNormal)(this.props.size.radius, canvas, { width: w, height: h }, { layer: true });
70
70
  let { x, y } = (0, utils_1.centring)(this.props.centring, this.type, w, h, xs, ys);
71
+ if (debug)
72
+ LazyUtil_1.LazyLog.log('none', `ImageLayer:`, { x, y, w, h, r });
71
73
  ctx.save();
72
74
  (0, utils_1.transform)(ctx, this.props.transform, { width: w, height: h, x, y, type: this.type });
73
75
  (0, utils_1.drawShadow)(ctx, this.props.shadow);
@@ -31,7 +31,7 @@ export declare class MorphLayer extends BaseLayer<IMorphLayerProps> {
31
31
  * @param filled {boolean} - The `filled` of the layer.
32
32
  */
33
33
  setFilled(filled: boolean): this;
34
- draw(ctx: SKRSContext2D, canvas: Canvas): Promise<void>;
34
+ draw(ctx: SKRSContext2D, canvas: Canvas, debug: boolean): Promise<void>;
35
35
  /**
36
36
  * @returns {IMorphLayer}
37
37
  */
@@ -80,13 +80,16 @@ class MorphLayer extends BaseLayer_1.BaseLayer {
80
80
  this.props.filled = filled;
81
81
  return this;
82
82
  }
83
- async draw(ctx, canvas) {
83
+ async draw(ctx, canvas, debug) {
84
84
  const xs = (0, utils_1.parseToNormal)(this.props.x, canvas);
85
85
  const ys = (0, utils_1.parseToNormal)(this.props.y, canvas, { width: 0, height: 0 }, { vertical: true });
86
86
  const w = (0, utils_1.parseToNormal)(this.props.size.width, canvas);
87
87
  const h = (0, utils_1.parseToNormal)(this.props.size.height, canvas, { width: w, height: 0 }, { vertical: true });
88
- const r = (0, utils_1.parseToNormal)(this.props.size.radius, canvas, { width: w, height: h }, { layer: true });
88
+ const r = (0, utils_1.parseToNormal)(this.props.size.radius, canvas, { width: w / 2, height: h / 2 }, { layer: true });
89
89
  let { x, y } = (0, utils_1.centring)(this.props.centring, this.type, w, h, xs, ys);
90
+ let fillStyle = await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle);
91
+ if (debug)
92
+ LazyUtil_1.LazyLog.log('none', `MorphLayer:`, { x, y, w, h, r });
90
93
  ctx.save();
91
94
  (0, utils_1.transform)(ctx, this.props.transform, { width: w, height: h, x, y, type: this.type });
92
95
  ctx.beginPath();
@@ -104,14 +107,6 @@ class MorphLayer extends BaseLayer_1.BaseLayer {
104
107
  (0, utils_1.drawShadow)(ctx, this.props.shadow);
105
108
  (0, utils_1.opacity)(ctx, this.props.opacity);
106
109
  (0, utils_1.filters)(ctx, this.props.filter);
107
- let base = (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle);
108
- let fillStyle;
109
- if (base instanceof Promise) {
110
- fillStyle = await base;
111
- }
112
- else {
113
- fillStyle = base;
114
- }
115
110
  if (this.props.filled) {
116
111
  ctx.fillStyle = fillStyle;
117
112
  ctx.fill();
@@ -0,0 +1,39 @@
1
+ import { BaseLayer } from "./BaseLayer";
2
+ import { IQuadraticLayerProps, IQuadraticLayer, ColorType, ScaleType } from "../../types";
3
+ import { Canvas, SKRSContext2D } from "@napi-rs/canvas";
4
+ export declare class QuadraticLayer extends BaseLayer<IQuadraticLayerProps> {
5
+ props: IQuadraticLayerProps;
6
+ constructor(props?: IQuadraticLayerProps);
7
+ /**
8
+ * @description Sets the control point of the quadratic layer. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
9
+ * @param x {ScaleType} - The control `x` of the quadratic layer.
10
+ * @param y {ScaleType} - The control `y` of the quadratic layer.
11
+ */
12
+ setControlPoint(x: ScaleType, y: ScaleType): this;
13
+ /**
14
+ * @description Sets the end point of the quadratic layer. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
15
+ * @param x {ScaleType} - The end `x` of the quadratic layer.
16
+ * @param y {ScaleType} - The end `y` of the quadratic layer.
17
+ */
18
+ setEndPoint(x: ScaleType, y: ScaleType): this;
19
+ /**
20
+ * @description Sets the color of the layer. You can use `hex`, `rgb`, `rgba`, `hsl`, `hsla`, and `Gradient`color.
21
+ * @param color {string} - The `color` of the layer.
22
+ */
23
+ setColor(color: ColorType): this;
24
+ /**
25
+ * @description Sets the stroke of the layer.
26
+ * @param width {number} - The `width` of the stroke.
27
+ * @param cap {string} - The `cap` of the stroke.
28
+ * @param join {string} - The `join` of the stroke.
29
+ * @param dash {number[]} - The `dash` of the stroke.
30
+ * @param dashOffset {number} - The `dashOffset` of the stroke.
31
+ * @param miterLimit {number} - The `miterLimit` of the stroke.
32
+ */
33
+ setStroke(width: number, cap?: CanvasLineCap, join?: CanvasLineJoin, dash?: number[], dashOffset?: number, miterLimit?: number): this;
34
+ draw(ctx: SKRSContext2D, canvas: Canvas, debug: boolean): Promise<void>;
35
+ /**
36
+ * @returns {IQuadraticLayer}
37
+ */
38
+ toJSON(): IQuadraticLayer;
39
+ }
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.QuadraticLayer = void 0;
4
+ const BaseLayer_1 = require("./BaseLayer");
5
+ const enum_1 = require("../../types/enum");
6
+ const utils_1 = require("../../utils/utils");
7
+ const LazyUtil_1 = require("../../utils/LazyUtil");
8
+ const Gradient_1 = require("../helpers/Gradient");
9
+ const Pattern_1 = require("../helpers/Pattern");
10
+ class QuadraticLayer extends BaseLayer_1.BaseLayer {
11
+ props;
12
+ constructor(props) {
13
+ super(enum_1.LayerType.QuadraticCurve, props);
14
+ this.props = props ? props : {};
15
+ if (!this.props.fillStyle)
16
+ this.props.fillStyle = '#000000';
17
+ this.props.centring = enum_1.Centring.None;
18
+ }
19
+ /**
20
+ * @description Sets the control point of the quadratic layer. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
21
+ * @param x {ScaleType} - The control `x` of the quadratic layer.
22
+ * @param y {ScaleType} - The control `y` of the quadratic layer.
23
+ */
24
+ setControlPoint(x, y) {
25
+ this.props.controlPoint = { x, y };
26
+ return this;
27
+ }
28
+ /**
29
+ * @description Sets the end point of the quadratic layer. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
30
+ * @param x {ScaleType} - The end `x` of the quadratic layer.
31
+ * @param y {ScaleType} - The end `y` of the quadratic layer.
32
+ */
33
+ setEndPoint(x, y) {
34
+ this.props.endPoint = { x, y };
35
+ return this;
36
+ }
37
+ /**
38
+ * @description Sets the color of the layer. You can use `hex`, `rgb`, `rgba`, `hsl`, `hsla`, and `Gradient`color.
39
+ * @param color {string} - The `color` of the layer.
40
+ */
41
+ setColor(color) {
42
+ if (!color)
43
+ throw new LazyUtil_1.LazyError('The color of the layer must be provided');
44
+ if (!(0, utils_1.isColor)(color))
45
+ throw new LazyUtil_1.LazyError('The color of the layer must be a valid color');
46
+ let fill = (0, utils_1.parseColor)(color);
47
+ if (fill instanceof Gradient_1.Gradient || fill instanceof Pattern_1.Pattern) {
48
+ this.props.fillStyle = fill;
49
+ }
50
+ else {
51
+ let arr = fill.split(':');
52
+ this.props.fillStyle = arr[0];
53
+ this.props.opacity = parseFloat(arr[1]) || 1;
54
+ }
55
+ return this;
56
+ }
57
+ /**
58
+ * @description Sets the stroke of the layer.
59
+ * @param width {number} - The `width` of the stroke.
60
+ * @param cap {string} - The `cap` of the stroke.
61
+ * @param join {string} - The `join` of the stroke.
62
+ * @param dash {number[]} - The `dash` of the stroke.
63
+ * @param dashOffset {number} - The `dashOffset` of the stroke.
64
+ * @param miterLimit {number} - The `miterLimit` of the stroke.
65
+ */
66
+ setStroke(width, cap, join, dash, dashOffset, miterLimit) {
67
+ this.props.stroke = {
68
+ width,
69
+ cap: cap || 'butt',
70
+ join: join || 'miter',
71
+ dash: dash || [],
72
+ dashOffset: dashOffset || 0,
73
+ miterLimit: miterLimit || 10,
74
+ };
75
+ return this;
76
+ }
77
+ async draw(ctx, canvas, debug) {
78
+ const xs = (0, utils_1.parseToNormal)(this.props.x, canvas);
79
+ const ys = (0, utils_1.parseToNormal)(this.props.y, canvas, { width: 0, height: 0 }, { vertical: true });
80
+ const cx = (0, utils_1.parseToNormal)(this.props.controlPoint.x, canvas);
81
+ const cy = (0, utils_1.parseToNormal)(this.props.controlPoint.y, canvas, { width: 0, height: 0 }, { vertical: true });
82
+ const xe = (0, utils_1.parseToNormal)(this.props.endPoint.x, canvas);
83
+ const ye = (0, utils_1.parseToNormal)(this.props.endPoint.y, canvas, { width: 0, height: 0 }, { vertical: true });
84
+ const { max, min, center, width, height } = (0, utils_1.getBoundingBoxBezier)([{ x: xs, y: ys }, { x: cx, y: cy }, { x: xe, y: ye }]);
85
+ let fillStyle = await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle);
86
+ if (debug)
87
+ LazyUtil_1.LazyLog.log('none', `BezierLayer:`, { xs, ys, cx, cy, xe, ye, max, min, center, width, height, fillStyle });
88
+ ctx.save();
89
+ (0, utils_1.transform)(ctx, this.props.transform, { x: center.x, y: center.y, width, height, type: this.type });
90
+ (0, utils_1.drawShadow)(ctx, this.props.shadow);
91
+ (0, utils_1.opacity)(ctx, this.props.opacity);
92
+ (0, utils_1.filters)(ctx, this.props.filter);
93
+ ctx.beginPath();
94
+ ctx.moveTo(xs, ys);
95
+ ctx.strokeStyle = fillStyle;
96
+ ctx.lineWidth = this.props.stroke?.width || 1;
97
+ ctx.lineCap = this.props.stroke?.cap || 'butt';
98
+ ctx.lineJoin = this.props.stroke?.join || 'miter';
99
+ ctx.miterLimit = this.props.stroke?.miterLimit || 10;
100
+ ctx.lineDashOffset = this.props.stroke?.dashOffset || 0;
101
+ ctx.setLineDash(this.props.stroke?.dash || []);
102
+ ctx.quadraticCurveTo(cx, cy, xe, ye);
103
+ ctx.stroke();
104
+ ctx.closePath();
105
+ ctx.restore();
106
+ }
107
+ /**
108
+ * @returns {IQuadraticLayer}
109
+ */
110
+ toJSON() {
111
+ let data = super.toJSON();
112
+ data.props = this.props;
113
+ return { ...data };
114
+ }
115
+ }
116
+ exports.QuadraticLayer = QuadraticLayer;
@@ -60,7 +60,7 @@ export declare class TextLayer extends BaseLayer<ITextLayerProps> {
60
60
  * @param filled {boolean} - The `filled` of the layer.
61
61
  */
62
62
  setFilled(filled: boolean): this;
63
- draw(ctx: SKRSContext2D, canvas: Canvas): Promise<void>;
63
+ draw(ctx: SKRSContext2D, canvas: Canvas, debug: boolean): Promise<void>;
64
64
  private drawText;
65
65
  /**
66
66
  * @returns {ITextLayer}
@@ -144,13 +144,14 @@ class TextLayer extends BaseLayer_1.BaseLayer {
144
144
  this.props.filled = filled;
145
145
  return this;
146
146
  }
147
- async draw(ctx, canvas) {
147
+ async draw(ctx, canvas, debug) {
148
148
  const x = (0, utils_1.parseToNormal)(this.props.x, canvas);
149
149
  const y = (0, utils_1.parseToNormal)(this.props.y, canvas, { width: 0, height: 0 }, { vertical: true });
150
150
  const w = (0, utils_1.parseToNormal)(this.props.multiline?.width, canvas);
151
151
  const h = (0, utils_1.parseToNormal)(this.props.multiline?.height, canvas, { width: w, height: 0 }, { vertical: true });
152
+ if (debug)
153
+ LazyUtil_1.LazyLog.log('none', `TextLayer:`, { x, y, w, h });
152
154
  ctx.save();
153
- console.log(this.props);
154
155
  (0, utils_1.transform)(ctx, this.props.transform, { width: w, height: h, x, y, type: this.type }, { text: this.props.text, textAlign: this.props.align, fontSize: this.props.font.size, multiline: this.props.multiline.enabled });
155
156
  ctx.beginPath();
156
157
  (0, utils_1.drawShadow)(ctx, this.props.shadow);
@@ -58,7 +58,8 @@ class Font {
58
58
  return {
59
59
  family: this.family,
60
60
  weight: this.weight,
61
- path: this.path
61
+ path: this.path,
62
+ base64: this.base64
62
63
  };
63
64
  }
64
65
  }
@@ -2,7 +2,16 @@ import { IFontsManager, IFonts } from "../../types";
2
2
  import { Font } from "../helpers/Font";
3
3
  export declare class FontsManager implements IFontsManager {
4
4
  map: Map<string, Font>;
5
- constructor(fonts?: IFonts);
5
+ debug: boolean;
6
+ constructor(debug?: boolean);
7
+ /**
8
+ * Replace base fonts with custom fonts by special file.
9
+ * Use this method before loading fonts by `FontManager`.
10
+ * The file should be generated by the following instructions:
11
+ * @see https://github.com/NMMTY/LazyCanvas/blob/main/scripts/FontsGenerate.md
12
+ * @param fontList {IFonts[]} - The `fonts` to set
13
+ */
14
+ loadFonts(fontList: IFonts): this;
6
15
  /**
7
16
  * Add a font to the map
8
17
  * @param fonts {Font[]} - The `font` to add to the map
@@ -7,9 +7,21 @@ const Fonts_1 = require("../../helpers/Fonts");
7
7
  const canvas_1 = require("@napi-rs/canvas");
8
8
  class FontsManager {
9
9
  map;
10
- constructor(fonts) {
10
+ debug;
11
+ constructor(debug = false) {
12
+ this.debug = debug;
11
13
  this.map = new Map();
12
- let fontList = fonts || Fonts_1.Fonts;
14
+ let fontList = Fonts_1.Fonts;
15
+ this.loadFonts(fontList);
16
+ }
17
+ /**
18
+ * Replace base fonts with custom fonts by special file.
19
+ * Use this method before loading fonts by `FontManager`.
20
+ * The file should be generated by the following instructions:
21
+ * @see https://github.com/NMMTY/LazyCanvas/blob/main/scripts/FontsGenerate.md
22
+ * @param fontList {IFonts[]} - The `fonts` to set
23
+ */
24
+ loadFonts(fontList) {
13
25
  for (const fontFamily in fontList) {
14
26
  if (fontList.hasOwnProperty(fontFamily)) {
15
27
  for (const weight in fontList[fontFamily]) {
@@ -23,13 +35,18 @@ class FontsManager {
23
35
  }
24
36
  }
25
37
  }
38
+ return this;
26
39
  }
27
40
  /**
28
41
  * Add a font to the map
29
42
  * @param fonts {Font[]} - The `font` to add to the map
30
43
  */
31
44
  add(...fonts) {
45
+ if (this.debug)
46
+ LazyUtil_1.LazyLog.log('info', `Adding fonts...\nlength: ${fonts.length}`);
32
47
  for (const font of fonts) {
48
+ if (this.debug)
49
+ LazyUtil_1.LazyLog.log('none', `Data:`, font.toJSON());
33
50
  if (!font.family)
34
51
  throw new LazyUtil_1.LazyError("Family must be provided");
35
52
  if (!font.weight)
@@ -3,7 +3,8 @@ import { ILayersManager } from "../../types";
3
3
  import { Group } from "../components/Group";
4
4
  export declare class LayersManager implements ILayersManager {
5
5
  map: Map<string, AnyLayer | Group>;
6
- constructor();
6
+ debug: boolean;
7
+ constructor(debug?: boolean);
7
8
  /**
8
9
  * Add a layer to the map
9
10
  * @param layers {AnyLayer[] | Group[]} - The `layer` or `group` to add to the map
@@ -1,21 +1,28 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LayersManager = void 0;
4
+ const LazyUtil_1 = require("../../utils/LazyUtil");
4
5
  class LayersManager {
5
6
  map;
6
- constructor() {
7
+ debug;
8
+ constructor(debug = false) {
7
9
  this.map = new Map();
10
+ this.debug = debug;
8
11
  }
9
12
  /**
10
13
  * Add a layer to the map
11
14
  * @param layers {AnyLayer[] | Group[]} - The `layer` or `group` to add to the map
12
15
  */
13
16
  add(...layers) {
17
+ if (this.debug)
18
+ LazyUtil_1.LazyLog.log('info', `Adding layers...\nlength: ${layers.length}`);
14
19
  let layersArray = layers.flat();
15
20
  layersArray = layersArray.filter(l => l !== undefined);
16
21
  for (const layer of layersArray) {
22
+ if (this.debug)
23
+ LazyUtil_1.LazyLog.log('none', `Data:`, layer.toJSON());
17
24
  if (this.map.has(layer.id))
18
- throw new Error("Layer already exists");
25
+ throw new LazyUtil_1.LazyError("Layer already exists");
19
26
  this.map.set(layer.id, layer);
20
27
  }
21
28
  this.sort();
@@ -5,7 +5,8 @@ import { LazyCanvas } from "../LazyCanvas";
5
5
  import { SKRSContext2D } from "@napi-rs/canvas";
6
6
  export declare class RenderManager implements IRenderManager {
7
7
  lazyCanvas: LazyCanvas;
8
- constructor(lazyCanvas: LazyCanvas);
8
+ debug: boolean;
9
+ constructor(lazyCanvas: LazyCanvas, debug?: boolean);
9
10
  /**
10
11
  * This will render all the layers and return the rendered canvas buffer or ctx.
11
12
  * @returns {Promise<Buffer | SKRSContext2D>}
@@ -3,10 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RenderManager = void 0;
4
4
  const enum_1 = require("../../types/enum");
5
5
  const Group_1 = require("../components/Group");
6
+ const LazyUtil_1 = require("../../utils/LazyUtil");
6
7
  class RenderManager {
7
8
  lazyCanvas;
8
- constructor(lazyCanvas) {
9
+ debug;
10
+ constructor(lazyCanvas, debug = false) {
9
11
  this.lazyCanvas = lazyCanvas;
12
+ this.debug = debug;
10
13
  }
11
14
  /**
12
15
  * This will render all the layers and return the rendered canvas buffer or ctx.
@@ -14,16 +17,18 @@ class RenderManager {
14
17
  */
15
18
  async render() {
16
19
  for (const layer of this.lazyCanvas.layers.toArray()) {
20
+ if (this.debug)
21
+ LazyUtil_1.LazyLog.log('info', `Rendering ${layer.id}...\nData:`, layer.toJSON());
17
22
  if (layer.visible) {
18
23
  if (layer instanceof Group_1.Group) {
19
24
  for (const subLayer of layer.components) {
20
25
  if (subLayer.visible) {
21
- await subLayer.draw(this.lazyCanvas.ctx, this.lazyCanvas.canvas);
26
+ await subLayer.draw(this.lazyCanvas.ctx, this.lazyCanvas.canvas, this.debug);
22
27
  }
23
28
  }
24
29
  }
25
30
  else {
26
- await layer.draw(this.lazyCanvas.ctx, this.lazyCanvas.canvas);
31
+ await layer.draw(this.lazyCanvas.ctx, this.lazyCanvas.canvas, this.debug);
27
32
  }
28
33
  this.lazyCanvas.ctx.shadowColor = 'transparent';
29
34
  }
@@ -17,12 +17,6 @@ export interface IBaseLayerProps {
17
17
  opacity: number;
18
18
  filled: boolean;
19
19
  fillStyle: ColorType;
20
- shadow: {
21
- color: string;
22
- blur: number;
23
- offsetX: number;
24
- offsetY: number;
25
- };
26
20
  stroke: {
27
21
  width: number;
28
22
  cap: CanvasLineCap;
@@ -31,6 +25,12 @@ export interface IBaseLayerProps {
31
25
  dash: number[];
32
26
  miterLimit: number;
33
27
  };
28
+ shadow: {
29
+ color: string;
30
+ blur: number;
31
+ offsetX: number;
32
+ offsetY: number;
33
+ };
34
34
  transform: Transform;
35
35
  }
36
36
 
@@ -0,0 +1,11 @@
1
+ import { IBaseLayer, IBaseLayerProps } from "./BaseLayer";
2
+ import { Point } from "../";
3
+
4
+ export interface IBezierLayer extends IBaseLayer {
5
+ props: IBezierLayerProps;
6
+ }
7
+
8
+ export interface IBezierLayerProps extends IBaseLayerProps {
9
+ controlPoints: Array<Point>;
10
+ endPoint: Point;
11
+ }
@@ -1,5 +1,5 @@
1
1
  import { IBaseLayer, IBaseLayerProps } from "./BaseLayer";
2
- import { ScaleType } from "../";
2
+ import {ColorType, ScaleType} from "../";
3
3
 
4
4
  export interface IMorphLayer extends IBaseLayer {
5
5
  props: IMorphLayerProps;
@@ -0,0 +1,11 @@
1
+ import { IBaseLayer, IBaseLayerProps } from "./BaseLayer";
2
+ import { Point } from "../";
3
+
4
+ export interface IQuadraticLayer extends IBaseLayer {
5
+ props: IQuadraticLayerProps;
6
+ }
7
+
8
+ export interface IQuadraticLayerProps extends IBaseLayerProps {
9
+ controlPoint: Point;
10
+ endPoint: Point;
11
+ }
@@ -19,7 +19,6 @@ export interface ITextLayerProps extends IBaseLayerProps {
19
19
  height: ScaleType;
20
20
  spacing?: number;
21
21
  };
22
- color: ColorType;
23
22
  align: TextAlign;
24
23
  baseline: TextBaseline;
25
24
  direction: TextDirection;
@@ -78,7 +78,15 @@ export declare enum SaveFormat {
78
78
  }
79
79
  export declare enum Centring {
80
80
  Start = "start",
81
- Center = "center"
81
+ StartTop = "start-top",
82
+ StartBottom = "start-bottom",
83
+ Center = "center",
84
+ CenterTop = "center-top",
85
+ CenterBottom = "center-bottom",
86
+ End = "end",
87
+ EndTop = "end-top",
88
+ EndBottom = "end-bottom",
89
+ None = "none"
82
90
  }
83
91
  export declare enum PatternType {
84
92
  Repeat = "repeat",
@@ -93,7 +93,15 @@ var SaveFormat;
93
93
  var Centring;
94
94
  (function (Centring) {
95
95
  Centring["Start"] = "start";
96
+ Centring["StartTop"] = "start-top";
97
+ Centring["StartBottom"] = "start-bottom";
96
98
  Centring["Center"] = "center";
99
+ Centring["CenterTop"] = "center-top";
100
+ Centring["CenterBottom"] = "center-bottom";
101
+ Centring["End"] = "end";
102
+ Centring["EndTop"] = "end-top";
103
+ Centring["EndBottom"] = "end-bottom";
104
+ Centring["None"] = "none";
97
105
  })(Centring || (exports.Centring = Centring = {}));
98
106
  var PatternType;
99
107
  (function (PatternType) {
@@ -3,6 +3,8 @@ export * from "./components/BaseLayer";
3
3
  export * from "./components/TextLayer";
4
4
  export * from "./components/ImageLayer";
5
5
  export * from "./components/MorphLayer";
6
+ export * from "./components/BezierLayer";
7
+ export * from "./components/QuadraticLayer";
6
8
  export * from "./components/Group";
7
9
  export * from "./helpers/Font";
8
10
  export * from "./helpers/Gradient";
@@ -2,4 +2,5 @@ import { Font } from "../../structures/helpers/Font";
2
2
 
3
3
  export interface IFontsManager {
4
4
  map: Map<string, Font>;
5
+ debug: boolean;
5
6
  }
@@ -3,4 +3,5 @@ import { Group } from "../../structures/components/Group";
3
3
 
4
4
  export interface ILayersManager {
5
5
  map: Map<string, AnyLayer | Group>;
6
+ debug: boolean;
6
7
  }
@@ -2,4 +2,5 @@ import { LazyCanvas } from "../../structures/LazyCanvas";
2
2
 
3
3
  export interface IRenderManager {
4
4
  lazyCanvas: LazyCanvas;
5
+ debug: boolean;
5
6
  }
@@ -3,10 +3,22 @@ import { Pattern } from "../structures/helpers/Pattern";
3
3
  import { MorphLayer } from "../structures/components/MorphLayer";
4
4
  import { ImageLayer } from "../structures/components/ImageLayer";
5
5
  import { TextLayer } from "../structures/components/TextLayer";
6
+ import { BezierLayer } from "../structures/components/BezierLayer";
7
+ import { QuadraticLayer } from "../structures/components/QuadraticLayer";
6
8
  import { Group } from "../structures/components/Group";
7
9
 
8
- export type ScaleType = string | number;
10
+ export type ScaleType = string | number | 'vw' | 'vh' | 'vmin' | 'vmax';
9
11
 
10
12
  export type ColorType = string | Gradient | Pattern;
11
13
 
12
- export type AnyLayer = MorphLayer | ImageLayer | TextLayer | Group;
14
+ export type AnyLayer = MorphLayer | ImageLayer | TextLayer | BezierLayer | QuadraticLayer | Group;
15
+
16
+ export type Point = {
17
+ x: ScaleType;
18
+ y: ScaleType;
19
+ }
20
+
21
+ export type PointNumber = {
22
+ x: number;
23
+ y: number;
24
+ };
@@ -3,5 +3,5 @@ export declare class LazyError extends Error {
3
3
  constructor(message: string);
4
4
  }
5
5
  export declare class LazyLog {
6
- static log(message: string, type?: string): void;
6
+ static log(type?: string, ...message: any): void;
7
7
  }
@@ -10,16 +10,16 @@ class LazyError extends Error {
10
10
  }
11
11
  exports.LazyError = LazyError;
12
12
  class LazyLog {
13
- static log(message, type = "info") {
13
+ static log(type = "none", ...message) {
14
14
  switch (type) {
15
15
  case "info":
16
- console.log("[LazyCanvas] [INFO] " + message);
16
+ console.log("[LazyCanvas] [INFO] ", ...message);
17
17
  break;
18
18
  case "warn":
19
- console.warn("[LazyCanvas] [WARN] " + message);
19
+ console.warn("[LazyCanvas] [WARN] ", ...message);
20
20
  break;
21
21
  default:
22
- console.log("[LazyCanvas] [INFO] " + message);
22
+ console.log(...message);
23
23
  break;
24
24
  }
25
25
  }
@@ -1,5 +1,5 @@
1
1
  import { Centring, LayerType, SaveFormat, TextAlign } from "../types/enum";
2
- import { Transform, ScaleType, ColorType } from "../types";
2
+ import { Transform, ScaleType, ColorType, PointNumber } from "../types";
3
3
  import { Gradient } from "../structures/helpers/Gradient";
4
4
  import { Canvas, SKRSContext2D } from "@napi-rs/canvas";
5
5
  import { Pattern } from "../structures/helpers/Pattern";
@@ -37,3 +37,19 @@ export declare function centring(centring: Centring, type: LayerType, width: num
37
37
  x: number;
38
38
  y: number;
39
39
  };
40
+ export declare function getBoundingBoxBezier(points: PointNumber[], steps?: number): {
41
+ min: {
42
+ x: number;
43
+ y: number;
44
+ };
45
+ max: {
46
+ x: number;
47
+ y: number;
48
+ };
49
+ center: {
50
+ x: number;
51
+ y: number;
52
+ };
53
+ width: number;
54
+ height: number;
55
+ };
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.centring = exports.isImageUrlValid = exports.generateRandomName = exports.saveFile = exports.transform = exports.parseFillStyle = exports.filters = exports.opacity = exports.drawShadow = exports.parseToNormal = exports.parseColor = exports.parseHex = exports.isColor = exports.generateID = void 0;
26
+ exports.getBoundingBoxBezier = exports.centring = exports.isImageUrlValid = exports.generateRandomName = exports.saveFile = exports.transform = exports.parseFillStyle = exports.filters = exports.opacity = exports.drawShadow = exports.parseToNormal = exports.parseColor = exports.parseHex = exports.isColor = exports.generateID = void 0;
27
27
  const enum_1 = require("../types/enum");
28
28
  const Gradient_1 = require("../structures/helpers/Gradient");
29
29
  const LazyUtil_1 = require("./LazyUtil");
@@ -180,6 +180,8 @@ function transform(ctx, transform, layer = { width: 0, height: 0, x: 0, y: 0, ty
180
180
  switch (layer.type) {
181
181
  case enum_1.LayerType.Image:
182
182
  case enum_1.LayerType.Morph:
183
+ case enum_1.LayerType.BezierCurve:
184
+ case enum_1.LayerType.QuadraticCurve:
183
185
  ctx.translate(layer.x + (layer.width / 2), layer.y + (layer.height / 2));
184
186
  ctx.rotate((Math.PI / 180) * transform.rotate);
185
187
  ctx.translate(-(layer.x + (layer.width / 2)), -(layer.y + (layer.height / 2)));
@@ -244,18 +246,111 @@ function isImageUrlValid(src) {
244
246
  }
245
247
  exports.isImageUrlValid = isImageUrlValid;
246
248
  function centring(centring, type, width, height, x, y) {
247
- if (centring === enum_1.Centring.Center) {
248
- switch (type) {
249
- case enum_1.LayerType.Image:
250
- case enum_1.LayerType.Morph:
251
- x -= width / 2;
252
- y -= height / 2;
253
- break;
254
- }
255
- return { x, y };
256
- }
257
- else {
258
- return { x, y };
249
+ switch (centring) {
250
+ case enum_1.Centring.Center:
251
+ switch (type) {
252
+ case enum_1.LayerType.Image:
253
+ case enum_1.LayerType.Morph:
254
+ x -= width / 2;
255
+ y -= height / 2;
256
+ break;
257
+ }
258
+ return { x, y };
259
+ case enum_1.Centring.CenterBottom:
260
+ switch (type) {
261
+ case enum_1.LayerType.Image:
262
+ case enum_1.LayerType.Morph:
263
+ x -= width / 2;
264
+ break;
265
+ }
266
+ return { x, y };
267
+ case enum_1.Centring.CenterTop:
268
+ switch (type) {
269
+ case enum_1.LayerType.Image:
270
+ case enum_1.LayerType.Morph:
271
+ x -= width / 2;
272
+ y -= height;
273
+ break;
274
+ }
275
+ return { x, y };
276
+ case enum_1.Centring.Start:
277
+ switch (type) {
278
+ case enum_1.LayerType.Image:
279
+ case enum_1.LayerType.Morph:
280
+ y -= height / 2;
281
+ break;
282
+ }
283
+ return { x, y };
284
+ case enum_1.Centring.StartBottom:
285
+ return { x, y };
286
+ case enum_1.Centring.StartTop:
287
+ switch (type) {
288
+ case enum_1.LayerType.Image:
289
+ case enum_1.LayerType.Morph:
290
+ y -= height;
291
+ break;
292
+ }
293
+ return { x, y };
294
+ case enum_1.Centring.End:
295
+ switch (type) {
296
+ case enum_1.LayerType.Image:
297
+ case enum_1.LayerType.Morph:
298
+ x -= width;
299
+ y -= height / 2;
300
+ break;
301
+ }
302
+ return { x, y };
303
+ case enum_1.Centring.EndBottom:
304
+ switch (type) {
305
+ case enum_1.LayerType.Image:
306
+ case enum_1.LayerType.Morph:
307
+ x -= width;
308
+ break;
309
+ }
310
+ return { x, y };
311
+ case enum_1.Centring.EndTop:
312
+ switch (type) {
313
+ case enum_1.LayerType.Image:
314
+ case enum_1.LayerType.Morph:
315
+ x -= width;
316
+ y -= height;
317
+ break;
318
+ }
319
+ return { x, y };
320
+ case enum_1.Centring.None:
321
+ return { x, y };
259
322
  }
260
323
  }
261
324
  exports.centring = centring;
325
+ function quadraticBezier(p0, p1, p2, t) {
326
+ const x = (1 - t) ** 2 * p0.x + 2 * (1 - t) * t * p1.x + t ** 2 * p2.x;
327
+ const y = (1 - t) ** 2 * p0.y + 2 * (1 - t) * t * p1.y + t ** 2 * p2.y;
328
+ return { x, y };
329
+ }
330
+ function cubicBezier(p0, p1, p2, p3, t) {
331
+ const x = (1 - t) ** 3 * p0.x + 3 * (1 - t) ** 2 * t * p1.x + 3 * (1 - t) * t ** 2 * p2.x + t ** 3 * p3.x;
332
+ const y = (1 - t) ** 3 * p0.y + 3 * (1 - t) ** 2 * t * p1.y + 3 * (1 - t) * t ** 2 * p2.y + t ** 3 * p3.y;
333
+ return { x, y };
334
+ }
335
+ function getBoundingBoxBezier(points, steps = 100) {
336
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
337
+ for (let i = 0; i <= steps; i++) {
338
+ const t = i / steps;
339
+ let p;
340
+ if (points.length === 3) {
341
+ p = quadraticBezier(points[0], points[1], points[2], t);
342
+ }
343
+ else if (points.length === 4) {
344
+ p = cubicBezier(points[0], points[1], points[2], points[3], t);
345
+ }
346
+ else {
347
+ throw new LazyUtil_1.LazyError("Invalid number of points");
348
+ }
349
+ minX = Math.min(minX, p.x);
350
+ minY = Math.min(minY, p.y);
351
+ maxX = Math.max(maxX, p.x);
352
+ maxY = Math.max(maxY, p.y);
353
+ }
354
+ 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 };
355
+ }
356
+ exports.getBoundingBoxBezier = getBoundingBoxBezier;
package/package.json CHANGED
@@ -1,51 +1,49 @@
1
- {
2
- "name": "@nmmty/lazycanvas",
3
- "version": "0.2.0",
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
- "logo": "tsc ./test/logo.ts && node ./test/logo.js",
10
- "text": "tsc ./test/text.ts && node ./test/text.js",
11
- "doc": "tsx docgen.ts",
12
- "build": "tsc"
13
- },
14
- "repository": {
15
- "type": "git",
16
- "url": "git+https://github.com/NMMTY/LazyCanvas.git"
17
- },
18
- "keywords": [
19
- "canvas",
20
- "@napi-rs/canvas",
21
- "node-canvas",
22
- "easy",
23
- "simple"
24
- ],
25
- "author": "NMMTY",
26
- "license": "MIT",
27
- "bugs": {
28
- "url": "https://github.com/NMMTY/LazyCanvas/issues"
29
- },
30
- "homepage": "https://github.com/NMMTY/LazyCanvas#readme",
31
- "dependencies": {
32
- "@napi-rs/canvas": "^0.1.65",
33
- "jimp": "0.22.12",
34
- "path": "^0.12.7",
35
- "svgson": "^5.3.1"
36
- },
37
- "devDependencies": {
38
- "@hitomihiumi/micro-docgen": "^0.3.0",
39
- "@types/jimp": "^0.2.28",
40
- "@types/node": "^22.10.2",
41
- "@types/react": "^19.0.2",
42
- "ava": "^6.2.0",
43
- "globals": "^15.14.0",
44
- "tslib": "^2.8.1",
45
- "tsx": "^4.19.2",
46
- "typescript": "^5.4.5"
47
- },
48
- "publishConfig": {
49
- "access": "public"
50
- }
51
- }
1
+ {
2
+ "name": "@nmmty/lazycanvas",
3
+ "version": "0.3.0",
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
+ "doc": "tsx docgen.ts",
13
+ "build": "tsc"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/NMMTY/LazyCanvas.git"
18
+ },
19
+ "keywords": [
20
+ "canvas",
21
+ "@napi-rs/canvas",
22
+ "node-canvas",
23
+ "easy",
24
+ "simple"
25
+ ],
26
+ "author": "NMMTY",
27
+ "license": "MIT",
28
+ "bugs": {
29
+ "url": "https://github.com/NMMTY/LazyCanvas/issues"
30
+ },
31
+ "homepage": "https://github.com/NMMTY/LazyCanvas#readme",
32
+ "dependencies": {
33
+ "@napi-rs/canvas": "^0.1.65",
34
+ "jimp": "0.22.12",
35
+ "path": "^0.12.7",
36
+ "svgson": "^5.3.1"
37
+ },
38
+ "devDependencies": {
39
+ "@hitomihiumi/micro-docgen": "^0.3.0",
40
+ "@types/jimp": "^0.2.28",
41
+ "@types/node": "^22.10.2",
42
+ "@types/react": "^19.0.2",
43
+ "ava": "^6.2.0",
44
+ "globals": "^15.14.0",
45
+ "tslib": "^2.8.1",
46
+ "tsx": "^4.19.2",
47
+ "typescript": "^5.4.5"
48
+ }
49
+ }