-
Notifications
You must be signed in to change notification settings - Fork 1
/
node-demo.mts
74 lines (64 loc) · 2.31 KB
/
node-demo.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import fs from 'node:fs';
import path from 'node:path';
import { drawText, textToWords } from '../lib/index';
/* eslint-disable no-console */
const OUT_FILEPATH = './demo/node-demo-output.png';
async function main() {
let createCanvas;
try {
({ createCanvas } = await import('canvas'));
} catch {
console.error(
"ERROR: Failed to load the `canvas` module: Make sure it's installed by first running `npm install`"
);
process.exit(1);
}
const canvas = createCanvas(400, 400);
const ctx = canvas.getContext('2d');
const text =
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin convallis eros.';
const words = textToWords(text);
words.forEach((word) => {
if (word.text === 'ipsum') {
word.format = { fontStyle: 'italic', fontColor: 'red' };
} else if (word.text === 'consectetur') {
word.format = { fontWeight: '700', fontColor: 'blue' };
}
});
let height;
try {
// NOTE: text-to-canvas does not formally support Node, nor `node-canvas`, nor does
// `node-canvas` claim to support the full `HTMLCanvasElement` API; but it supports enough
// of it that this should work :) -- if it fails, we'll need to find another Node-based
// Canvas library that has more complete support for the Web Canvas API.
// @ts-expect-error -- because `node-canvas` doesn't support exactly the same API as the `HTMLCanvasElement` on the 2D context
({ height } = drawText(ctx, words, {
x: 100,
y: 100,
width: 200,
height: 200,
fontSize: 24,
fontFamily: 'Times New Roman, serif',
fontWeight: '400',
fontColor: 'green',
debug: true,
}));
} catch (err) {
console.warn(
`WARNING: Encountered an error in drawText() using the 2D context obtained from node-canvas (make sure it supports the HTMLCanvasElement API): "${err instanceof Error ? err.message : (err as string)}"`
);
}
// convert the canvas to a buffer in PNG format
const buffer = canvas.toBuffer('image/png');
fs.mkdirSync(path.dirname(OUT_FILEPATH), { recursive: true });
if (fs.existsSync(OUT_FILEPATH)) {
fs.unlinkSync(OUT_FILEPATH);
}
fs.writeFileSync(OUT_FILEPATH, buffer);
if (height !== undefined) {
console.log(
`Total height (px) = ${height}\nDemo output in ${OUT_FILEPATH}`
);
}
}
void main();