Readme

A simplified version of this: https://www.val.town/v/panphora/canvasText to generate open graph images dynamically for my blog

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { fetch } from "https://esm.town/v/std/fetch";
export let canvasText = async (req: Request) => {
const query = new URL(req.url).searchParams;
const { loadImage, createCanvas } = await import(
"https://deno.land/x/canvas/mod.ts"
);
const canvas = createCanvas(1200, 675);
const ctx = canvas.getContext("2d");
let canvasWidth = canvas.width;
let canvasHeight = canvas.height;
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
const librelightres = await fetch(
`https://tomcritchlow.com/fonts/LibreFranklin-Light.ttf`,
);
const librelight = await librelightres.arrayBuffer();
canvas.loadFont(librelight, {
family: "Libre Franklin",
weight: "300",
});
const libresemiboldres = await fetch(
`https://tomcritchlow.com/fonts/LibreFranklin-SemiBold.ttf`,
);
const libresemibold = await libresemiboldres.arrayBuffer();
canvas.loadFont(libresemibold, {
family: "Libre Franklin",
weight: "600",
});
// Assuming query.get("headline") is the parameter you're using
const headline = query.get("headline");
const subheadline = query.get("subheadline");
const date = query.get("date");
// Check if the parameter exists and has content
const hasHeadline = headline && headline.trim().length > 0;
const hasSubheadline = subheadline && subheadline.trim().length > 0;
const hasDate = date && date.trim().length > 0;
// Simple background
ctx.fillStyle = "#f1f1f1"; // Change to your preferred background color
ctx.fillRect(0, 0, canvasWidth, canvasHeight);
// Date text
if (hasDate) {
ctx.fillStyle = "#212121"; // Text color
ctx.font = "20px Courier New"; // Simple font
ctx.fillText(query.get("date"), 50, 100); // Adjust text position and query parameter key as needed
}
// Draw line
ctx.strokeStyle = "#02AD28"; // Black color, you can change this to any color you prefer
ctx.lineWidth = 1; // Width of the line, adjust as needed
ctx.beginPath();
ctx.moveTo(50, 120); // Move to x=50, y=100, change these values to your desired starting point
ctx.lineTo(1150, 120); // Draw to x=200, y=100, change x to your desired ending point
ctx.stroke();
// Headline text
if (hasHeadline) {
ctx.fillStyle = "#212121"; // Text color
ctx.font = "600 55px Libre Franklin"; // Simple font
ctx.fillText(query.get("headline"), 50, 350); // Adjust text position and query parameter key as needed
}
// Subheadline text
if (hasSubheadline) {
ctx.fillStyle = "#212121"; // Text color
ctx.font = "300 35px Libre Franklin"; // Smaller font for subheadline
ctx.fillText(query.get("subheadline"), 50, 400); // Adjust text position and query parameter key as needed
}
// TC text
ctx.fillStyle = "#02AD28"; // Text color
ctx.font = "600 18px Libre Franklin"; // Smaller font for subheadline
ctx.fillText("TOMCRITCHLOW.COM", 50, 600); // Adjust text position and query parameter key as needed
// Optional: Draw an image
// If you have an image URL parameter, you can load and draw it like this:
// const imgBase64 = await fetchRemote(query.get("img"));
// const img = await loadImage(imgBase64);
// ctx.drawImage(img, 50, 200, 100, 100); // Adjust position and size as needed
return new Response(canvas.toBuffer(), {
headers: {
"Content-Type": "image/png",
},
});
};
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.