xpict 0.3.4 → 0.4.0
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/error.d.ts +3 -0
- package/dist/error.js +10 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/layers/group-layer.d.ts +2 -2
- package/dist/layers/group-layer.js +3 -2
- package/dist/layers/image-layer.js +26 -18
- package/package.json +1 -1
package/dist/error.d.ts
ADDED
package/dist/error.js
ADDED
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export * from "./layers";
|
|
|
5
5
|
export * from "./effects";
|
|
6
6
|
export * from "./template";
|
|
7
7
|
export * from "./utils";
|
|
8
|
+
export * from "./error";
|
|
8
9
|
declare const xpict: {
|
|
9
10
|
rectangle<Data>(options: RectangleLayerOptions<Data>): RectangleLayer<Data>;
|
|
10
11
|
circle<Data>(options: CircleLayerOptions<Data>): CircleLayer<Data>;
|
package/dist/index.js
CHANGED
|
@@ -21,6 +21,7 @@ __exportStar(require("./layers"), exports);
|
|
|
21
21
|
__exportStar(require("./effects"), exports);
|
|
22
22
|
__exportStar(require("./template"), exports);
|
|
23
23
|
__exportStar(require("./utils"), exports);
|
|
24
|
+
__exportStar(require("./error"), exports);
|
|
24
25
|
const xpict = {
|
|
25
26
|
rectangle(options) {
|
|
26
27
|
return new layers_1.RectangleLayer(options);
|
|
@@ -2,8 +2,8 @@ import { Layer, RenderOptions, WhenOptions } from "./layer";
|
|
|
2
2
|
import { Axis } from "../utils/resolve-axis";
|
|
3
3
|
export type GroupLayerOptions<Data> = {
|
|
4
4
|
layers: Layer<Data>[];
|
|
5
|
-
x
|
|
6
|
-
y
|
|
5
|
+
x?: Axis<Data>;
|
|
6
|
+
y?: Axis<Data>;
|
|
7
7
|
when?: (options: WhenOptions<Data>) => boolean;
|
|
8
8
|
};
|
|
9
9
|
export declare class GroupLayer<Data> extends Layer<Data> {
|
|
@@ -10,14 +10,15 @@ class GroupLayer extends layer_1.Layer {
|
|
|
10
10
|
this.options = options;
|
|
11
11
|
}
|
|
12
12
|
async render({ context: ctx, data, index = 0, templateConfig }) {
|
|
13
|
+
var _a, _b;
|
|
13
14
|
const dx = (0, resolve_axis_1.resolveAxis)({
|
|
14
|
-
axis: this.options.x,
|
|
15
|
+
axis: (_a = this.options.x) !== null && _a !== void 0 ? _a : 0,
|
|
15
16
|
data: data,
|
|
16
17
|
index: index,
|
|
17
18
|
templateConfig: templateConfig,
|
|
18
19
|
});
|
|
19
20
|
const dy = (0, resolve_axis_1.resolveAxis)({
|
|
20
|
-
axis: this.options.y,
|
|
21
|
+
axis: (_b = this.options.y) !== null && _b !== void 0 ? _b : 0,
|
|
21
22
|
data: data,
|
|
22
23
|
index: index,
|
|
23
24
|
templateConfig: templateConfig,
|
|
@@ -7,6 +7,7 @@ exports.ImageLayer = void 0;
|
|
|
7
7
|
const sharp_1 = __importDefault(require("sharp"));
|
|
8
8
|
const layer_1 = require("./layer");
|
|
9
9
|
const resolve_axis_1 = require("../utils/resolve-axis");
|
|
10
|
+
const error_1 = require("../error");
|
|
10
11
|
class ImageLayer extends layer_1.Layer {
|
|
11
12
|
constructor(options) {
|
|
12
13
|
super(options.when);
|
|
@@ -29,27 +30,34 @@ class ImageLayer extends layer_1.Layer {
|
|
|
29
30
|
const y = ctx.offsetY + localY;
|
|
30
31
|
const src = this.options.src;
|
|
31
32
|
const resolvedImageSource = typeof src === "string" ? src : src({ data: data, index: index });
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
for (const transform of this.options.transform) {
|
|
44
|
-
img = await transform({
|
|
45
|
-
data: data,
|
|
46
|
-
index: index,
|
|
47
|
-
image: img,
|
|
33
|
+
try {
|
|
34
|
+
let img = (0, sharp_1.default)(resolvedImageSource).resize(this.options.width, this.options.height);
|
|
35
|
+
if (this.options.flipX) {
|
|
36
|
+
img = img.flop();
|
|
37
|
+
}
|
|
38
|
+
if (this.options.flipY) {
|
|
39
|
+
img = img.flip();
|
|
40
|
+
}
|
|
41
|
+
if (this.options.rotate !== undefined) {
|
|
42
|
+
img = img.rotate(this.options.rotate, {
|
|
43
|
+
background: { r: 0, g: 0, b: 0, alpha: 0 },
|
|
48
44
|
});
|
|
49
45
|
}
|
|
46
|
+
if (this.options.transform && this.options.transform.length > 0) {
|
|
47
|
+
for (const transform of this.options.transform) {
|
|
48
|
+
img = await transform({
|
|
49
|
+
data: data,
|
|
50
|
+
index: index,
|
|
51
|
+
image: img,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const buffer = await img.toBuffer();
|
|
56
|
+
ctx.image = ctx.image.composite([{ input: buffer, left: x, top: y }]);
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
throw new error_1.XpictError(`Failed to render image layer (${resolvedImageSource}): ${error.message}`);
|
|
50
60
|
}
|
|
51
|
-
const buffer = await img.toBuffer();
|
|
52
|
-
ctx.image = ctx.image.composite([{ input: buffer, left: x, top: y }]);
|
|
53
61
|
}
|
|
54
62
|
}
|
|
55
63
|
exports.ImageLayer = ImageLayer;
|
package/package.json
CHANGED