xpict 1.0.1 → 1.0.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.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/layers/image-layer.d.ts +3 -2
- package/dist/layers/image-layer.js +48 -9
- package/dist/layers/line-layer.d.ts +2 -1
- package/dist/layers/line-layer.js +8 -1
- package/dist/layers/rectangle-layer.d.ts +3 -2
- package/dist/layers/rectangle-layer.js +14 -1
- package/dist/utils/resolve-dimension.d.ts +12 -0
- package/dist/utils/resolve-dimension.js +9 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { GroupLayer, RepeatLayer, GroupLayerOptions, ImageLayer, ImageLayerOptions, LineLayer, LineLayerOptions, RectangleLayer, RectangleLayerOptions, RepeatLayerOptions, TextLayer, TextLayerOptions, CircleLayerOptions, CircleLayer } from "./layers";
|
|
2
2
|
import { Template, InputTemplateOptions } from "./template";
|
|
3
|
-
import * as utils from "./utils";
|
|
4
3
|
import * as effects from "./effects";
|
|
4
|
+
import * as utils from "./utils";
|
|
5
5
|
export * from "./effects";
|
|
6
6
|
export * from "./layers";
|
|
7
7
|
export * from "./template";
|
package/dist/index.js
CHANGED
|
@@ -38,8 +38,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
39
|
const layers_1 = require("./layers");
|
|
40
40
|
const template_1 = require("./template");
|
|
41
|
-
const utils = __importStar(require("./utils"));
|
|
42
41
|
const effects = __importStar(require("./effects"));
|
|
42
|
+
const utils = __importStar(require("./utils"));
|
|
43
43
|
__exportStar(require("./effects"), exports);
|
|
44
44
|
__exportStar(require("./layers"), exports);
|
|
45
45
|
__exportStar(require("./template"), exports);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Canvas, SKRSContext2D } from "@napi-rs/canvas";
|
|
2
|
+
import { Dimension } from "../utils/resolve-dimension";
|
|
2
3
|
import { Layer, RenderOptions, WhenFunction } from "./layer";
|
|
3
4
|
import { Axis } from "../utils/resolve-axis";
|
|
4
5
|
export type TransformOptions<Data> = {
|
|
@@ -17,8 +18,8 @@ export type ImageLayerOptions<Data> = {
|
|
|
17
18
|
src: string | ImageSrcFunction<Data>;
|
|
18
19
|
x: Axis<Data>;
|
|
19
20
|
y: Axis<Data>;
|
|
20
|
-
width?:
|
|
21
|
-
height?:
|
|
21
|
+
width?: Dimension<Data>;
|
|
22
|
+
height?: Dimension<Data>;
|
|
22
23
|
flipX?: boolean;
|
|
23
24
|
flipY?: boolean;
|
|
24
25
|
rotate?: number;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ImageLayer = void 0;
|
|
4
4
|
const canvas_1 = require("@napi-rs/canvas");
|
|
5
|
+
const resolve_dimension_1 = require("../utils/resolve-dimension");
|
|
5
6
|
const layer_1 = require("./layer");
|
|
6
7
|
const resolve_axis_1 = require("../utils/resolve-axis");
|
|
7
8
|
const error_1 = require("../error");
|
|
@@ -23,23 +24,60 @@ class ImageLayer extends layer_1.Layer {
|
|
|
23
24
|
index: index,
|
|
24
25
|
templateSize: templateConfig,
|
|
25
26
|
});
|
|
27
|
+
const width = this.options.width
|
|
28
|
+
? (0, resolve_dimension_1.resolveDimension)({
|
|
29
|
+
data: data,
|
|
30
|
+
dimension: this.options.width,
|
|
31
|
+
index: index,
|
|
32
|
+
templateSize: templateConfig,
|
|
33
|
+
})
|
|
34
|
+
: this.options.width;
|
|
35
|
+
const height = this.options.height
|
|
36
|
+
? (0, resolve_dimension_1.resolveDimension)({
|
|
37
|
+
data: data,
|
|
38
|
+
dimension: this.options.height,
|
|
39
|
+
index: index,
|
|
40
|
+
templateSize: templateConfig,
|
|
41
|
+
})
|
|
42
|
+
: this.options.height;
|
|
26
43
|
const x = renderContext.offsetX + localX;
|
|
27
44
|
const y = renderContext.offsetY + localY;
|
|
28
45
|
const src = this.options.src;
|
|
29
46
|
const resolvedImageSource = typeof src === "string" ? src : src({ data: data, index: index });
|
|
30
47
|
const image = await (0, canvas_1.loadImage)(resolvedImageSource);
|
|
31
|
-
|
|
48
|
+
let finalWidth;
|
|
49
|
+
let finalHeight;
|
|
50
|
+
if (width && height) {
|
|
51
|
+
finalWidth = width;
|
|
52
|
+
finalHeight = height;
|
|
53
|
+
}
|
|
54
|
+
else if (width) {
|
|
55
|
+
finalWidth = width;
|
|
56
|
+
finalHeight = (width / image.width) * image.height;
|
|
57
|
+
}
|
|
58
|
+
else if (height) {
|
|
59
|
+
finalHeight = height;
|
|
60
|
+
finalWidth = (height / image.height) * image.width;
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
finalWidth = image.width;
|
|
64
|
+
finalHeight = image.height;
|
|
65
|
+
}
|
|
66
|
+
const localCanvas = (0, canvas_1.createCanvas)(finalWidth, finalHeight);
|
|
32
67
|
const localContext = localCanvas.getContext("2d");
|
|
33
68
|
try {
|
|
34
|
-
|
|
35
|
-
if (
|
|
36
|
-
localContext.translate(
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
69
|
+
const hasRotaion = this.options.flipX || this.options.flipY || this.options.rotate !== undefined;
|
|
70
|
+
if (hasRotaion) {
|
|
71
|
+
localContext.translate(finalWidth / 2, finalHeight / 2);
|
|
72
|
+
if (this.options.rotate !== undefined) {
|
|
73
|
+
localContext.rotate((this.options.rotate * Math.PI) / 180);
|
|
74
|
+
}
|
|
75
|
+
const scaleX = this.options.flipX ? -1 : 1;
|
|
76
|
+
const scaleY = this.options.flipY ? -1 : 1;
|
|
77
|
+
localContext.scale(scaleX, scaleY);
|
|
78
|
+
localContext.translate(-finalWidth / 2, -finalHeight / 2);
|
|
42
79
|
}
|
|
80
|
+
localContext.drawImage(image, 0, 0, finalWidth, finalHeight);
|
|
43
81
|
if (this.options.rotate !== undefined) {
|
|
44
82
|
localContext.translate(image.width / 2, image.height / 2);
|
|
45
83
|
localContext.rotate((this.options.rotate * Math.PI) / 180);
|
|
@@ -55,6 +93,7 @@ class ImageLayer extends layer_1.Layer {
|
|
|
55
93
|
});
|
|
56
94
|
}
|
|
57
95
|
}
|
|
96
|
+
localContext.restore();
|
|
58
97
|
const buffer = localCanvas.toBuffer("image/png");
|
|
59
98
|
const finalImage = await (0, canvas_1.loadImage)(buffer);
|
|
60
99
|
renderContext.ctx.drawImage(finalImage, x, y);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Dimension } from "../utils/resolve-dimension";
|
|
1
2
|
import { Layer, RenderOptions, WhenFunction } from "./layer";
|
|
2
3
|
import { Axis } from "../utils/resolve-axis";
|
|
3
4
|
export type LineLayerOptions<Data> = {
|
|
@@ -10,7 +11,7 @@ export type LineLayerOptions<Data> = {
|
|
|
10
11
|
y: Axis<Data>;
|
|
11
12
|
};
|
|
12
13
|
color: string;
|
|
13
|
-
width:
|
|
14
|
+
width: Dimension<Data>;
|
|
14
15
|
when?: WhenFunction<Data>;
|
|
15
16
|
};
|
|
16
17
|
export declare class LineLayer<Data> extends Layer<Data> {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LineLayer = void 0;
|
|
4
|
+
const resolve_dimension_1 = require("../utils/resolve-dimension");
|
|
4
5
|
const layer_1 = require("./layer");
|
|
5
6
|
const resolve_axis_1 = require("../utils/resolve-axis");
|
|
6
7
|
class LineLayer extends layer_1.Layer {
|
|
@@ -37,9 +38,15 @@ class LineLayer extends layer_1.Layer {
|
|
|
37
38
|
index: index,
|
|
38
39
|
templateSize: templateConfig,
|
|
39
40
|
});
|
|
41
|
+
const width = (0, resolve_dimension_1.resolveDimension)({
|
|
42
|
+
data: data,
|
|
43
|
+
dimension: this.options.width,
|
|
44
|
+
index: index,
|
|
45
|
+
templateSize: templateConfig,
|
|
46
|
+
});
|
|
40
47
|
const context = renderContext.ctx;
|
|
41
48
|
context.strokeStyle = this.options.color;
|
|
42
|
-
context.lineWidth =
|
|
49
|
+
context.lineWidth = width;
|
|
43
50
|
context.lineCap = "round";
|
|
44
51
|
context.beginPath();
|
|
45
52
|
context.moveTo(x1, y1);
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import { Dimension } from "../utils/resolve-dimension";
|
|
1
2
|
import { Layer, RenderOptions, WhenFunction } from "./layer";
|
|
2
3
|
import { Axis } from "../utils/resolve-axis";
|
|
3
4
|
export type RectangleLayerOptions<Data> = {
|
|
4
5
|
x: Axis<Data>;
|
|
5
6
|
y: Axis<Data>;
|
|
6
|
-
width:
|
|
7
|
-
height:
|
|
7
|
+
width: Dimension<Data>;
|
|
8
|
+
height: Dimension<Data>;
|
|
8
9
|
fill: string;
|
|
9
10
|
borderRadius?: number;
|
|
10
11
|
when?: WhenFunction<Data>;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RectangleLayer = void 0;
|
|
4
|
+
const resolve_dimension_1 = require("../utils/resolve-dimension");
|
|
4
5
|
const layer_1 = require("./layer");
|
|
5
6
|
const resolve_axis_1 = require("../utils/resolve-axis");
|
|
6
7
|
class RectangleLayer extends layer_1.Layer {
|
|
@@ -23,7 +24,19 @@ class RectangleLayer extends layer_1.Layer {
|
|
|
23
24
|
});
|
|
24
25
|
const x = renderContext.offsetX + localX;
|
|
25
26
|
const y = renderContext.offsetY + localY;
|
|
26
|
-
const {
|
|
27
|
+
const { fill, borderRadius } = this.options;
|
|
28
|
+
const width = (0, resolve_dimension_1.resolveDimension)({
|
|
29
|
+
data: data,
|
|
30
|
+
dimension: this.options.width,
|
|
31
|
+
index: index,
|
|
32
|
+
templateSize: templateConfig,
|
|
33
|
+
});
|
|
34
|
+
const height = (0, resolve_dimension_1.resolveDimension)({
|
|
35
|
+
data: data,
|
|
36
|
+
dimension: this.options.height,
|
|
37
|
+
index: index,
|
|
38
|
+
templateSize: templateConfig,
|
|
39
|
+
});
|
|
27
40
|
const context = renderContext.ctx;
|
|
28
41
|
context.fillStyle = fill;
|
|
29
42
|
if (borderRadius && borderRadius > 0) {
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { TemplateSize } from "../template";
|
|
2
|
+
export type ComputeDimensionOptions<Data> = {
|
|
3
|
+
data: Data;
|
|
4
|
+
index: number;
|
|
5
|
+
templateSize: TemplateSize;
|
|
6
|
+
};
|
|
7
|
+
export type ComputeDimension<Data> = (options: ComputeDimensionOptions<Data>) => number;
|
|
8
|
+
export type Dimension<Data> = ComputeDimension<Data> | number;
|
|
9
|
+
export type ResolveDimensionOptions<Data> = ComputeDimensionOptions<Data> & {
|
|
10
|
+
dimension: Dimension<Data>;
|
|
11
|
+
};
|
|
12
|
+
export declare function resolveDimension<Data>({ dimension, ...params }: ResolveDimensionOptions<Data>): number;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveDimension = resolveDimension;
|
|
4
|
+
function resolveDimension({ dimension, ...params }) {
|
|
5
|
+
if (typeof dimension === "function") {
|
|
6
|
+
return dimension(params);
|
|
7
|
+
}
|
|
8
|
+
return dimension;
|
|
9
|
+
}
|
package/package.json
CHANGED