@nmmty/lazycanvas 0.3.0 → 0.3.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.
@@ -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);
@@ -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
  */
@@ -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
  */
@@ -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 });
@@ -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
  */
@@ -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);
@@ -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
  */
@@ -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}
@@ -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
  }
@@ -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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nmmty/lazycanvas",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
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",