@nmmty/lazycanvas 0.5.2 → 0.6.0-dev.1a1f37
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.
- package/dist/structures/components/ClearLayer.d.ts +5 -1
- package/dist/structures/components/Group.d.ts +15 -1
- package/dist/structures/components/Group.js +19 -0
- package/dist/structures/components/ImageLayer.d.ts +8 -4
- package/dist/structures/components/ImageLayer.js +16 -9
- package/dist/structures/components/MorphLayer.d.ts +8 -4
- package/dist/structures/components/MorphLayer.js +16 -9
- package/dist/structures/managers/RenderManager.js +2 -23
- package/dist/types/types.d.ts +48 -20
- package/dist/utils/utils.js +53 -84
- package/package.json +3 -2
- package/dist/structures/helpers/readers/SVGReader.d.ts +0 -20
- package/dist/structures/helpers/readers/SVGReader.js +0 -577
- package/dist/structures/helpers/readers/YAMLReader.d.ts +0 -0
- package/dist/structures/helpers/readers/YAMLReader.js +0 -1
- package/dist/types/LazyCanvas.d.ts +0 -18
- package/dist/types/components/BaseLayer.d.ts +0 -49
- package/dist/types/components/BezierLayer.d.ts +0 -11
- package/dist/types/components/ClearLayer.d.ts +0 -19
- package/dist/types/components/Group.d.ts +0 -8
- package/dist/types/components/ImageLayer.d.ts +0 -15
- package/dist/types/components/LineLayer.d.ts +0 -12
- package/dist/types/components/MorphLayer.d.ts +0 -14
- package/dist/types/components/QuadraticLayer.d.ts +0 -11
- package/dist/types/components/TextLayer.d.ts +0 -28
- package/dist/types/helpers/Font.d.ts +0 -12
- package/dist/types/helpers/Gradient.d.ts +0 -19
- package/dist/types/helpers/Link.d.ts +0 -7
- package/dist/types/helpers/Pattern.d.ts +0 -7
- package/dist/types/managers/AnimationManager.d.ts +0 -14
- package/dist/types/managers/FontsManager.d.ts +0 -6
- package/dist/types/managers/LayersManager.d.ts +0 -7
- package/dist/types/managers/RenderManager.d.ts +0 -6
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ScaleType, LayerType, AnyCentring } from "../../types";
|
|
1
|
+
import { ScaleType, LayerType, AnyCentring, AnyGlobalCompositeOperation } from "../../types";
|
|
2
2
|
import { Canvas, SKRSContext2D, SvgCanvas } from "@napi-rs/canvas";
|
|
3
3
|
import { LayersManager } from "../managers/LayersManager";
|
|
4
4
|
import { IBaseLayerMisc } from "./BaseLayer";
|
|
@@ -56,6 +56,10 @@ export interface IClearLayerProps {
|
|
|
56
56
|
* The centring type of the layer.
|
|
57
57
|
*/
|
|
58
58
|
centring: AnyCentring;
|
|
59
|
+
/**
|
|
60
|
+
* Don't use, this is just for compatibility.
|
|
61
|
+
*/
|
|
62
|
+
globalComposite: AnyGlobalCompositeOperation;
|
|
59
63
|
}
|
|
60
64
|
/**
|
|
61
65
|
* Class representing a Clear Layer.
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { AnyLayer, LayerType } from "../../types";
|
|
1
|
+
import { AnyGlobalCompositeOperation, AnyLayer, LayerType } from "../../types";
|
|
2
|
+
import { Canvas, SKRSContext2D, SvgCanvas } from "@napi-rs/canvas";
|
|
3
|
+
import { LayersManager } from "../managers/LayersManager";
|
|
2
4
|
/**
|
|
3
5
|
* Interface representing a group of layers.
|
|
4
6
|
*/
|
|
@@ -23,6 +25,16 @@ export interface IGroup {
|
|
|
23
25
|
* The layers contained within the group.
|
|
24
26
|
*/
|
|
25
27
|
layers: Array<AnyLayer>;
|
|
28
|
+
/**
|
|
29
|
+
*
|
|
30
|
+
*/
|
|
31
|
+
props?: IGroupProps;
|
|
32
|
+
}
|
|
33
|
+
export interface IGroupProps {
|
|
34
|
+
/**
|
|
35
|
+
* Don't use, this is just for compatibility.
|
|
36
|
+
*/
|
|
37
|
+
globalComposite: AnyGlobalCompositeOperation;
|
|
26
38
|
}
|
|
27
39
|
/**
|
|
28
40
|
* Class representing a group of layers.
|
|
@@ -48,6 +60,7 @@ export declare class Group implements IGroup {
|
|
|
48
60
|
* The layers contained within the group.
|
|
49
61
|
*/
|
|
50
62
|
layers: Array<any>;
|
|
63
|
+
props?: IGroupProps;
|
|
51
64
|
/**
|
|
52
65
|
* Constructs a new Group instance.
|
|
53
66
|
* @param opts {Object} - Optional parameters for the group.
|
|
@@ -111,6 +124,7 @@ export declare class Group implements IGroup {
|
|
|
111
124
|
* @returns {number} The number of components in the group.
|
|
112
125
|
*/
|
|
113
126
|
get length(): number;
|
|
127
|
+
draw(ctx: SKRSContext2D, canvas: Canvas | SvgCanvas, manager: LayersManager, debug: boolean): Promise<void>;
|
|
114
128
|
/**
|
|
115
129
|
* Converts the group to a JSON representation.
|
|
116
130
|
* @returns {IGroup} The JSON representation of the group.
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Group = void 0;
|
|
4
4
|
const types_1 = require("../../types");
|
|
5
5
|
const utils_1 = require("../../utils/utils");
|
|
6
|
+
const LazyUtil_1 = require("../../utils/LazyUtil");
|
|
6
7
|
/**
|
|
7
8
|
* Class representing a group of layers.
|
|
8
9
|
*/
|
|
@@ -27,6 +28,7 @@ class Group {
|
|
|
27
28
|
* The layers contained within the group.
|
|
28
29
|
*/
|
|
29
30
|
layers;
|
|
31
|
+
props;
|
|
30
32
|
/**
|
|
31
33
|
* Constructs a new Group instance.
|
|
32
34
|
* @param opts {Object} - Optional parameters for the group.
|
|
@@ -39,6 +41,7 @@ class Group {
|
|
|
39
41
|
this.visible = opts?.visible || true;
|
|
40
42
|
this.zIndex = opts?.zIndex || 1;
|
|
41
43
|
this.layers = [];
|
|
44
|
+
this.props = {};
|
|
42
45
|
}
|
|
43
46
|
/**
|
|
44
47
|
* Sets the ID of the group.
|
|
@@ -118,6 +121,22 @@ class Group {
|
|
|
118
121
|
get length() {
|
|
119
122
|
return this.layers.length;
|
|
120
123
|
}
|
|
124
|
+
async draw(ctx, canvas, manager, debug) {
|
|
125
|
+
for (const subLayer of this.layers) {
|
|
126
|
+
if (debug)
|
|
127
|
+
LazyUtil_1.LazyLog.log('info', `Rendering ${subLayer.id}...\nData:`, subLayer.toJSON());
|
|
128
|
+
if (subLayer.visible) {
|
|
129
|
+
if ('globalComposite' in subLayer.props && subLayer.props.globalComposite) {
|
|
130
|
+
ctx.globalCompositeOperation = subLayer.props.globalComposite;
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
ctx.globalCompositeOperation = 'source-over';
|
|
134
|
+
}
|
|
135
|
+
await subLayer.draw(ctx, canvas, manager, debug);
|
|
136
|
+
ctx.shadowColor = 'transparent';
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
121
140
|
/**
|
|
122
141
|
* Converts the group to a JSON representation.
|
|
123
142
|
* @returns {IGroup} The JSON representation of the group.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
3
|
import { BaseLayer, IBaseLayer, IBaseLayerMisc, IBaseLayerProps } from "./BaseLayer";
|
|
4
|
-
import { ScaleType, LayerType } from "../../types";
|
|
4
|
+
import { ScaleType, LayerType, radiusCorner } from "../../types";
|
|
5
5
|
import { Canvas, SKRSContext2D, SvgCanvas } from "@napi-rs/canvas";
|
|
6
6
|
import { LayersManager } from "../managers/LayersManager";
|
|
7
7
|
/**
|
|
@@ -40,7 +40,9 @@ export interface IImageLayerProps extends IBaseLayerProps {
|
|
|
40
40
|
/**
|
|
41
41
|
* The radius of the image.
|
|
42
42
|
*/
|
|
43
|
-
radius:
|
|
43
|
+
radius: {
|
|
44
|
+
[corner in radiusCorner]?: ScaleType;
|
|
45
|
+
};
|
|
44
46
|
};
|
|
45
47
|
}
|
|
46
48
|
/**
|
|
@@ -68,10 +70,12 @@ export declare class ImageLayer extends BaseLayer<IImageLayerProps> {
|
|
|
68
70
|
* Sets the size of the image.
|
|
69
71
|
* @param width {ScaleType} - The width of the image.
|
|
70
72
|
* @param height {ScaleType} - The height of the image.
|
|
71
|
-
* @param radius {ScaleType} - The radius of the image (optional).
|
|
73
|
+
* @param radius {{ [corner in radiusCorner]?: ScaleType }} - The radius of the image (optional).
|
|
72
74
|
* @returns {this} The current instance for chaining.
|
|
73
75
|
*/
|
|
74
|
-
setSize(width: ScaleType, height: ScaleType, radius?:
|
|
76
|
+
setSize(width: ScaleType, height: ScaleType, radius?: {
|
|
77
|
+
[corner in radiusCorner]?: ScaleType;
|
|
78
|
+
}): this;
|
|
75
79
|
/**
|
|
76
80
|
* Draws the Image Layer on the canvas.
|
|
77
81
|
* @param ctx {SKRSContext2D} - The canvas rendering context.
|
|
@@ -6,6 +6,7 @@ const types_1 = require("../../types");
|
|
|
6
6
|
const canvas_1 = require("@napi-rs/canvas");
|
|
7
7
|
const utils_1 = require("../../utils/utils");
|
|
8
8
|
const LazyUtil_1 = require("../../utils/LazyUtil");
|
|
9
|
+
const helpers_1 = require("../helpers");
|
|
9
10
|
/**
|
|
10
11
|
* Class representing an Image Layer, extending the BaseLayer class.
|
|
11
12
|
*/
|
|
@@ -40,14 +41,14 @@ class ImageLayer extends BaseLayer_1.BaseLayer {
|
|
|
40
41
|
* Sets the size of the image.
|
|
41
42
|
* @param width {ScaleType} - The width of the image.
|
|
42
43
|
* @param height {ScaleType} - The height of the image.
|
|
43
|
-
* @param radius {ScaleType} - The radius of the image (optional).
|
|
44
|
+
* @param radius {{ [corner in radiusCorner]?: ScaleType }} - The radius of the image (optional).
|
|
44
45
|
* @returns {this} The current instance for chaining.
|
|
45
46
|
*/
|
|
46
47
|
setSize(width, height, radius) {
|
|
47
48
|
this.props.size = {
|
|
48
49
|
width: width,
|
|
49
50
|
height: height,
|
|
50
|
-
radius: radius || 0,
|
|
51
|
+
radius: radius || { all: 0 },
|
|
51
52
|
};
|
|
52
53
|
return this;
|
|
53
54
|
}
|
|
@@ -67,10 +68,16 @@ class ImageLayer extends BaseLayer_1.BaseLayer {
|
|
|
67
68
|
w: { v: this.props.size.width }
|
|
68
69
|
});
|
|
69
70
|
const h = parcer.parse(this.props.size.height, LazyUtil_1.defaultArg.wh(w), LazyUtil_1.defaultArg.vl(true));
|
|
70
|
-
const r = parcer.parse(this.props.size.radius, LazyUtil_1.defaultArg.wh(w / 2, h / 2), LazyUtil_1.defaultArg.vl(false, true));
|
|
71
71
|
let { x, y } = (0, utils_1.centring)(this.props.centring, this.type, w, h, xs, ys);
|
|
72
|
+
const rad = {};
|
|
73
|
+
if (typeof this.props.size.radius === 'object' && this.props.size.radius !== helpers_1.Link) {
|
|
74
|
+
for (const corner in this.props.size.radius) {
|
|
75
|
+
// @ts-ignore
|
|
76
|
+
rad[corner] = parcer.parse(this.props.size.radius[corner], LazyUtil_1.defaultArg.wh(w / 2, h / 2), LazyUtil_1.defaultArg.vl(false, true));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
72
79
|
if (debug)
|
|
73
|
-
LazyUtil_1.LazyLog.log('none', `ImageLayer:`, { x, y, w, h,
|
|
80
|
+
LazyUtil_1.LazyLog.log('none', `ImageLayer:`, { x, y, w, h, rad });
|
|
74
81
|
ctx.save();
|
|
75
82
|
(0, utils_1.transform)(ctx, this.props.transform, { width: w, height: h, x, y, type: this.type });
|
|
76
83
|
(0, utils_1.drawShadow)(ctx, this.props.shadow);
|
|
@@ -81,13 +88,13 @@ class ImageLayer extends BaseLayer_1.BaseLayer {
|
|
|
81
88
|
image.height = h;
|
|
82
89
|
if (!image)
|
|
83
90
|
throw new LazyUtil_1.LazyError('The image could not be loaded');
|
|
84
|
-
if (
|
|
91
|
+
if (Object.keys(rad).length > 0) {
|
|
85
92
|
ctx.beginPath();
|
|
86
93
|
ctx.moveTo(x + (w / 2), y);
|
|
87
|
-
ctx.arcTo(x + w, y, x + w, y + (h / 2),
|
|
88
|
-
ctx.arcTo(x + w, y + h, x + (w / 2), y + h,
|
|
89
|
-
ctx.arcTo(x, y + h, x, y + (h / 2),
|
|
90
|
-
ctx.arcTo(x, y, x + (w / 2), y,
|
|
94
|
+
ctx.arcTo(x + w, y, x + w, y + (h / 2), rad.rightTop || rad.all || 0);
|
|
95
|
+
ctx.arcTo(x + w, y + h, x + (w / 2), y + h, rad.rightBottom || rad.all || 0);
|
|
96
|
+
ctx.arcTo(x, y + h, x, y + (h / 2), rad.leftBottom || rad.all || 0);
|
|
97
|
+
ctx.arcTo(x, y, x + (w / 2), y, rad.leftTop || rad.all || 0);
|
|
91
98
|
ctx.closePath();
|
|
92
99
|
ctx.clip();
|
|
93
100
|
ctx.drawImage(image, x, y, w, h);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BaseLayer, IBaseLayer, IBaseLayerMisc, IBaseLayerProps } from "./BaseLayer";
|
|
2
|
-
import { ColorType, ScaleType, LayerType } from "../../types";
|
|
2
|
+
import { ColorType, ScaleType, LayerType, radiusCorner } from "../../types";
|
|
3
3
|
import { Canvas, SKRSContext2D, SvgCanvas } from "@napi-rs/canvas";
|
|
4
4
|
import { LayersManager } from "../managers/LayersManager";
|
|
5
5
|
/**
|
|
@@ -34,7 +34,9 @@ export interface IMorphLayerProps extends IBaseLayerProps {
|
|
|
34
34
|
/**
|
|
35
35
|
* The radius of the Morph Layer.
|
|
36
36
|
*/
|
|
37
|
-
radius:
|
|
37
|
+
radius: {
|
|
38
|
+
[corner in radiusCorner]?: ScaleType;
|
|
39
|
+
};
|
|
38
40
|
};
|
|
39
41
|
/**
|
|
40
42
|
* The stroke properties of the morph.
|
|
@@ -84,10 +86,12 @@ export declare class MorphLayer extends BaseLayer<IMorphLayerProps> {
|
|
|
84
86
|
* Sets the size of the Morph Layer.
|
|
85
87
|
* @param width {ScaleType} - The width of the Morph Layer.
|
|
86
88
|
* @param height {ScaleType} - The height of the Morph Layer.
|
|
87
|
-
* @param radius {ScaleType} - The radius of the Morph Layer (optional).
|
|
89
|
+
* @param radius {{ [corner in radiusCorner]?: ScaleType }} - The radius of the Morph Layer (optional).
|
|
88
90
|
* @returns {this} The current instance for chaining.
|
|
89
91
|
*/
|
|
90
|
-
setSize(width: ScaleType, height: ScaleType, radius?:
|
|
92
|
+
setSize(width: ScaleType, height: ScaleType, radius?: {
|
|
93
|
+
[corner in radiusCorner]?: ScaleType;
|
|
94
|
+
}): this;
|
|
91
95
|
/**
|
|
92
96
|
* Sets the color of the Morph Layer.
|
|
93
97
|
* @param color {string} - The color of the Morph Layer.
|
|
@@ -5,6 +5,7 @@ const BaseLayer_1 = require("./BaseLayer");
|
|
|
5
5
|
const types_1 = require("../../types");
|
|
6
6
|
const utils_1 = require("../../utils/utils");
|
|
7
7
|
const LazyUtil_1 = require("../../utils/LazyUtil");
|
|
8
|
+
const helpers_1 = require("../helpers");
|
|
8
9
|
/**
|
|
9
10
|
* Class representing a Morph Layer, extending the BaseLayer class.
|
|
10
11
|
*/
|
|
@@ -31,14 +32,14 @@ class MorphLayer extends BaseLayer_1.BaseLayer {
|
|
|
31
32
|
* Sets the size of the Morph Layer.
|
|
32
33
|
* @param width {ScaleType} - The width of the Morph Layer.
|
|
33
34
|
* @param height {ScaleType} - The height of the Morph Layer.
|
|
34
|
-
* @param radius {ScaleType} - The radius of the Morph Layer (optional).
|
|
35
|
+
* @param radius {{ [corner in radiusCorner]?: ScaleType }} - The radius of the Morph Layer (optional).
|
|
35
36
|
* @returns {this} The current instance for chaining.
|
|
36
37
|
*/
|
|
37
38
|
setSize(width, height, radius) {
|
|
38
39
|
this.props.size = {
|
|
39
40
|
width: width,
|
|
40
41
|
height: height,
|
|
41
|
-
radius: radius || 0,
|
|
42
|
+
radius: radius || { all: 0 },
|
|
42
43
|
};
|
|
43
44
|
return this;
|
|
44
45
|
}
|
|
@@ -101,20 +102,26 @@ class MorphLayer extends BaseLayer_1.BaseLayer {
|
|
|
101
102
|
w: { v: this.props.size.width },
|
|
102
103
|
});
|
|
103
104
|
const h = parcer.parse(this.props.size.height, LazyUtil_1.defaultArg.wh(w), LazyUtil_1.defaultArg.vl(true));
|
|
104
|
-
const
|
|
105
|
+
const rad = {};
|
|
106
|
+
if (typeof this.props.size.radius === 'object' && this.props.size.radius !== helpers_1.Link) {
|
|
107
|
+
for (const corner in this.props.size.radius) {
|
|
108
|
+
// @ts-ignore
|
|
109
|
+
rad[corner] = parcer.parse(this.props.size.radius[corner], LazyUtil_1.defaultArg.wh(w / 2, h / 2), LazyUtil_1.defaultArg.vl(false, true));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
105
112
|
let { x, y } = (0, utils_1.centring)(this.props.centring, this.type, w, h, xs, ys);
|
|
106
113
|
let fillStyle = await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle);
|
|
107
114
|
if (debug)
|
|
108
|
-
LazyUtil_1.LazyLog.log('none', `MorphLayer:`, { x, y, w, h,
|
|
115
|
+
LazyUtil_1.LazyLog.log('none', `MorphLayer:`, { x, y, w, h, rad });
|
|
109
116
|
ctx.save();
|
|
110
117
|
(0, utils_1.transform)(ctx, this.props.transform, { width: w, height: h, x, y, type: this.type });
|
|
111
118
|
ctx.beginPath();
|
|
112
|
-
if (
|
|
119
|
+
if (Object.keys(rad).length > 0) {
|
|
113
120
|
ctx.moveTo(x + (w / 2), y);
|
|
114
|
-
ctx.arcTo(x + w, y, x + w, y + (h / 2),
|
|
115
|
-
ctx.arcTo(x + w, y + h, x + (w / 2), y + h,
|
|
116
|
-
ctx.arcTo(x, y + h, x, y + (h / 2),
|
|
117
|
-
ctx.arcTo(x, y, x + (w / 2), y,
|
|
121
|
+
ctx.arcTo(x + w, y, x + w, y + (h / 2), rad.rightTop || rad.all || 0);
|
|
122
|
+
ctx.arcTo(x + w, y + h, x + (w / 2), y + h, rad.rightBottom || rad.all || 0);
|
|
123
|
+
ctx.arcTo(x, y + h, x, y + (h / 2), rad.leftBottom || rad.all || 0);
|
|
124
|
+
ctx.arcTo(x, y, x + (w / 2), y, rad.leftTop || rad.all || 0);
|
|
118
125
|
}
|
|
119
126
|
else {
|
|
120
127
|
ctx.rect(x, y, w, h);
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RenderManager = void 0;
|
|
4
4
|
const types_1 = require("../../types");
|
|
5
|
-
const components_1 = require("../components");
|
|
6
5
|
const LazyUtil_1 = require("../../utils/LazyUtil");
|
|
7
6
|
// @ts-ignore
|
|
8
7
|
const gifenc_1 = require("gifenc");
|
|
@@ -67,28 +66,8 @@ class RenderManager {
|
|
|
67
66
|
if (this.debug)
|
|
68
67
|
LazyUtil_1.LazyLog.log('info', `Rendering ${layer.id}...\nData:`, layer.toJSON());
|
|
69
68
|
if (layer.visible) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
if (subLayer.visible) {
|
|
73
|
-
if ('globalComposite' in subLayer.props && subLayer.props.globalComposite) {
|
|
74
|
-
this.lazyCanvas.ctx.globalCompositeOperation = subLayer.props.globalComposite;
|
|
75
|
-
}
|
|
76
|
-
else {
|
|
77
|
-
this.lazyCanvas.ctx.globalCompositeOperation = 'source-over';
|
|
78
|
-
}
|
|
79
|
-
await subLayer.draw(this.lazyCanvas.ctx, this.lazyCanvas.canvas, this.lazyCanvas.manager.layers, this.debug);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
else {
|
|
84
|
-
if ('globalComposite' in layer.props && layer.props.globalComposite) {
|
|
85
|
-
this.lazyCanvas.ctx.globalCompositeOperation = layer.props.globalComposite;
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
this.lazyCanvas.ctx.globalCompositeOperation = 'source-over';
|
|
89
|
-
}
|
|
90
|
-
await layer.draw(this.lazyCanvas.ctx, this.lazyCanvas.canvas, this.lazyCanvas.manager.layers, this.debug);
|
|
91
|
-
}
|
|
69
|
+
this.lazyCanvas.ctx.globalCompositeOperation = layer.props?.globalComposite || 'source-over';
|
|
70
|
+
await layer.draw(this.lazyCanvas.ctx, this.lazyCanvas.canvas, this.lazyCanvas.manager.layers, this.debug);
|
|
92
71
|
this.lazyCanvas.ctx.shadowColor = 'transparent';
|
|
93
72
|
}
|
|
94
73
|
return this.lazyCanvas.ctx;
|
package/dist/types/types.d.ts
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
1
|
-
import { Gradient } from "../structures/helpers
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
import { Gradient, Link, Pattern } from "../structures/helpers";
|
|
2
|
+
import {
|
|
3
|
+
MorphLayer,
|
|
4
|
+
ImageLayer,
|
|
5
|
+
TextLayer,
|
|
6
|
+
BezierLayer,
|
|
7
|
+
QuadraticLayer,
|
|
8
|
+
LineLayer,
|
|
9
|
+
ClearLayer,
|
|
10
|
+
Path2DLayer,
|
|
11
|
+
IMorphLayer,
|
|
12
|
+
IBezierLayer,
|
|
13
|
+
IClearLayer,
|
|
14
|
+
IImageLayer,
|
|
15
|
+
ITextLayer,
|
|
16
|
+
IQuadraticLayer,
|
|
17
|
+
ILineLayer,
|
|
18
|
+
IPath2DLayer
|
|
19
|
+
} from "../structures/components";
|
|
12
20
|
import {
|
|
13
21
|
FontWeight,
|
|
14
22
|
GradientType,
|
|
@@ -20,17 +28,20 @@ import {
|
|
|
20
28
|
Export,
|
|
21
29
|
Centring,
|
|
22
30
|
PatternType,
|
|
23
|
-
SaveFormat,
|
|
24
31
|
LinkType,
|
|
25
32
|
GlobalCompositeOperation,
|
|
26
|
-
ColorSpace
|
|
33
|
+
ColorSpace,
|
|
27
34
|
} from "./enum";
|
|
28
35
|
|
|
29
|
-
export type ScaleType = string | number | 'vw' | 'vh' | 'vmin' | 'vmax' | Link;
|
|
36
|
+
export type ScaleType = `link-w-${string}-${number}` | `link-h-${string}-${number}` | `link-x-${string}-${number}` | `link-y-${string}-${number}` | `${number}%` | `${number}px` | number | 'vw' | 'vh' | 'vmin' | 'vmax' | Link;
|
|
37
|
+
|
|
38
|
+
export type StringColorType = `rgba(${number}, ${number}, ${number}, ${number})` | `rgb(${number}, ${number}, ${number})` | `hsl(${number}, ${number}%, ${number}%)` | `hsla(${number}, ${number}%, ${number}%, ${number})` | `#${string}` | string;
|
|
30
39
|
|
|
31
|
-
export type ColorType =
|
|
40
|
+
export type ColorType = Gradient | Pattern | StringColorType;
|
|
32
41
|
|
|
33
|
-
export type
|
|
42
|
+
export type JSONLayer = IMorphLayer | IImageLayer | ITextLayer | IBezierLayer | IQuadraticLayer | ILineLayer | IClearLayer | IPath2DLayer;
|
|
43
|
+
|
|
44
|
+
export type AnyLayer = MorphLayer | ImageLayer | TextLayer | BezierLayer | QuadraticLayer | LineLayer | ClearLayer | Path2DLayer;
|
|
34
45
|
|
|
35
46
|
export type AnyWeight = FontWeight | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900 | 950;
|
|
36
47
|
|
|
@@ -46,20 +57,20 @@ export type AnyLineCap = LineCap | 'butt' | 'round' | 'square';
|
|
|
46
57
|
|
|
47
58
|
export type AnyLineJoin = LineJoin | 'bevel' | 'round' | 'miter';
|
|
48
59
|
|
|
49
|
-
export type AnyExport = Export | 'buffer' | 'svg' | '
|
|
60
|
+
export type AnyExport = Export | 'canvas' | 'ctx' | 'buffer' | 'svg' | 'png' | 'jpeg' | 'jpg' | 'gif' | 'webp' | 'yaml' | 'json';
|
|
50
61
|
|
|
51
62
|
export type AnyCentring = Centring | 'start' | 'start-top' | 'start-bottom' | 'center' | 'center-top' | 'center-bottom' | 'end' | 'end-top' | 'end-bottom' | 'none';
|
|
52
63
|
|
|
53
64
|
export type AnyPatternType = PatternType | 'repeat' | 'repeat-x' | 'repeat-y' | 'no-repeat';
|
|
54
65
|
|
|
55
|
-
export type AnySaveFormat = SaveFormat | 'png' | 'jpeg' | 'jpg' | 'svg' | 'gif' | 'webp';
|
|
56
|
-
|
|
57
66
|
export type AnyLinkType = LinkType | 'width' | 'height' | 'x' | 'y';
|
|
58
67
|
|
|
59
68
|
export type AnyGlobalCompositeOperation = GlobalCompositeOperation | 'source-over' | 'source-in' | 'source-out' | 'source-atop' | 'destination-over' | 'destination-in' | 'destination-out' | 'destination-atop' | 'lighter' | 'copy' | 'xor' | 'multiply' | 'screen' | 'overlay' | 'darken' | 'lighten' | 'color-dodge' | 'color-burn' | 'hard-light' | 'soft-light' | 'difference' | 'exclusion' | 'hue' | 'saturation' | 'color' | 'luminosity';
|
|
60
69
|
|
|
61
70
|
export type AnyColorSpace = ColorSpace | 'rgb565' | 'rgba4444' | 'rgba444';
|
|
62
71
|
|
|
72
|
+
export type AnyFilter = `sepia(${number}%)` | `saturate(${number}%)` | `opacity(${number}%)` | `invert(${number}%)` | `hue-rotate(${number}deg)` | `grayscale(${number}%)` | `drop-shadow(${number}px ${number}px ${number}px ${string})` | `contrast(${number}%)` | `brightness(${number}%)` | `blur(${number}px)`;
|
|
73
|
+
|
|
63
74
|
export type Point = {
|
|
64
75
|
x: ScaleType;
|
|
65
76
|
y: ScaleType;
|
|
@@ -68,4 +79,21 @@ export type Point = {
|
|
|
68
79
|
export type PointNumber = {
|
|
69
80
|
x: number;
|
|
70
81
|
y: number;
|
|
71
|
-
};
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export type Extensions = 'svg' | 'png' | 'jpeg' | 'jpg' | 'gif' | 'webp' | 'yaml' | 'json';
|
|
85
|
+
|
|
86
|
+
export interface Transform {
|
|
87
|
+
rotate: number;
|
|
88
|
+
scale: {
|
|
89
|
+
x: number;
|
|
90
|
+
y: number;
|
|
91
|
+
};
|
|
92
|
+
translate: {
|
|
93
|
+
x: number;
|
|
94
|
+
y: number;
|
|
95
|
+
};
|
|
96
|
+
matrix: DOMMatrix2DInit;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export type radiusCorner = 'leftTop' | 'leftBottom' | 'rightTop' | 'rightBottom' | 'all';
|
package/dist/utils/utils.js
CHANGED
|
@@ -24,113 +24,79 @@ function isColor(v) {
|
|
|
24
24
|
}
|
|
25
25
|
exports.isColor = isColor;
|
|
26
26
|
function parseToNormal(v, ctx, canvas, layer = { width: 0, height: 0 }, options = { vertical: false, layer: false }, manager) {
|
|
27
|
-
if (typeof v === 'number')
|
|
27
|
+
if (typeof v === 'number')
|
|
28
28
|
return v;
|
|
29
|
-
|
|
30
|
-
else if (typeof v === 'string') {
|
|
29
|
+
if (typeof v === 'string') {
|
|
31
30
|
if (percentReg.test(v)) {
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
const base = options.layer
|
|
32
|
+
? options.vertical ? layer.height : layer.width
|
|
33
|
+
: options.vertical ? canvas.height : canvas.width;
|
|
34
|
+
return (parseFloat(v) / 100) * base;
|
|
34
35
|
}
|
|
35
|
-
|
|
36
|
+
if (pxReg.test(v))
|
|
36
37
|
return parseFloat(v);
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
return (
|
|
44
|
-
}
|
|
45
|
-
else if (v === 'vmin') {
|
|
46
|
-
return (options.layer ? Math.max(layer.width, layer.height) : Math.min(canvas.width, canvas.height));
|
|
47
|
-
}
|
|
48
|
-
else if (v === 'vmax') {
|
|
49
|
-
return (options.layer ? Math.max(layer.width, layer.height) : Math.max(canvas.width, canvas.height));
|
|
38
|
+
if (canvasReg.test(v)) {
|
|
39
|
+
const base = options.layer ? layer : canvas;
|
|
40
|
+
switch (v) {
|
|
41
|
+
case 'vw': return base.width;
|
|
42
|
+
case 'vh': return base.height;
|
|
43
|
+
case 'vmin': return Math.min(base.width, base.height);
|
|
44
|
+
case 'vmax': return Math.max(base.width, base.height);
|
|
50
45
|
}
|
|
51
46
|
}
|
|
52
|
-
|
|
53
|
-
|
|
47
|
+
if (linkReg.test(v)) {
|
|
48
|
+
const match = v.match(linkReg);
|
|
54
49
|
if (!manager)
|
|
55
50
|
return 0;
|
|
56
|
-
|
|
51
|
+
const anyLayer = manager.get(match[2], true);
|
|
57
52
|
if (!anyLayer || anyLayer instanceof components_1.Group || anyLayer instanceof components_1.Path2DLayer)
|
|
58
53
|
return 0;
|
|
59
|
-
const
|
|
54
|
+
const parserInstance = parser(ctx, canvas, manager);
|
|
55
|
+
const additionalSpacing = parseInt(match[3]) || 0;
|
|
60
56
|
switch (match[1]) {
|
|
61
|
-
case 'link-w':
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
else if (anyLayer instanceof components_1.TextLayer) {
|
|
66
|
-
return anyLayer.measureText(ctx, canvas).width + (parseInt(match[3]) || 0);
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
return (parcer.parse(anyLayer.props.size.width) || 0) + (parseInt(match[3]) || 0);
|
|
70
|
-
}
|
|
71
|
-
case 'link-h':
|
|
72
|
-
if (anyLayer instanceof components_1.LineLayer || anyLayer instanceof components_1.BezierLayer || anyLayer instanceof components_1.QuadraticLayer) {
|
|
73
|
-
return anyLayer.getBoundingBox(ctx, canvas, manager).height + (parseInt(match[3]) || 0);
|
|
74
|
-
}
|
|
75
|
-
else if (anyLayer instanceof components_1.TextLayer) {
|
|
76
|
-
return anyLayer.measureText(ctx, canvas).height + (parseInt(match[3]) || 0);
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
return (parcer.parse(anyLayer.props.size.height, LazyUtil_1.defaultArg.wh(parcer.parse(anyLayer.props.size.width)), LazyUtil_1.defaultArg.vl(true)) || 0) + (parseInt(match[3]) || 0);
|
|
80
|
-
}
|
|
81
|
-
case 'link-x':
|
|
82
|
-
return (parcer.parse(anyLayer.props.x) || 0) + (parseInt(match[3]) || 0);
|
|
83
|
-
case 'link-y':
|
|
84
|
-
return (parcer.parse(anyLayer.props.y, LazyUtil_1.defaultArg.wh(), LazyUtil_1.defaultArg.vl(true)) || 0) + (parseInt(match[3]) || 0);
|
|
57
|
+
case 'link-w': return getLayerWidth(anyLayer, ctx, canvas, manager, parserInstance) + additionalSpacing;
|
|
58
|
+
case 'link-h': return getLayerHeight(anyLayer, ctx, canvas, manager, parserInstance) + additionalSpacing;
|
|
59
|
+
case 'link-x': return parserInstance.parse(anyLayer.props.x) + additionalSpacing;
|
|
60
|
+
case 'link-y': return parserInstance.parse(anyLayer.props.y) + additionalSpacing;
|
|
85
61
|
}
|
|
86
62
|
}
|
|
87
63
|
}
|
|
88
|
-
|
|
64
|
+
if (v instanceof helpers_1.Link) {
|
|
89
65
|
if (!manager)
|
|
90
66
|
return 0;
|
|
91
|
-
|
|
67
|
+
const anyLayer = manager.get(v.source, true);
|
|
92
68
|
if (!anyLayer || anyLayer instanceof components_1.Group || anyLayer instanceof components_1.Path2DLayer)
|
|
93
69
|
return 0;
|
|
94
|
-
const
|
|
70
|
+
const parserInstance = parser(ctx, canvas, manager);
|
|
71
|
+
const additionalSpacing = parserInstance.parse(v.additionalSpacing, LazyUtil_1.defaultArg.wh(layer.width, layer.height), LazyUtil_1.defaultArg.vl(options.vertical, options.layer)) || 0;
|
|
95
72
|
switch (v.type) {
|
|
96
|
-
case types_1.LinkType.Width:
|
|
97
|
-
case
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
else if (anyLayer instanceof components_1.TextLayer) {
|
|
102
|
-
return anyLayer.measureText(ctx, canvas).width + (parcer.parse(v.additionalSpacing, LazyUtil_1.defaultArg.wh(layer.width, layer.height), LazyUtil_1.defaultArg.vl(options.vertical, options.layer)) || 0);
|
|
103
|
-
}
|
|
104
|
-
else {
|
|
105
|
-
return (parcer.parse(anyLayer.props.size.width) || 0) + (parcer.parse(v.additionalSpacing, LazyUtil_1.defaultArg.wh(layer.width, layer.height), LazyUtil_1.defaultArg.vl(options.vertical, options.layer)) || 0);
|
|
106
|
-
}
|
|
107
|
-
case types_1.LinkType.Height:
|
|
108
|
-
case 'height':
|
|
109
|
-
if (anyLayer instanceof components_1.LineLayer || anyLayer instanceof components_1.BezierLayer || anyLayer instanceof components_1.QuadraticLayer) {
|
|
110
|
-
return anyLayer.getBoundingBox(ctx, canvas, manager).height + (parcer.parse(v.additionalSpacing, LazyUtil_1.defaultArg.wh(layer.width, layer.height), LazyUtil_1.defaultArg.vl(options.vertical, options.layer)) || 0);
|
|
111
|
-
}
|
|
112
|
-
else if (anyLayer instanceof components_1.TextLayer) {
|
|
113
|
-
return anyLayer.measureText(ctx, canvas).height + (parcer.parse(v.additionalSpacing, LazyUtil_1.defaultArg.wh(layer.width, layer.height), LazyUtil_1.defaultArg.vl(options.vertical, options.layer)) || 0);
|
|
114
|
-
}
|
|
115
|
-
else {
|
|
116
|
-
return (parcer.parse(anyLayer.props.size.height, LazyUtil_1.defaultArg.wh(parcer.parse(anyLayer.props.size.width)), LazyUtil_1.defaultArg.vl(true)) || 0) + (parcer.parse(v.additionalSpacing, LazyUtil_1.defaultArg.wh(layer.width, layer.height), LazyUtil_1.defaultArg.vl(options.vertical, options.layer)) || 0);
|
|
117
|
-
}
|
|
118
|
-
case types_1.LinkType.X:
|
|
119
|
-
case 'x':
|
|
120
|
-
return (parcer.parse(anyLayer.props.x) || 0) + (parcer.parse(v.additionalSpacing, LazyUtil_1.defaultArg.wh(layer.width, layer.height), LazyUtil_1.defaultArg.vl(options.vertical, options.layer)) || 0);
|
|
121
|
-
case types_1.LinkType.Y:
|
|
122
|
-
case 'y':
|
|
123
|
-
return (parcer.parse(anyLayer.props.y) || 0) + (parcer.parse(v.additionalSpacing, LazyUtil_1.defaultArg.wh(layer.width, layer.height), LazyUtil_1.defaultArg.vl(options.vertical, options.layer)) || 0);
|
|
124
|
-
default:
|
|
125
|
-
return 0;
|
|
73
|
+
case types_1.LinkType.Width: return getLayerWidth(anyLayer, ctx, canvas, manager, parserInstance) + additionalSpacing;
|
|
74
|
+
case types_1.LinkType.Height: return getLayerHeight(anyLayer, ctx, canvas, manager, parserInstance) + additionalSpacing;
|
|
75
|
+
case types_1.LinkType.X: return parserInstance.parse(anyLayer.props.x) + additionalSpacing;
|
|
76
|
+
case types_1.LinkType.Y: return parserInstance.parse(anyLayer.props.y) + additionalSpacing;
|
|
126
77
|
}
|
|
127
78
|
}
|
|
128
|
-
else {
|
|
129
|
-
return 0;
|
|
130
|
-
}
|
|
131
79
|
return 0;
|
|
132
80
|
}
|
|
133
81
|
exports.parseToNormal = parseToNormal;
|
|
82
|
+
function getLayerWidth(anyLayer, ctx, canvas, manager, parserInstance) {
|
|
83
|
+
if (anyLayer instanceof components_1.LineLayer || anyLayer instanceof components_1.BezierLayer || anyLayer instanceof components_1.QuadraticLayer) {
|
|
84
|
+
return anyLayer.getBoundingBox(ctx, canvas, manager).width;
|
|
85
|
+
}
|
|
86
|
+
if (anyLayer instanceof components_1.TextLayer) {
|
|
87
|
+
return anyLayer.measureText(ctx, canvas).width;
|
|
88
|
+
}
|
|
89
|
+
return anyLayer instanceof components_1.Path2DLayer ? 0 : parserInstance.parse(anyLayer.props.size.width) || 0;
|
|
90
|
+
}
|
|
91
|
+
function getLayerHeight(anyLayer, ctx, canvas, manager, parserInstance) {
|
|
92
|
+
if (anyLayer instanceof components_1.LineLayer || anyLayer instanceof components_1.BezierLayer || anyLayer instanceof components_1.QuadraticLayer) {
|
|
93
|
+
return anyLayer.getBoundingBox(ctx, canvas, manager).height;
|
|
94
|
+
}
|
|
95
|
+
if (anyLayer instanceof components_1.TextLayer) {
|
|
96
|
+
return anyLayer.measureText(ctx, canvas).height;
|
|
97
|
+
}
|
|
98
|
+
return anyLayer instanceof components_1.Path2DLayer ? 0 : parserInstance.parse(anyLayer.props.size.height) || 0;
|
|
99
|
+
}
|
|
134
100
|
function parser(ctx, canvas, manager) {
|
|
135
101
|
return {
|
|
136
102
|
parse(v, layer = LazyUtil_1.defaultArg.wh(), options = LazyUtil_1.defaultArg.vl()) {
|
|
@@ -403,7 +369,10 @@ function resizeLayers(layers, ratio) {
|
|
|
403
369
|
layer.props.size.width = resize(layer.props.size.width, ratio);
|
|
404
370
|
layer.props.size.height = resize(layer.props.size.height, ratio);
|
|
405
371
|
if ('radius' in layer.props.size) {
|
|
406
|
-
|
|
372
|
+
for (const corner in layer.props.size.radius) {
|
|
373
|
+
// @ts-ignore
|
|
374
|
+
layer.props.size.radius[corner] = resize(layer.props.size.radius[corner], ratio);
|
|
375
|
+
}
|
|
407
376
|
}
|
|
408
377
|
}
|
|
409
378
|
if ('stroke' in layer.props) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nmmty/lazycanvas",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0-dev.1a1f37",
|
|
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",
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"text": "tsc ./test/text.ts && node ./test/text.js",
|
|
12
12
|
"animation": "tsc ./test/animation.ts && node ./test/animation.js",
|
|
13
13
|
"iotest": "tsc ./test/iotest.ts && node ./test/iotest.js",
|
|
14
|
+
"gradient": "tsc ./test/gradient.ts && node ./test/gradient.js",
|
|
14
15
|
"doc": "tsx docgen.ts",
|
|
15
16
|
"font": "tsc ./scripts/font-gen.ts && node ./scripts/font-gen.js",
|
|
16
17
|
"build": "tsc"
|
|
@@ -33,7 +34,7 @@
|
|
|
33
34
|
},
|
|
34
35
|
"homepage": "https://github.com/NMMTY/LazyCanvas#readme",
|
|
35
36
|
"dependencies": {
|
|
36
|
-
"@napi-rs/canvas": "^0.1.
|
|
37
|
+
"@napi-rs/canvas": "^0.1.72",
|
|
37
38
|
"gifenc": "^1.0.3",
|
|
38
39
|
"js-yaml": "^4.1.0",
|
|
39
40
|
"path": "^0.12.7",
|