@nmmty/lazycanvas 0.5.3 → 0.6.0-dev.321c2e
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/index.d.ts +1 -1
- package/dist/index.js +2 -1
- package/dist/structures/components/BaseLayer.d.ts +3 -2
- package/dist/structures/components/BaseLayer.js +15 -10
- package/dist/structures/components/BezierLayer.d.ts +6 -0
- package/dist/structures/components/BezierLayer.js +23 -4
- package/dist/structures/components/ClearLayer.d.ts +1 -0
- package/dist/structures/components/ClearLayer.js +14 -8
- package/dist/structures/components/ImageLayer.d.ts +6 -0
- package/dist/structures/components/ImageLayer.js +21 -5
- package/dist/structures/components/LineLayer.d.ts +6 -0
- package/dist/structures/components/LineLayer.js +27 -6
- package/dist/structures/components/MorphLayer.d.ts +6 -0
- package/dist/structures/components/MorphLayer.js +25 -6
- package/dist/structures/components/Path2DLayer.d.ts +6 -0
- package/dist/structures/components/Path2DLayer.js +23 -1
- package/dist/structures/components/QuadraticLayer.d.ts +6 -0
- package/dist/structures/components/QuadraticLayer.js +23 -4
- package/dist/structures/components/TextLayer.d.ts +11 -5
- package/dist/structures/components/TextLayer.js +35 -23
- package/dist/structures/helpers/Gradient.d.ts +48 -11
- package/dist/structures/helpers/Gradient.js +133 -8
- package/dist/structures/helpers/index.d.ts +1 -0
- package/dist/structures/helpers/index.js +1 -0
- package/dist/structures/helpers/readers/JSONReader.js +3 -3
- package/dist/structures/helpers/readers/YAMLReader.d.ts +22 -0
- package/dist/structures/helpers/readers/YAMLReader.js +73 -0
- package/dist/utils/utils.d.ts +13 -3
- package/dist/utils/utils.js +59 -92
- package/package.json +5 -4
- package/test.png +0 -0
- package/test.yaml +1780 -0
- package/animation.gif +0 -0
package/dist/utils/utils.js
CHANGED
|
@@ -13,7 +13,7 @@ exports.generateID = generateID;
|
|
|
13
13
|
let percentReg = /^(\d+)%$/;
|
|
14
14
|
let pxReg = /^(\d+)px$/;
|
|
15
15
|
let canvasReg = /^(vw|vh|vmin|vmax)$/;
|
|
16
|
-
let linkReg = /^(link-w|link-h|link-x|link-y)-([A-Za-z0-9_]+)-(\d+)$/;
|
|
16
|
+
let linkReg = /^(link-w|link-h|link-x|link-y)-([A-Za-z0-9_]+)-(\d+(.\d+)?)$/;
|
|
17
17
|
let hexReg = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
|
|
18
18
|
let rgbReg = /^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/;
|
|
19
19
|
let rgbaReg = /^rgba\((\d+),\s*(\d+),\s*(\d+),\s*(0|0?\.\d+|1(\.0)?)\)$/;
|
|
@@ -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()) {
|
|
@@ -156,7 +122,7 @@ function drawShadow(ctx, shadow) {
|
|
|
156
122
|
}
|
|
157
123
|
}
|
|
158
124
|
exports.drawShadow = drawShadow;
|
|
159
|
-
function opacity(ctx, opacity) {
|
|
125
|
+
function opacity(ctx, opacity = 1) {
|
|
160
126
|
if (opacity < 1) {
|
|
161
127
|
ctx.globalAlpha = opacity;
|
|
162
128
|
}
|
|
@@ -168,13 +134,13 @@ function filters(ctx, filters) {
|
|
|
168
134
|
}
|
|
169
135
|
}
|
|
170
136
|
exports.filters = filters;
|
|
171
|
-
function parseFillStyle(ctx, color) {
|
|
137
|
+
function parseFillStyle(ctx, color, opts) {
|
|
172
138
|
if (!ctx)
|
|
173
139
|
throw new LazyUtil_1.LazyError('The context is not defined');
|
|
174
140
|
if (!color)
|
|
175
141
|
throw new LazyUtil_1.LazyError('The color is not defined');
|
|
176
142
|
if (color instanceof helpers_1.Gradient || color instanceof helpers_1.Pattern) {
|
|
177
|
-
return color.draw(ctx);
|
|
143
|
+
return color.draw(ctx, opts);
|
|
178
144
|
}
|
|
179
145
|
return color;
|
|
180
146
|
}
|
|
@@ -266,6 +232,7 @@ function centring(centring, type, width, height, x, y) {
|
|
|
266
232
|
case types_1.LayerType.Morph:
|
|
267
233
|
case types_1.LayerType.Clear:
|
|
268
234
|
x -= width / 2;
|
|
235
|
+
y -= height;
|
|
269
236
|
break;
|
|
270
237
|
}
|
|
271
238
|
return { x, y };
|
|
@@ -292,9 +259,6 @@ function centring(centring, type, width, height, x, y) {
|
|
|
292
259
|
return { x, y };
|
|
293
260
|
case types_1.Centring.StartBottom:
|
|
294
261
|
case "start-bottom":
|
|
295
|
-
return { x, y };
|
|
296
|
-
case types_1.Centring.StartTop:
|
|
297
|
-
case "start-top":
|
|
298
262
|
switch (type) {
|
|
299
263
|
case types_1.LayerType.Image:
|
|
300
264
|
case types_1.LayerType.Morph:
|
|
@@ -303,6 +267,9 @@ function centring(centring, type, width, height, x, y) {
|
|
|
303
267
|
break;
|
|
304
268
|
}
|
|
305
269
|
return { x, y };
|
|
270
|
+
case types_1.Centring.StartTop:
|
|
271
|
+
case "start-top":
|
|
272
|
+
return { x, y };
|
|
306
273
|
case types_1.Centring.End:
|
|
307
274
|
case "end":
|
|
308
275
|
switch (type) {
|
|
@@ -321,6 +288,7 @@ function centring(centring, type, width, height, x, y) {
|
|
|
321
288
|
case types_1.LayerType.Morph:
|
|
322
289
|
case types_1.LayerType.Clear:
|
|
323
290
|
x -= width;
|
|
291
|
+
y -= height;
|
|
324
292
|
break;
|
|
325
293
|
}
|
|
326
294
|
return { x, y };
|
|
@@ -331,7 +299,6 @@ function centring(centring, type, width, height, x, y) {
|
|
|
331
299
|
case types_1.LayerType.Morph:
|
|
332
300
|
case types_1.LayerType.Clear:
|
|
333
301
|
x -= width;
|
|
334
|
-
y -= height;
|
|
335
302
|
break;
|
|
336
303
|
}
|
|
337
304
|
return { x, y };
|
|
@@ -409,7 +376,7 @@ function resizeLayers(layers, ratio) {
|
|
|
409
376
|
}
|
|
410
377
|
}
|
|
411
378
|
}
|
|
412
|
-
if ('stroke' in layer.props) {
|
|
379
|
+
if ('stroke' in layer.props && layer.props.stroke) {
|
|
413
380
|
layer.props.stroke.width = resize(layer.props.stroke.width, ratio);
|
|
414
381
|
}
|
|
415
382
|
if ('endPoint' 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.321c2e",
|
|
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,9 +11,10 @@
|
|
|
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
|
-
"font": "
|
|
16
|
-
"build": "tsc"
|
|
16
|
+
"font": "tsx ./scripts/font-gen.ts",
|
|
17
|
+
"build": "tsc && tsx ./scripts/post-build.ts"
|
|
17
18
|
},
|
|
18
19
|
"repository": {
|
|
19
20
|
"type": "git",
|
|
@@ -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",
|
package/test.png
CHANGED
|
Binary file
|