-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathogimage.js
More file actions
69 lines (63 loc) · 2.06 KB
/
ogimage.js
File metadata and controls
69 lines (63 loc) · 2.06 KB
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
const WIDTH = 4096;
const HEIGHT = 2048;
const PADDING = 500;
const title = process.argv[2];
const postSlug = process.argv[3];
const { createCanvas, loadImage, registerFont } = require("canvas");
registerFont(__dirname + "/ogimage-fonts/Roboto_Mono/RobotoMono-Bold.ttf", {
family: "Roboto Mono"
});
const canvas = createCanvas(WIDTH, HEIGHT);
const ctx = canvas.getContext("2d");
// Draw post cover
// const image = await loadImage(imagePath);
// ctx.filter = 'blur(500px)'
// ctx.drawImage(image, 0, 0, image.width, image.height, 0, 0, WIDTH, HEIGHT);
// ctx.filter = 'blur(0px)'
// Draw overlay
ctx.fillStyle = "hsl(7, 80%, 98%)";
ctx.fillRect(0, 0, WIDTH, HEIGHT);
// Write text
drawText(ctx);
// Save image
const fs = require("fs");
const out = fs.createWriteStream(
__dirname + `/assets/img/og-images/posts/${postSlug}.png`
);
const stream = canvas.createPNGStream();
stream.pipe(out);
out.on("finish", () => console.log(postSlug + ".png was created"));
function drawText(ctx) {
ctx.fillStyle = "hsl(12, 79%, 72%)";
ctx.font = "200px 'Roboto Mono'";
ctx.textBaseline = "top";
const maxWidth = WIDTH - PADDING * 2;
const words = title.split(" ");
const lines = [];
let fits = true;
while (words.length > 0) {
let line = "";
let i;
for (i = 0; i < words.length; i++) {
line += (i === 0 ? "" : " ") + words[i];
if (ctx.measureText(line).width > maxWidth) {
fits = false;
break;
}
}
const goodIndex = fits ? i : i - 1;
const wordsForLine = words.splice(0, Math.max(1, goodIndex + 1));
// console.log(i, line, ctx.measureText(line).width, maxWidth);
lines.push(wordsForLine.join(" "));
}
// console.log(lines);
const final = lines.join("\n");
const { emHeightAscent, emHeightDescent } = ctx.measureText(final);
const textBoxHeight = emHeightDescent + emHeightAscent;
const y = (HEIGHT - textBoxHeight) / 2;
ctx.fillText(final, PADDING, y);
ctx.font = "100px 'Roboto Mono'";
ctx.fillStyle = "hsla(12, 79%, 72%, 0.5)";
ctx.textBaseline = "bottom";
ctx.fillText("Code The Web", PADDING, y - 25);
}