@nmmty/lazycanvas 0.3.0 → 0.3.2

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.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * List of CSS [`filters`](https://developer.mozilla.org/en-US/docs/Web/CSS/filter) that can be used with the `filter` property. Use this functions to `setFilter` on the layer.
2
+ * List of CSS [`filters`](https://developer.mozilla.org/en-US/docs/Web/CSS/filter) that can be used with the `filter` property. Use this functions to `setFilters` on the layer.
3
3
  */
4
4
  export declare const Filters: {
5
5
  /**
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Filters = void 0;
4
4
  /**
5
- * List of CSS [`filters`](https://developer.mozilla.org/en-US/docs/Web/CSS/filter) that can be used with the `filter` property. Use this functions to `setFilter` on the layer.
5
+ * List of CSS [`filters`](https://developer.mozilla.org/en-US/docs/Web/CSS/filter) that can be used with the `filter` property. Use this functions to `setFilters` on the layer.
6
6
  */
7
7
  exports.Filters = {
8
8
  /**
@@ -56,9 +56,9 @@ export declare class BaseLayer<T extends IBaseLayerProps> {
56
56
  /**
57
57
  * @description The **`CanvasRenderingContext2D`**. filter property of the Canvas 2D API provides filter effects such as blurring and grayscaling.
58
58
  * It is similar to the CSS [`filter`](https://developer.mozilla.org/en-US/docs/Web/CSS/filter) property and accepts the same values.
59
- * @param filter {string} - The `filter` of the layer. Not support multiple filters.
59
+ * @param filter {string} - The `filter` of the layer. Multiple filters are supported.
60
60
  */
61
- setFilter(filter: string): this;
61
+ setFilters(...filter: string[]): this;
62
62
  /**
63
63
  * @description Sets type of centring of the layer.
64
64
  * @param centring {Centring} - The `centring` of the layer
@@ -111,10 +111,10 @@ class BaseLayer {
111
111
  /**
112
112
  * @description The **`CanvasRenderingContext2D`**. filter property of the Canvas 2D API provides filter effects such as blurring and grayscaling.
113
113
  * It is similar to the CSS [`filter`](https://developer.mozilla.org/en-US/docs/Web/CSS/filter) property and accepts the same values.
114
- * @param filter {string} - The `filter` of the layer. Not support multiple filters.
114
+ * @param filter {string} - The `filter` of the layer. Multiple filters are supported.
115
115
  */
116
- setFilter(filter) {
117
- this.props.filter = filter;
116
+ setFilters(...filter) {
117
+ this.props.filter = filter.join(' ');
118
118
  return this;
119
119
  }
120
120
  /**
@@ -1,6 +1,7 @@
1
1
  import { BaseLayer } from "./BaseLayer";
2
2
  import { ColorType, IBezierLayer, IBezierLayerProps, ScaleType } from "../../types";
3
3
  import { Canvas, SKRSContext2D } from "@napi-rs/canvas";
4
+ import { LayersManager } from "../managers/LayersManager";
4
5
  export declare class BezierLayer extends BaseLayer<IBezierLayerProps> {
5
6
  props: IBezierLayerProps;
6
7
  constructor(props?: IBezierLayerProps);
@@ -17,7 +18,7 @@ export declare class BezierLayer extends BaseLayer<IBezierLayerProps> {
17
18
  * @param x {ScaleType} - The end `x` of the bezier layer.
18
19
  * @param y {ScaleType} - The end `y` of the bezier layer.
19
20
  */
20
- setEndPoint(x: ScaleType, y: ScaleType): this;
21
+ setEndPosition(x: ScaleType, y: ScaleType): this;
21
22
  /**
22
23
  * @description Sets the color of the layer. You can use `hex`, `rgb`, `rgba`, `hsl`, `hsla`, and `Gradient`color.
23
24
  * @param color {string} - The `color` of the layer.
@@ -33,7 +34,7 @@ export declare class BezierLayer extends BaseLayer<IBezierLayerProps> {
33
34
  * @param miterLimit {number} - The `miterLimit` of the stroke.
34
35
  */
35
36
  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
+ draw(ctx: SKRSContext2D, canvas: Canvas, manager: LayersManager, debug: boolean): Promise<void>;
37
38
  /**
38
39
  * @returns {IBezierLayer}
39
40
  */
@@ -10,7 +10,7 @@ const Pattern_1 = require("../helpers/Pattern");
10
10
  class BezierLayer extends BaseLayer_1.BaseLayer {
11
11
  props;
12
12
  constructor(props) {
13
- super(enum_1.LayerType.BezierCurve, props);
13
+ super(enum_1.LayerType.BezierCurve, props || {});
14
14
  this.props = props ? props : {};
15
15
  if (!this.props.fillStyle)
16
16
  this.props.fillStyle = '#000000';
@@ -31,7 +31,7 @@ class BezierLayer extends BaseLayer_1.BaseLayer {
31
31
  * @param x {ScaleType} - The end `x` of the bezier layer.
32
32
  * @param y {ScaleType} - The end `y` of the bezier layer.
33
33
  */
34
- setEndPoint(x, y) {
34
+ setEndPosition(x, y) {
35
35
  this.props.endPoint = { x, y };
36
36
  return this;
37
37
  }
@@ -75,15 +75,15 @@ class BezierLayer extends BaseLayer_1.BaseLayer {
75
75
  };
76
76
  return this;
77
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 });
78
+ async draw(ctx, canvas, manager, debug) {
79
+ const xs = (0, utils_1.parseToNormal)(this.props.x, ctx, canvas);
80
+ const ys = (0, utils_1.parseToNormal)(this.props.y, ctx, canvas, { width: 0, height: 0 }, { vertical: true });
81
+ const cp1x = (0, utils_1.parseToNormal)(this.props.controlPoints[0].x, ctx, canvas);
82
+ const cp1y = (0, utils_1.parseToNormal)(this.props.controlPoints[0].y, ctx, canvas, { width: 0, height: 0 }, { vertical: true });
83
+ const cp2x = (0, utils_1.parseToNormal)(this.props.controlPoints[1].x, ctx, canvas);
84
+ const cp2y = (0, utils_1.parseToNormal)(this.props.controlPoints[1].y, ctx, canvas, { width: 0, height: 0 }, { vertical: true });
85
+ const xe = (0, utils_1.parseToNormal)(this.props.endPoint.x, ctx, canvas);
86
+ const ye = (0, utils_1.parseToNormal)(this.props.endPoint.y, ctx, canvas, { width: 0, height: 0 }, { vertical: true });
87
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
88
  let fillStyle = await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle);
89
89
  if (debug)
@@ -1,6 +1,7 @@
1
1
  import { BaseLayer } from "./BaseLayer";
2
2
  import { IImageLayer, IImageLayerProps, ScaleType } from "../../types";
3
3
  import { Canvas, SKRSContext2D } from "@napi-rs/canvas";
4
+ import { LayersManager } from "../managers/LayersManager";
4
5
  export declare class ImageLayer extends BaseLayer<IImageLayerProps> {
5
6
  props: IImageLayerProps;
6
7
  constructor(props?: IImageLayerProps);
@@ -16,7 +17,7 @@ export declare class ImageLayer extends BaseLayer<IImageLayerProps> {
16
17
  * @param radius {ScaleType} - The `radius` of the image. (optional)
17
18
  */
18
19
  setSize(width: ScaleType, height: ScaleType, radius?: ScaleType): this;
19
- draw(ctx: SKRSContext2D, canvas: Canvas, debug: boolean): Promise<void>;
20
+ draw(ctx: SKRSContext2D, canvas: Canvas, manager: LayersManager, debug: boolean): Promise<void>;
20
21
  /**
21
22
  * @returns {IImageLayer}
22
23
  */
@@ -33,7 +33,7 @@ const jimp = __importStar(require("jimp"));
33
33
  class ImageLayer extends BaseLayer_1.BaseLayer {
34
34
  props;
35
35
  constructor(props) {
36
- super(enum_1.LayerType.Image, props);
36
+ super(enum_1.LayerType.Image, props || {});
37
37
  this.props = props ? props : {};
38
38
  this.props.centring = enum_1.Centring.Center;
39
39
  }
@@ -61,12 +61,12 @@ class ImageLayer extends BaseLayer_1.BaseLayer {
61
61
  };
62
62
  return this;
63
63
  }
64
- async draw(ctx, canvas, debug) {
65
- let xs = (0, utils_1.parseToNormal)(this.props.x, canvas);
66
- let ys = (0, utils_1.parseToNormal)(this.props.y, canvas, { width: 0, height: 0 }, { vertical: true });
67
- let w = (0, utils_1.parseToNormal)(this.props.size.width, canvas);
68
- let h = (0, utils_1.parseToNormal)(this.props.size.height, canvas, { width: w, height: 0 }, { vertical: true });
69
- let r = (0, utils_1.parseToNormal)(this.props.size.radius, canvas, { width: w, height: h }, { layer: true });
64
+ async draw(ctx, canvas, manager, debug) {
65
+ let xs = (0, utils_1.parseToNormal)(this.props.x, ctx, canvas);
66
+ let ys = (0, utils_1.parseToNormal)(this.props.y, ctx, canvas, { width: 0, height: 0 }, { vertical: true });
67
+ let w = (0, utils_1.parseToNormal)(this.props.size.width, ctx, canvas);
68
+ let h = (0, utils_1.parseToNormal)(this.props.size.height, ctx, canvas, { width: w, height: 0 }, { vertical: true });
69
+ let r = (0, utils_1.parseToNormal)(this.props.size.radius, ctx, 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
71
  if (debug)
72
72
  LazyUtil_1.LazyLog.log('none', `ImageLayer:`, { x, y, w, h, r });
@@ -0,0 +1,31 @@
1
+ import { BaseLayer } from "./BaseLayer";
2
+ import { ColorType, ILineLayer, ILineLayerProps, ScaleType } from "../../types/";
3
+ import { Canvas, SKRSContext2D } from "@napi-rs/canvas";
4
+ import { LayersManager } from "../managers/LayersManager";
5
+ export declare class LineLayer extends BaseLayer<ILineLayerProps> {
6
+ props: ILineLayerProps;
7
+ constructor(props?: ILineLayerProps);
8
+ /**
9
+ * @description Sets the end point of the line layer. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
10
+ * @param x {ScaleType} - The end `x` of the line layer.
11
+ * @param y {ScaleType} - The end `y` of the line layer.
12
+ */
13
+ setEndPosition(x: ScaleType, y: ScaleType): this;
14
+ /**
15
+ * @description Sets the color of the layer. You can use `hex`, `rgb`, `rgba`, `hsl`, `hsla`, and `Gradient`color.
16
+ * @param color {string} - The `color` of the layer.
17
+ */
18
+ setColor(color: ColorType): this;
19
+ /**
20
+ * @description Sets the stroke of the layer.
21
+ * @param width {number} - The `width` of the stroke.
22
+ * @param cap {string} - The `cap` of the stroke.
23
+ * @param join {string} - The `join` of the stroke.
24
+ * @param dash {number[]} - The `dash` of the stroke.
25
+ * @param dashOffset {number} - The `dashOffset` of the stroke.
26
+ * @param miterLimit {number} - The `miterLimit` of the stroke.
27
+ */
28
+ setStroke(width: number, cap?: CanvasLineCap, join?: CanvasLineJoin, dash?: number[], dashOffset?: number, miterLimit?: number): this;
29
+ draw(ctx: SKRSContext2D, canvas: Canvas, manager: LayersManager, debug: boolean): Promise<void>;
30
+ toJSON(): ILineLayer;
31
+ }
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LineLayer = void 0;
4
+ const BaseLayer_1 = require("./BaseLayer");
5
+ const enum_1 = require("../../types/enum");
6
+ const LazyUtil_1 = require("../../utils/LazyUtil");
7
+ const utils_1 = require("../../utils/utils");
8
+ const Gradient_1 = require("../helpers/Gradient");
9
+ const Pattern_1 = require("../helpers/Pattern");
10
+ class LineLayer extends BaseLayer_1.BaseLayer {
11
+ props;
12
+ constructor(props) {
13
+ super(enum_1.LayerType.Line, 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 end point of the line layer. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
21
+ * @param x {ScaleType} - The end `x` of the line layer.
22
+ * @param y {ScaleType} - The end `y` of the line layer.
23
+ */
24
+ setEndPosition(x, y) {
25
+ this.props.endPoint = { x, y };
26
+ return this;
27
+ }
28
+ /**
29
+ * @description Sets the color of the layer. You can use `hex`, `rgb`, `rgba`, `hsl`, `hsla`, and `Gradient`color.
30
+ * @param color {string} - The `color` of the layer.
31
+ */
32
+ setColor(color) {
33
+ if (!color)
34
+ throw new LazyUtil_1.LazyError('The color of the layer must be provided');
35
+ if (!(0, utils_1.isColor)(color))
36
+ throw new LazyUtil_1.LazyError('The color of the layer must be a valid color');
37
+ let fill = (0, utils_1.parseColor)(color);
38
+ if (fill instanceof Gradient_1.Gradient || fill instanceof Pattern_1.Pattern) {
39
+ this.props.fillStyle = fill;
40
+ }
41
+ else {
42
+ let arr = fill.split(':');
43
+ this.props.fillStyle = arr[0];
44
+ this.props.opacity = parseFloat(arr[1]) || 1;
45
+ }
46
+ return this;
47
+ }
48
+ /**
49
+ * @description Sets the stroke of the layer.
50
+ * @param width {number} - The `width` of the stroke.
51
+ * @param cap {string} - The `cap` of the stroke.
52
+ * @param join {string} - The `join` of the stroke.
53
+ * @param dash {number[]} - The `dash` of the stroke.
54
+ * @param dashOffset {number} - The `dashOffset` of the stroke.
55
+ * @param miterLimit {number} - The `miterLimit` of the stroke.
56
+ */
57
+ setStroke(width, cap, join, dash, dashOffset, miterLimit) {
58
+ this.props.stroke = {
59
+ width,
60
+ cap: cap || 'butt',
61
+ join: join || 'miter',
62
+ dash: dash || [],
63
+ dashOffset: dashOffset || 0,
64
+ miterLimit: miterLimit || 10,
65
+ };
66
+ return this;
67
+ }
68
+ async draw(ctx, canvas, manager, debug) {
69
+ const xs = (0, utils_1.parseToNormal)(this.props.x, ctx, canvas);
70
+ const ys = (0, utils_1.parseToNormal)(this.props.y, ctx, canvas, { width: 0, height: 0 }, { vertical: true });
71
+ const xe = (0, utils_1.parseToNormal)(this.props.endPoint.x, ctx, canvas);
72
+ const ye = (0, utils_1.parseToNormal)(this.props.endPoint.y, ctx, canvas, { width: 0, height: 0 }, { vertical: true });
73
+ let width = xe - xs;
74
+ let height = ye - ys;
75
+ let fillStyle = await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle);
76
+ if (debug)
77
+ LazyUtil_1.LazyLog.log('none', `LineLayer:`, { xs, ys, xe, ye, width, height });
78
+ ctx.save();
79
+ (0, utils_1.transform)(ctx, this.props.transform, { x: xs, y: ys, width, height, type: this.type });
80
+ (0, utils_1.drawShadow)(ctx, this.props.shadow);
81
+ (0, utils_1.opacity)(ctx, this.props.opacity);
82
+ (0, utils_1.filters)(ctx, this.props.filter);
83
+ ctx.beginPath();
84
+ ctx.moveTo(xs, ys);
85
+ ctx.strokeStyle = fillStyle;
86
+ ctx.lineWidth = this.props.stroke?.width || 1;
87
+ ctx.lineCap = this.props.stroke?.cap || 'butt';
88
+ ctx.lineJoin = this.props.stroke?.join || 'miter';
89
+ ctx.miterLimit = this.props.stroke?.miterLimit || 10;
90
+ ctx.lineDashOffset = this.props.stroke?.dashOffset || 0;
91
+ ctx.setLineDash(this.props.stroke?.dash || []);
92
+ ctx.lineTo(xe, ye);
93
+ ctx.closePath();
94
+ }
95
+ toJSON() {
96
+ let data = super.toJSON();
97
+ data.props = this.props;
98
+ return { ...data };
99
+ }
100
+ }
101
+ exports.LineLayer = LineLayer;
@@ -1,6 +1,7 @@
1
1
  import { BaseLayer } from "./BaseLayer";
2
2
  import { IMorphLayer, IMorphLayerProps, ColorType, ScaleType } from "../../types";
3
3
  import { Canvas, SKRSContext2D } from "@napi-rs/canvas";
4
+ import { LayersManager } from "../managers/LayersManager";
4
5
  export declare class MorphLayer extends BaseLayer<IMorphLayerProps> {
5
6
  props: IMorphLayerProps;
6
7
  constructor(props?: IMorphLayerProps);
@@ -31,7 +32,7 @@ export declare class MorphLayer extends BaseLayer<IMorphLayerProps> {
31
32
  * @param filled {boolean} - The `filled` of the layer.
32
33
  */
33
34
  setFilled(filled: boolean): this;
34
- draw(ctx: SKRSContext2D, canvas: Canvas, debug: boolean): Promise<void>;
35
+ draw(ctx: SKRSContext2D, canvas: Canvas, manager: LayersManager, debug: boolean): Promise<void>;
35
36
  /**
36
37
  * @returns {IMorphLayer}
37
38
  */
@@ -10,7 +10,7 @@ const Pattern_1 = require("../helpers/Pattern");
10
10
  class MorphLayer extends BaseLayer_1.BaseLayer {
11
11
  props;
12
12
  constructor(props) {
13
- super(enum_1.LayerType.Morph, props);
13
+ super(enum_1.LayerType.Morph, props || {});
14
14
  this.props = props ? props : {};
15
15
  if (!this.props.fillStyle)
16
16
  this.props.fillStyle = '#000000';
@@ -80,12 +80,12 @@ class MorphLayer extends BaseLayer_1.BaseLayer {
80
80
  this.props.filled = filled;
81
81
  return this;
82
82
  }
83
- async draw(ctx, canvas, debug) {
84
- const xs = (0, utils_1.parseToNormal)(this.props.x, canvas);
85
- const ys = (0, utils_1.parseToNormal)(this.props.y, canvas, { width: 0, height: 0 }, { vertical: true });
86
- const w = (0, utils_1.parseToNormal)(this.props.size.width, canvas);
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 / 2, height: h / 2 }, { layer: true });
83
+ async draw(ctx, canvas, manager, debug) {
84
+ const xs = (0, utils_1.parseToNormal)(this.props.x, ctx, canvas);
85
+ const ys = (0, utils_1.parseToNormal)(this.props.y, ctx, canvas, { width: 0, height: 0 }, { vertical: true });
86
+ const w = (0, utils_1.parseToNormal)(this.props.size.width, ctx, canvas, { width: 0, height: 0 }, { vertical: false }, manager);
87
+ const h = (0, utils_1.parseToNormal)(this.props.size.height, ctx, canvas, { width: w, height: 0 }, { vertical: true }, manager);
88
+ const r = (0, utils_1.parseToNormal)(this.props.size.radius, ctx, 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
90
  let fillStyle = await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle);
91
91
  if (debug)
@@ -1,6 +1,7 @@
1
1
  import { BaseLayer } from "./BaseLayer";
2
2
  import { IQuadraticLayerProps, IQuadraticLayer, ColorType, ScaleType } from "../../types";
3
3
  import { Canvas, SKRSContext2D } from "@napi-rs/canvas";
4
+ import { LayersManager } from "../managers/LayersManager";
4
5
  export declare class QuadraticLayer extends BaseLayer<IQuadraticLayerProps> {
5
6
  props: IQuadraticLayerProps;
6
7
  constructor(props?: IQuadraticLayerProps);
@@ -15,7 +16,7 @@ export declare class QuadraticLayer extends BaseLayer<IQuadraticLayerProps> {
15
16
  * @param x {ScaleType} - The end `x` of the quadratic layer.
16
17
  * @param y {ScaleType} - The end `y` of the quadratic layer.
17
18
  */
18
- setEndPoint(x: ScaleType, y: ScaleType): this;
19
+ setEndPosition(x: ScaleType, y: ScaleType): this;
19
20
  /**
20
21
  * @description Sets the color of the layer. You can use `hex`, `rgb`, `rgba`, `hsl`, `hsla`, and `Gradient`color.
21
22
  * @param color {string} - The `color` of the layer.
@@ -31,7 +32,7 @@ export declare class QuadraticLayer extends BaseLayer<IQuadraticLayerProps> {
31
32
  * @param miterLimit {number} - The `miterLimit` of the stroke.
32
33
  */
33
34
  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
+ draw(ctx: SKRSContext2D, canvas: Canvas, manager: LayersManager, debug: boolean): Promise<void>;
35
36
  /**
36
37
  * @returns {IQuadraticLayer}
37
38
  */
@@ -10,7 +10,7 @@ const Pattern_1 = require("../helpers/Pattern");
10
10
  class QuadraticLayer extends BaseLayer_1.BaseLayer {
11
11
  props;
12
12
  constructor(props) {
13
- super(enum_1.LayerType.QuadraticCurve, props);
13
+ super(enum_1.LayerType.QuadraticCurve, props || {});
14
14
  this.props = props ? props : {};
15
15
  if (!this.props.fillStyle)
16
16
  this.props.fillStyle = '#000000';
@@ -30,7 +30,7 @@ class QuadraticLayer extends BaseLayer_1.BaseLayer {
30
30
  * @param x {ScaleType} - The end `x` of the quadratic layer.
31
31
  * @param y {ScaleType} - The end `y` of the quadratic layer.
32
32
  */
33
- setEndPoint(x, y) {
33
+ setEndPosition(x, y) {
34
34
  this.props.endPoint = { x, y };
35
35
  return this;
36
36
  }
@@ -74,13 +74,13 @@ class QuadraticLayer extends BaseLayer_1.BaseLayer {
74
74
  };
75
75
  return this;
76
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 });
77
+ async draw(ctx, canvas, manager, debug) {
78
+ const xs = (0, utils_1.parseToNormal)(this.props.x, ctx, canvas);
79
+ const ys = (0, utils_1.parseToNormal)(this.props.y, ctx, canvas, { width: 0, height: 0 }, { vertical: true });
80
+ const cx = (0, utils_1.parseToNormal)(this.props.controlPoint.x, ctx, canvas);
81
+ const cy = (0, utils_1.parseToNormal)(this.props.controlPoint.y, ctx, canvas, { width: 0, height: 0 }, { vertical: true });
82
+ const xe = (0, utils_1.parseToNormal)(this.props.endPoint.x, ctx, canvas);
83
+ const ye = (0, utils_1.parseToNormal)(this.props.endPoint.y, ctx, canvas, { width: 0, height: 0 }, { vertical: true });
84
84
  const { max, min, center, width, height } = (0, utils_1.getBoundingBoxBezier)([{ x: xs, y: ys }, { x: cx, y: cy }, { x: xe, y: ye }]);
85
85
  let fillStyle = await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle);
86
86
  if (debug)
@@ -2,6 +2,7 @@ import { BaseLayer } from "./BaseLayer";
2
2
  import { FontWeight, LineCap, LineJoin, TextAlign, TextBaseline, TextDirection } from "../../types/enum";
3
3
  import { ITextLayer, ITextLayerProps, ScaleType, ColorType } from "../../types";
4
4
  import { Canvas, SKRSContext2D } from "@napi-rs/canvas";
5
+ import { LayersManager } from "../managers/LayersManager";
5
6
  export declare class TextLayer extends BaseLayer<ITextLayerProps> {
6
7
  props: ITextLayerProps;
7
8
  constructor(props?: ITextLayerProps);
@@ -60,7 +61,11 @@ export declare class TextLayer extends BaseLayer<ITextLayerProps> {
60
61
  * @param filled {boolean} - The `filled` of the layer.
61
62
  */
62
63
  setFilled(filled: boolean): this;
63
- draw(ctx: SKRSContext2D, canvas: Canvas, debug: boolean): Promise<void>;
64
+ measureText(ctx: SKRSContext2D, canvas: Canvas): {
65
+ width: number;
66
+ height: number;
67
+ };
68
+ draw(ctx: SKRSContext2D, canvas: Canvas, manager: LayersManager, debug: boolean): Promise<void>;
64
69
  private drawText;
65
70
  /**
66
71
  * @returns {ITextLayer}
@@ -10,7 +10,7 @@ const Pattern_1 = require("../helpers/Pattern");
10
10
  class TextLayer extends BaseLayer_1.BaseLayer {
11
11
  props;
12
12
  constructor(props) {
13
- super(enum_1.LayerType.Text, props);
13
+ super(enum_1.LayerType.Text, props || {});
14
14
  this.props = props ? props : {};
15
15
  this.props.align = enum_1.TextAlign.Left;
16
16
  this.props.font = {
@@ -144,11 +144,23 @@ class TextLayer extends BaseLayer_1.BaseLayer {
144
144
  this.props.filled = filled;
145
145
  return this;
146
146
  }
147
- async draw(ctx, canvas, debug) {
148
- const x = (0, utils_1.parseToNormal)(this.props.x, canvas);
149
- const y = (0, utils_1.parseToNormal)(this.props.y, canvas, { width: 0, height: 0 }, { vertical: true });
150
- const w = (0, utils_1.parseToNormal)(this.props.multiline?.width, canvas);
151
- const h = (0, utils_1.parseToNormal)(this.props.multiline?.height, canvas, { width: w, height: 0 }, { vertical: true });
147
+ measureText(ctx, canvas) {
148
+ const w = (0, utils_1.parseToNormal)(this.props.multiline?.width, ctx, canvas);
149
+ const h = (0, utils_1.parseToNormal)(this.props.multiline?.height, ctx, canvas, { width: w, height: 0 }, { vertical: true });
150
+ if (this.props.multiline.enabled) {
151
+ return { width: w, height: h };
152
+ }
153
+ else {
154
+ ctx.font = `${this.props.font.weight} ${this.props.font.size}px ${this.props.font.family}`;
155
+ let data = ctx.measureText(this.props.text);
156
+ return { width: data.width, height: this.props.font.size };
157
+ }
158
+ }
159
+ async draw(ctx, canvas, manager, debug) {
160
+ const x = (0, utils_1.parseToNormal)(this.props.x, ctx, canvas);
161
+ const y = (0, utils_1.parseToNormal)(this.props.y, ctx, canvas, { width: 0, height: 0 }, { vertical: true });
162
+ const w = (0, utils_1.parseToNormal)(this.props.multiline?.width, ctx, canvas);
163
+ const h = (0, utils_1.parseToNormal)(this.props.multiline?.height, ctx, canvas, { width: w, height: 0 }, { vertical: true });
152
164
  if (debug)
153
165
  LazyUtil_1.LazyLog.log('none', `TextLayer:`, { x, y, w, h });
154
166
  ctx.save();
@@ -23,12 +23,12 @@ class RenderManager {
23
23
  if (layer instanceof Group_1.Group) {
24
24
  for (const subLayer of layer.components) {
25
25
  if (subLayer.visible) {
26
- await subLayer.draw(this.lazyCanvas.ctx, this.lazyCanvas.canvas, this.debug);
26
+ await subLayer.draw(this.lazyCanvas.ctx, this.lazyCanvas.canvas, this.lazyCanvas.layers, this.debug);
27
27
  }
28
28
  }
29
29
  }
30
30
  else {
31
- await layer.draw(this.lazyCanvas.ctx, this.lazyCanvas.canvas, this.debug);
31
+ await layer.draw(this.lazyCanvas.ctx, this.lazyCanvas.canvas, this.lazyCanvas.layers, this.debug);
32
32
  }
33
33
  this.lazyCanvas.ctx.shadowColor = 'transparent';
34
34
  }
@@ -0,0 +1,12 @@
1
+ import { IBaseLayer, IBaseLayerProps, ScaleType } from "../";
2
+
3
+ export interface ILineLayer extends IBaseLayer {
4
+ props: ILineLayerProps;
5
+ }
6
+
7
+ export interface ILineLayerProps extends IBaseLayerProps {
8
+ endPoint: {
9
+ x: ScaleType,
10
+ y: ScaleType
11
+ }
12
+ }
@@ -5,6 +5,7 @@ export * from "./components/ImageLayer";
5
5
  export * from "./components/MorphLayer";
6
6
  export * from "./components/BezierLayer";
7
7
  export * from "./components/QuadraticLayer";
8
+ export * from "./components/LineLayer";
8
9
  export * from "./components/Group";
9
10
  export * from "./helpers/Font";
10
11
  export * from "./helpers/Gradient";
@@ -3,17 +3,18 @@ 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";
6
+ import { LayersManager } from "../structures/managers/LayersManager";
6
7
  export declare function generateID(type: string): string;
7
8
  export declare function isColor(v: ColorType): "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
8
9
  export declare function parseHex(v: string): string;
9
10
  export declare function parseColor(v: ColorType): string | Pattern | Gradient;
10
- export declare function parseToNormal(v: ScaleType, canvas: Canvas, layer?: {
11
+ export declare function parseToNormal(v: ScaleType, ctx: SKRSContext2D, canvas: Canvas, layer?: {
11
12
  width: number;
12
13
  height: number;
13
14
  }, options?: {
14
15
  vertical?: boolean;
15
16
  layer?: boolean;
16
- }): number;
17
+ }, manager?: LayersManager): number;
17
18
  export declare function drawShadow(ctx: SKRSContext2D, shadow: any): void;
18
19
  export declare function opacity(ctx: SKRSContext2D, opacity: number): void;
19
20
  export declare function filters(ctx: SKRSContext2D, filters: string): void;
@@ -30,6 +30,7 @@ const LazyUtil_1 = require("./LazyUtil");
30
30
  const fs = __importStar(require("fs"));
31
31
  const jimp = __importStar(require("jimp"));
32
32
  const Pattern_1 = require("../structures/helpers/Pattern");
33
+ const TextLayer_1 = require("../structures/components/TextLayer");
33
34
  function generateID(type) {
34
35
  return `${type}-${Math.random().toString(36).substr(2, 9)}`;
35
36
  }
@@ -37,6 +38,7 @@ exports.generateID = generateID;
37
38
  let percentReg = /^(\d+)%$/;
38
39
  let pxReg = /^(\d+)px$/;
39
40
  let canvasReg = /^(vw|vh|vmin|vmax)$/;
41
+ let linkReg = /^(link-w|link-h)-([A-Za-z0-9_]+)-(\d+)$/;
40
42
  let hexReg = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
41
43
  let rgbReg = /^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/;
42
44
  let rgbaReg = /^rgba\((\d+),\s*(\d+),\s*(\d+),\s*(0|0?\.\d+|1(\.0)?)\)$/;
@@ -114,12 +116,12 @@ function parseColor(v) {
114
116
  }
115
117
  }
116
118
  exports.parseColor = parseColor;
117
- function parseToNormal(v, canvas, layer = { width: 0, height: 0 }, options = { vertical: false, layer: false }) {
119
+ function parseToNormal(v, ctx, canvas, layer = { width: 0, height: 0 }, options = { vertical: false, layer: false }, manager) {
118
120
  if (typeof v === 'number') {
119
121
  return v;
120
122
  }
121
123
  else if (percentReg.test(v)) {
122
- return parseFloat(v) * (options.layer ? (options.vertical ? layer.width : layer.height) : (options.vertical ? canvas.width : canvas.height));
124
+ return (parseFloat(v) / 100) * (options.layer ? (options.vertical ? layer.width : layer.height) : (options.vertical ? canvas.width : canvas.height));
123
125
  }
124
126
  else if (pxReg.test(v)) {
125
127
  return parseFloat(v);
@@ -138,6 +140,24 @@ function parseToNormal(v, canvas, layer = { width: 0, height: 0 }, options = { v
138
140
  return (options.layer ? Math.max(layer.width, layer.height) : Math.max(canvas.width, canvas.height));
139
141
  }
140
142
  }
143
+ else if (linkReg.test(v)) {
144
+ let match = v.match(linkReg);
145
+ if (!manager)
146
+ return 0;
147
+ let layer = manager.get(match[2]);
148
+ switch (match[1]) {
149
+ case 'link-w':
150
+ if (layer instanceof TextLayer_1.TextLayer) {
151
+ return layer.measureText(ctx, canvas).width + (parseInt(match[3]) || 0);
152
+ }
153
+ break;
154
+ case 'link-h':
155
+ if (layer instanceof TextLayer_1.TextLayer) {
156
+ return layer.measureText(ctx, canvas).height + (parseInt(match[3]) || 0);
157
+ }
158
+ break;
159
+ }
160
+ }
141
161
  return 0;
142
162
  }
143
163
  exports.parseToNormal = parseToNormal;
@@ -182,6 +202,7 @@ function transform(ctx, transform, layer = { width: 0, height: 0, x: 0, y: 0, ty
182
202
  case enum_1.LayerType.Morph:
183
203
  case enum_1.LayerType.BezierCurve:
184
204
  case enum_1.LayerType.QuadraticCurve:
205
+ case enum_1.LayerType.Line:
185
206
  ctx.translate(layer.x + (layer.width / 2), layer.y + (layer.height / 2));
186
207
  ctx.rotate((Math.PI / 180) * transform.rotate);
187
208
  ctx.translate(-(layer.x + (layer.width / 2)), -(layer.y + (layer.height / 2)));
package/example.png ADDED
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nmmty/lazycanvas",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "A simple way to interact with @napi-rs/canvas in an advanced way!",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",