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
export const socialImage = async (req: Request) => {
const params = new URL(req.url).searchParams;
// Look for values form query params, set defaults
const bgColor = params.get("bg") || "orange";
const width = parseInt(params.get("w"), 10) || 1200;
const height = parseInt(params.get("h"), 10) || 600;
const text = params.get("text") || "Wellow horld";
const textColor = params.get("color") || "black";
try {
const { createCanvas } = await import("https://deno.land/x/canvas/mod.ts");
const canvas = createCanvas(width, height);
const ctx = canvas.getContext("2d");
ctx.fillStyle = bgColor;
ctx.fillRect(0, 0, width, height);
if (text) {
ctx.fillStyle = textColor;
ctx.font = "bold 38px sans-serif";
// x and y position is arbitrary right now, this would need improved to actually work
ctx.fillText(text, 60, height - 80);
}
return new Response(canvas.toBuffer(), {
headers: {
"Content-Type": "image/png",
},
});
}
catch (err) {
return err;
}
};