xpict 0.4.1 → 1.0.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.
- package/README.md +272 -17
- package/dist/effects/blur.d.ts +2 -3
- package/dist/effects/blur.js +14 -4
- package/dist/effects/box-blur.d.ts +2 -0
- package/dist/effects/box-blur.js +61 -0
- package/dist/effects/grayscale.d.ts +2 -2
- package/dist/effects/grayscale.js +14 -4
- package/dist/effects/index.d.ts +1 -1
- package/dist/effects/index.js +1 -1
- package/dist/enums/index.d.ts +2 -0
- package/dist/enums/index.js +18 -0
- package/dist/enums/text-align.d.ts +5 -0
- package/dist/enums/text-align.js +9 -0
- package/dist/enums/text-baseline.d.ts +8 -0
- package/dist/enums/text-baseline.js +12 -0
- package/dist/index.d.ts +5 -7
- package/dist/index.js +27 -7
- package/dist/layers/circle-layer.d.ts +1 -1
- package/dist/layers/circle-layer.js +6 -20
- package/dist/layers/group-layer.js +2 -4
- package/dist/layers/image-layer.d.ts +5 -4
- package/dist/layers/image-layer.js +23 -19
- package/dist/layers/line-layer.d.ts +1 -1
- package/dist/layers/line-layer.js +10 -24
- package/dist/layers/rectangle-layer.d.ts +1 -1
- package/dist/layers/rectangle-layer.js +6 -20
- package/dist/layers/repeat-layer.js +2 -4
- package/dist/layers/text-layer.d.ts +7 -15
- package/dist/layers/text-layer.js +8 -42
- package/dist/render-context.d.ts +2 -2
- package/dist/template.d.ts +6 -4
- package/dist/template.js +22 -30
- package/dist/utils/font.d.ts +17 -0
- package/dist/utils/font.js +21 -0
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +1 -1
- package/dist/utils/resolve-axis.d.ts +3 -5
- package/dist/utils/value-of-enum.d.ts +1 -0
- package/dist/utils/value-of-enum.js +2 -0
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -1,35 +1,290 @@
|
|
|
1
1
|
# Xpict
|
|
2
2
|
|
|
3
|
-
Xpict
|
|
3
|
+
**Xpict** is a library built on top of `@napi-rs/canvas` designed to create templates for generating standardized images with data—which can be static or dynamic. It features support for conditional rendering, runtime-generated position axes, and dynamic text.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## 📦 Installation
|
|
6
|
+
|
|
7
|
+
Installation is straightforward; just use your preferred package manager. Here is an example using NPM:
|
|
6
8
|
|
|
7
9
|
```bash
|
|
8
|
-
npm
|
|
10
|
+
npm i xpict
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## 🚀 Usage
|
|
15
|
+
|
|
16
|
+
<a href="https://www.buymeacoffee.com/marcuth">
|
|
17
|
+
<img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" width="200">
|
|
18
|
+
</a>
|
|
19
|
+
|
|
20
|
+
### Template
|
|
21
|
+
|
|
22
|
+
The foundation for creating images in Xpict is the `Template`. This is where you define your image layers and encapsulate all assembly logic. There are two ways to define an initial `Template`.
|
|
23
|
+
|
|
24
|
+
**Using the config property (width, height, and fill):**
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import xpict from "xpict"
|
|
28
|
+
|
|
29
|
+
type RenderData = {} // Your data structure used in layer logic
|
|
30
|
+
|
|
31
|
+
const template = xpict.template<RenderData>({
|
|
32
|
+
config: {
|
|
33
|
+
width: 1280,
|
|
34
|
+
height: 720,
|
|
35
|
+
fill: "#020817" // optional
|
|
36
|
+
},
|
|
37
|
+
layers: [...]
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
**Using an image as a background:**
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import xpict from "xpict"
|
|
46
|
+
|
|
47
|
+
type RenderData = {} // Your data structure used in layer logic
|
|
48
|
+
|
|
49
|
+
const template = xpict.template<RenderData>({
|
|
50
|
+
imagePath: "path/to/background.png",
|
|
51
|
+
layers: [...]
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
### Layers
|
|
59
|
+
|
|
60
|
+
Layers are another fundamental component of Xpict. They act as building blocks—sometimes functional, sometimes just wrappers—that organize code declaratively and provide instructions to `@napi-rs/canvas` on how to perform operations within our canvas context.
|
|
61
|
+
|
|
62
|
+
All axes (x, y) across all layer types support dynamic values:
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
x: ({ data, index /* index is useful if you are inside a `RepeatLayer` loop */ }) => index * 110,
|
|
66
|
+
y: ({ data, index /* index is useful if you are inside a `RepeatLayer` loop */ }) => index * 50,
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Every layer also supports conditional rendering via the `when` property. If not defined, the layer renders by default.
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
when: ({ data, index }) => false // must return a boolean
|
|
74
|
+
|
|
9
75
|
```
|
|
10
76
|
|
|
11
|
-
|
|
77
|
+
#### TextLayer
|
|
12
78
|
|
|
13
|
-
|
|
79
|
+
To insert text into the canvas, use `TextLayer`:
|
|
80
|
+
|
|
81
|
+
```ts
|
|
14
82
|
import xpict from "xpict"
|
|
15
83
|
|
|
16
|
-
const
|
|
84
|
+
const myFont = xpict.utils.font({
|
|
85
|
+
name: "curse-casual",
|
|
86
|
+
filePath: "path/to/font.ttf",
|
|
87
|
+
color: "#ffffff",
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
type RenderData = {}
|
|
91
|
+
|
|
92
|
+
const template = xpict.template<RenderData>({
|
|
17
93
|
config: {
|
|
18
|
-
width:
|
|
19
|
-
height:
|
|
20
|
-
fill: "#
|
|
94
|
+
width: 1280,
|
|
95
|
+
height: 720,
|
|
96
|
+
fill: "#020817"
|
|
21
97
|
},
|
|
22
98
|
layers: [
|
|
23
|
-
xpict.
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
99
|
+
xpict.text({
|
|
100
|
+
x: 640,
|
|
101
|
+
y: 360,
|
|
102
|
+
font: myFont({
|
|
103
|
+
size: 60,
|
|
104
|
+
color: "#ffffff" // optional, as it was previously defined in font config
|
|
105
|
+
}),
|
|
106
|
+
text: "Hello World",
|
|
107
|
+
align: "center", // optional
|
|
108
|
+
baseline: "middle", // optional
|
|
109
|
+
stroke: {
|
|
110
|
+
width: 4,
|
|
111
|
+
fill: "#000000"
|
|
112
|
+
} // optional
|
|
113
|
+
})
|
|
30
114
|
]
|
|
31
115
|
})
|
|
32
116
|
|
|
33
|
-
const image = await template.render({...}) // para renderizar a imagem passando os dados de entrada
|
|
34
117
|
```
|
|
35
118
|
|
|
119
|
+
**Using dynamic text:**
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
text: ({ data, index }) => data.myText
|
|
123
|
+
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
#### ImageLayer
|
|
129
|
+
|
|
130
|
+
To insert images into the canvas, use `ImageLayer`:
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
import xpict from "xpict"
|
|
134
|
+
|
|
135
|
+
const template = xpict.template<RenderData>({
|
|
136
|
+
config: {
|
|
137
|
+
width: 1280,
|
|
138
|
+
height: 720,
|
|
139
|
+
fill: "#020817"
|
|
140
|
+
},
|
|
141
|
+
layers: [
|
|
142
|
+
xpict.image({
|
|
143
|
+
src: "path/to/image.png",
|
|
144
|
+
x: 0,
|
|
145
|
+
y: 0,
|
|
146
|
+
}),
|
|
147
|
+
]
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
**Using a dynamic image:**
|
|
153
|
+
|
|
154
|
+
```ts
|
|
155
|
+
src: ({ data, index }) => data.myImagePath
|
|
156
|
+
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
|
|
161
|
+
#### RectangleLayer
|
|
162
|
+
|
|
163
|
+
To insert rectangles into the canvas, use `RectangleLayer`:
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
xpict.rectangle({
|
|
167
|
+
x: 0,
|
|
168
|
+
y: 0,
|
|
169
|
+
fill: "#ffffff",
|
|
170
|
+
width: 150,
|
|
171
|
+
height: 50,
|
|
172
|
+
borderRadius: 5 // optional
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
---
|
|
178
|
+
|
|
179
|
+
#### CircleLayer
|
|
180
|
+
|
|
181
|
+
To insert circles into the canvas, use `CircleLayer`:
|
|
182
|
+
|
|
183
|
+
```ts
|
|
184
|
+
xpict.circle({
|
|
185
|
+
x: 0,
|
|
186
|
+
y: 0,
|
|
187
|
+
fill: "#ffffff",
|
|
188
|
+
radius: 50,
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
---
|
|
194
|
+
|
|
195
|
+
#### LineLayer
|
|
196
|
+
|
|
197
|
+
To insert lines into the canvas, use `LineLayer`:
|
|
198
|
+
|
|
199
|
+
```ts
|
|
200
|
+
xpict.line({
|
|
201
|
+
from: { x: 0, y: 0 },
|
|
202
|
+
to: { x: 0, y: 50 },
|
|
203
|
+
color: "#ffffff",
|
|
204
|
+
width: 1
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
---
|
|
210
|
+
|
|
211
|
+
#### GroupLayer
|
|
212
|
+
|
|
213
|
+
If you want to group layers that share a common context, you can use `GroupLayer` to keep your code organized and apply an offset if desired:
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
xpict.group({
|
|
217
|
+
x: 50,
|
|
218
|
+
y: 50,
|
|
219
|
+
layers: [...]
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
---
|
|
225
|
+
|
|
226
|
+
#### RepeatLayer
|
|
227
|
+
|
|
228
|
+
If you need to handle arrays to generate layers, use `RepeatLayer` to organize your code and loop logic:
|
|
229
|
+
|
|
230
|
+
```ts
|
|
231
|
+
xpict.repeat({
|
|
232
|
+
each: ({ data }) => data.images,
|
|
233
|
+
x: ({ index }) => index * 50,
|
|
234
|
+
y: ({ index }) => index * 50,
|
|
235
|
+
layer: (image, { index, length }) => xpict.image({
|
|
236
|
+
x: 0,
|
|
237
|
+
y: 0,
|
|
238
|
+
src: image
|
|
239
|
+
})
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
---
|
|
245
|
+
|
|
246
|
+
### Image Transformations and Effects
|
|
247
|
+
|
|
248
|
+
If you want to manipulate an image or apply effects, you can do so using the `transform` property of `ImageLayer`, or via `flipX`, `flipY`, and `rotate`.
|
|
249
|
+
|
|
250
|
+
**Using transform:**
|
|
251
|
+
|
|
252
|
+
```ts
|
|
253
|
+
xpict.image({
|
|
254
|
+
x: 0,
|
|
255
|
+
y: 0,
|
|
256
|
+
src: "...",
|
|
257
|
+
transform: [
|
|
258
|
+
xpict.effects.blur(2.5),
|
|
259
|
+
({ canvas, ctx, data, index }) => {
|
|
260
|
+
// manipulate the `ctx` directly here to apply custom logic
|
|
261
|
+
}
|
|
262
|
+
]
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
---
|
|
268
|
+
|
|
269
|
+
## 🧪 Tests (Not included yet, CONTRIBUTE! :D)
|
|
270
|
+
|
|
271
|
+
Automated tests are located in `__tests__`. To run them:
|
|
272
|
+
|
|
273
|
+
```bash
|
|
274
|
+
npm run test
|
|
275
|
+
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
## 🤝 Contributing
|
|
279
|
+
|
|
280
|
+
Want to contribute? Follow these steps:
|
|
281
|
+
|
|
282
|
+
1. Fork the repository.
|
|
283
|
+
2. Create a new branch (`git checkout -b feature-new`).
|
|
284
|
+
3. Commit your changes (`git commit -m 'Add new feature'`).
|
|
285
|
+
4. Push to the branch (`git push origin feature-new`).
|
|
286
|
+
5. Open a Pull Request.
|
|
287
|
+
|
|
288
|
+
## 📝 License
|
|
289
|
+
|
|
290
|
+
This project is licensed under the MIT License.
|
package/dist/effects/blur.d.ts
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
export declare function blurEffect(sigma: number | boolean | sharp.BlurOptions): ImageTransformFunction<any>;
|
|
1
|
+
import { TransformOptions } from "../layers/image-layer";
|
|
2
|
+
export declare function blur<Data>(radius: number): ({ canvas, ctx }: TransformOptions<Data>) => void;
|
package/dist/effects/blur.js
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
exports.blur = blur;
|
|
4
|
+
const canvas_1 = require("@napi-rs/canvas");
|
|
5
|
+
function blur(radius) {
|
|
6
|
+
return ({ canvas, ctx }) => {
|
|
7
|
+
if (radius <= 0)
|
|
8
|
+
return;
|
|
9
|
+
const tempCanvas = (0, canvas_1.createCanvas)(canvas.width, canvas.height);
|
|
10
|
+
const tempCtx = tempCanvas.getContext("2d");
|
|
11
|
+
tempCtx.drawImage(canvas, 0, 0);
|
|
12
|
+
ctx.save();
|
|
13
|
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
14
|
+
ctx.filter = `blur(${radius}px)`;
|
|
15
|
+
ctx.drawImage(tempCanvas, 0, 0);
|
|
16
|
+
ctx.restore();
|
|
7
17
|
};
|
|
8
18
|
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.boxBlur = boxBlur;
|
|
4
|
+
function boxBlur(radius) {
|
|
5
|
+
return ({ canvas, ctx }) => {
|
|
6
|
+
const r = Math.floor(radius);
|
|
7
|
+
if (r <= 0)
|
|
8
|
+
return;
|
|
9
|
+
const width = canvas.width;
|
|
10
|
+
const height = canvas.height;
|
|
11
|
+
const imageData = ctx.getImageData(0, 0, width, height);
|
|
12
|
+
const data = imageData.data;
|
|
13
|
+
const sourceData = new Uint8ClampedArray(data);
|
|
14
|
+
for (let y = 0; y < height; y++) {
|
|
15
|
+
for (let x = 0; x < width; x++) {
|
|
16
|
+
let red = 0, green = 0, blue = 0, alpha = 0;
|
|
17
|
+
let count = 0;
|
|
18
|
+
for (let i = -r; i <= r; i++) {
|
|
19
|
+
const nx = x + i;
|
|
20
|
+
if (nx >= 0 && nx < width) {
|
|
21
|
+
const sourceIndex = (y * width + nx) * 4;
|
|
22
|
+
red += sourceData[sourceIndex];
|
|
23
|
+
green += sourceData[sourceIndex + 1];
|
|
24
|
+
blue += sourceData[sourceIndex + 2];
|
|
25
|
+
alpha += sourceData[sourceIndex + 3];
|
|
26
|
+
count++;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const targetIndex = (y * width + x) * 4;
|
|
30
|
+
data[targetIndex] = red / count;
|
|
31
|
+
data[targetIndex + 1] = green / count;
|
|
32
|
+
data[targetIndex + 2] = blue / count;
|
|
33
|
+
data[targetIndex + 3] = alpha / count;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
sourceData.set(data);
|
|
37
|
+
for (let x = 0; x < width; x++) {
|
|
38
|
+
for (let y = 0; y < height; y++) {
|
|
39
|
+
let red = 0, green = 0, blue = 0, alpha = 0;
|
|
40
|
+
let count = 0;
|
|
41
|
+
for (let i = -r; i <= r; i++) {
|
|
42
|
+
const ny = y + i;
|
|
43
|
+
if (ny >= 0 && ny < height) {
|
|
44
|
+
const sourceIndex = (ny * width + x) * 4;
|
|
45
|
+
red += sourceData[sourceIndex];
|
|
46
|
+
green += sourceData[sourceIndex + 1];
|
|
47
|
+
blue += sourceData[sourceIndex + 2];
|
|
48
|
+
alpha += sourceData[sourceIndex + 3];
|
|
49
|
+
count++;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const targetIndex = (y * width + x) * 4;
|
|
53
|
+
data[targetIndex] = red / count;
|
|
54
|
+
data[targetIndex + 1] = green / count;
|
|
55
|
+
data[targetIndex + 2] = blue / count;
|
|
56
|
+
data[targetIndex + 3] = alpha / count;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
ctx.putImageData(imageData, 0, 0);
|
|
60
|
+
};
|
|
61
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare function
|
|
1
|
+
import { TransformOptions } from "../layers/image-layer";
|
|
2
|
+
export declare function grayscale<Data>(amount?: number): ({ canvas, ctx }: TransformOptions<Data>) => void;
|
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
exports.grayscale = grayscale;
|
|
4
|
+
const canvas_1 = require("@napi-rs/canvas");
|
|
5
|
+
function grayscale(amount = 100) {
|
|
6
|
+
return ({ canvas, ctx }) => {
|
|
7
|
+
if (amount <= 0)
|
|
8
|
+
return;
|
|
9
|
+
const tempCanvas = (0, canvas_1.createCanvas)(canvas.width, canvas.height);
|
|
10
|
+
const tempCtx = tempCanvas.getContext("2d");
|
|
11
|
+
tempCtx.drawImage(canvas, 0, 0);
|
|
12
|
+
ctx.save();
|
|
13
|
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
14
|
+
ctx.filter = `grayscale(${amount}%)`;
|
|
15
|
+
ctx.drawImage(tempCanvas, 0, 0);
|
|
16
|
+
ctx.restore();
|
|
7
17
|
};
|
|
8
18
|
}
|
package/dist/effects/index.d.ts
CHANGED
package/dist/effects/index.js
CHANGED
|
@@ -16,4 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./blur"), exports);
|
|
18
18
|
__exportStar(require("./grayscale"), exports);
|
|
19
|
-
__exportStar(require("./
|
|
19
|
+
__exportStar(require("./box-blur"), exports);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./text-align"), exports);
|
|
18
|
+
__exportStar(require("./text-baseline"), exports);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TextAlign = void 0;
|
|
4
|
+
var TextAlign;
|
|
5
|
+
(function (TextAlign) {
|
|
6
|
+
TextAlign["Left"] = "left";
|
|
7
|
+
TextAlign["Center"] = "center";
|
|
8
|
+
TextAlign["Right"] = "right";
|
|
9
|
+
})(TextAlign || (exports.TextAlign = TextAlign = {}));
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TextBaseline = void 0;
|
|
4
|
+
var TextBaseline;
|
|
5
|
+
(function (TextBaseline) {
|
|
6
|
+
TextBaseline["Top"] = "top";
|
|
7
|
+
TextBaseline["Hanging"] = "hanging";
|
|
8
|
+
TextBaseline["Middle"] = "middle";
|
|
9
|
+
TextBaseline["Alphabetic"] = "alphabetic";
|
|
10
|
+
TextBaseline["Ideographic"] = "ideographic";
|
|
11
|
+
TextBaseline["Bottom"] = "bottom";
|
|
12
|
+
})(TextBaseline || (exports.TextBaseline = TextBaseline = {}));
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { GroupLayer, RepeatLayer, GroupLayerOptions, ImageLayer, ImageLayerOptions, LineLayer, LineLayerOptions, RectangleLayer, RectangleLayerOptions, RepeatLayerOptions, TextLayer, TextLayerOptions, CircleLayerOptions, CircleLayer } from "./layers";
|
|
2
|
-
import { negativeEffect, blurEffect, grayscaleEffect } from "./effects";
|
|
3
2
|
import { Template, InputTemplateOptions } from "./template";
|
|
4
|
-
|
|
3
|
+
import * as utils from "./utils";
|
|
4
|
+
import * as effects from "./effects";
|
|
5
5
|
export * from "./effects";
|
|
6
|
+
export * from "./layers";
|
|
6
7
|
export * from "./template";
|
|
7
8
|
export * from "./utils";
|
|
8
9
|
export * from "./error";
|
|
@@ -15,10 +16,7 @@ declare const xpict: {
|
|
|
15
16
|
group<Data>(options: GroupLayerOptions<Data>): GroupLayer<Data>;
|
|
16
17
|
line<Data>(options: LineLayerOptions<Data>): LineLayer<Data>;
|
|
17
18
|
template<Data>(options: InputTemplateOptions<Data>): Template<Data>;
|
|
18
|
-
effects:
|
|
19
|
-
|
|
20
|
-
blur: typeof blurEffect;
|
|
21
|
-
grayscale: typeof grayscaleEffect;
|
|
22
|
-
};
|
|
19
|
+
effects: typeof effects;
|
|
20
|
+
utils: typeof utils;
|
|
23
21
|
};
|
|
24
22
|
export default xpict;
|
package/dist/index.js
CHANGED
|
@@ -10,15 +10,38 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
10
10
|
if (k2 === undefined) k2 = k;
|
|
11
11
|
o[k2] = m[k];
|
|
12
12
|
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
13
35
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
36
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
37
|
};
|
|
16
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
39
|
const layers_1 = require("./layers");
|
|
18
|
-
const effects_1 = require("./effects");
|
|
19
40
|
const template_1 = require("./template");
|
|
20
|
-
|
|
41
|
+
const utils = __importStar(require("./utils"));
|
|
42
|
+
const effects = __importStar(require("./effects"));
|
|
21
43
|
__exportStar(require("./effects"), exports);
|
|
44
|
+
__exportStar(require("./layers"), exports);
|
|
22
45
|
__exportStar(require("./template"), exports);
|
|
23
46
|
__exportStar(require("./utils"), exports);
|
|
24
47
|
__exportStar(require("./error"), exports);
|
|
@@ -47,10 +70,7 @@ const xpict = {
|
|
|
47
70
|
template(options) {
|
|
48
71
|
return new template_1.Template(options);
|
|
49
72
|
},
|
|
50
|
-
effects:
|
|
51
|
-
|
|
52
|
-
blur: effects_1.blurEffect,
|
|
53
|
-
grayscale: effects_1.grayscaleEffect,
|
|
54
|
-
},
|
|
73
|
+
effects: effects,
|
|
74
|
+
utils: utils,
|
|
55
75
|
};
|
|
56
76
|
exports.default = xpict;
|
|
@@ -10,5 +10,5 @@ export type CircleLayerOptions<Data> = {
|
|
|
10
10
|
export declare class CircleLayer<Data> extends Layer<Data> {
|
|
11
11
|
private readonly options;
|
|
12
12
|
constructor(options: CircleLayerOptions<Data>);
|
|
13
|
-
render({ context:
|
|
13
|
+
render({ context: renderContext, data, index, templateConfig }: RenderOptions<Data>): Promise<void>;
|
|
14
14
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CircleLayer = void 0;
|
|
4
|
-
const canvas_1 = require("canvas");
|
|
5
4
|
const layer_1 = require("./layer");
|
|
6
5
|
const resolve_axis_1 = require("../utils/resolve-axis");
|
|
7
6
|
class CircleLayer extends layer_1.Layer {
|
|
@@ -9,41 +8,28 @@ class CircleLayer extends layer_1.Layer {
|
|
|
9
8
|
super(options.when);
|
|
10
9
|
this.options = options;
|
|
11
10
|
}
|
|
12
|
-
async render({ context:
|
|
13
|
-
const
|
|
14
|
-
const metadata = await image.metadata();
|
|
15
|
-
const canvasWidth = metadata.width;
|
|
16
|
-
const canvasHeight = metadata.height;
|
|
17
|
-
const x = ctx.offsetX +
|
|
11
|
+
async render({ context: renderContext, data, index = 0, templateConfig }) {
|
|
12
|
+
const x = renderContext.offsetX +
|
|
18
13
|
(0, resolve_axis_1.resolveAxis)({
|
|
19
14
|
axis: this.options.x,
|
|
20
15
|
data: data,
|
|
21
16
|
index: index,
|
|
22
|
-
|
|
17
|
+
templateSize: templateConfig,
|
|
23
18
|
});
|
|
24
|
-
const y =
|
|
19
|
+
const y = renderContext.offsetY +
|
|
25
20
|
(0, resolve_axis_1.resolveAxis)({
|
|
26
21
|
axis: this.options.y,
|
|
27
22
|
data: data,
|
|
28
23
|
index: index,
|
|
29
|
-
|
|
24
|
+
templateSize: templateConfig,
|
|
30
25
|
});
|
|
31
26
|
const { radius, fill } = this.options;
|
|
32
|
-
const
|
|
33
|
-
const context = canvas.getContext("2d");
|
|
27
|
+
const context = renderContext.ctx;
|
|
34
28
|
context.beginPath();
|
|
35
29
|
context.arc(x, y, radius, 0, Math.PI * 2);
|
|
36
30
|
context.closePath();
|
|
37
31
|
context.fillStyle = fill;
|
|
38
32
|
context.fill();
|
|
39
|
-
const circleBuffer = canvas.toBuffer();
|
|
40
|
-
ctx.image = image.composite([
|
|
41
|
-
{
|
|
42
|
-
input: circleBuffer,
|
|
43
|
-
top: 0,
|
|
44
|
-
left: 0,
|
|
45
|
-
},
|
|
46
|
-
]);
|
|
47
33
|
}
|
|
48
34
|
}
|
|
49
35
|
exports.CircleLayer = CircleLayer;
|