-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.tsx
59 lines (54 loc) · 1.7 KB
/
mod.tsx
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
/** @jsx h */
import { h, renderToString } from "https://deno.land/x/[email protected]/mod.ts";
/** Parameters to configure SVG generation */
export type GenerateParams = {
/** width of the image */
width?: number;
/** height of the image */
height?: number;
/** color of the text */
color?: string;
/** image text content. defaults to {width}x{height} */
text?: string;
/** font family of the text content */
font?: string;
/** background color */
bgcolor?: string;
};
/** Generates an SVG image for the passed params */
export const generate = async (params: GenerateParams = {}) => {
const width = params.width || 200;
const height = params.height || width;
const text = params.text ?? `${width} x ${height}`;
const font = params.font ?? "sans-serif";
const color = params.color ?? "currentColor";
const bgColor = params.bgcolor ?? "currentColor";
const bgFilter = params.bgcolor ? undefined : "invert(0.8)";
return await renderToString(
<svg
xmlns="http://www.w3.org/2000/svg"
width={width}
height={height}
viewBox={[0, 0, width, height].join(" ")}
style="color-scheme: light dark"
>
<rect width={width} height={height} fill={bgColor} filter={bgFilter} />
<text
fill={color}
x={width / 2}
y={height / 2}
font-family={font}
text-anchor="middle"
font-size="1.2rem"
dominant-baseline="central"
>
{text}
</text>
</svg>,
);
};
/** Generates an SVG image in data URL format */
export const generateUrl = async (params: GenerateParams = {}) => {
const svgString = await generate(params);
return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svgString)}`;
};