tangled
alpha
login
or
join now
timtinkers.online
/
lemoncalendar
0
fork
atom
this repo has no description
0
fork
atom
overview
issues
pulls
pipelines
Testing with svg2png-wasm instead of resvg-js
timtinkers.online
1 year ago
73cf6782
50990099
+22
-13
1 changed file
expand all
collapse all
unified
split
routes
api
svg-to-png.ts
+22
-13
routes/api/svg-to-png.ts
···
1
1
-
// routes/api/svg-to-png.ts
2
1
import { Handlers } from "$fresh/server.ts";
3
3
-
import { Resvg } from "npm:@resvg/resvg-js";
2
2
+
import { initialize, svg2png } from "npm:svg2png-wasm";
3
3
+
4
4
+
// Initialize the WebAssembly module once
5
5
+
let initialized = false;
6
6
+
const initializeWasm = async () => {
7
7
+
if (!initialized) {
8
8
+
const wasmModule = await fetch(
9
9
+
"https://cdn.jsdelivr.net/npm/svg2png-wasm/svg2png_wasm_bg.wasm",
10
10
+
).then((res) => res.arrayBuffer());
11
11
+
12
12
+
await initialize(new Uint8Array(wasmModule));
13
13
+
initialized = true;
14
14
+
}
15
15
+
};
4
16
5
17
export const handler: Handlers = {
6
18
async POST(req) {
7
19
try {
20
20
+
// Make sure WASM is initialized
21
21
+
await initializeWasm();
22
22
+
8
23
const svgString = await req.text();
9
24
10
10
-
// Configure resvg
11
11
-
const resvg = new Resvg(svgString, {
12
12
-
background: "transparent",
13
13
-
fitTo: {
14
14
-
mode: "width",
15
15
-
value: 800,
16
16
-
},
25
25
+
// Convert SVG to PNG
26
26
+
const pngBuffer = await svg2png(svgString, {
27
27
+
width: 800, // Set width to match your original config
28
28
+
backgroundColor: "transparent",
29
29
+
// Add other options as needed
17
30
});
18
18
-
19
19
-
// Render to PNG
20
20
-
const pngData = resvg.render();
21
21
-
const pngBuffer = pngData.asPng();
22
31
23
32
return new Response(pngBuffer, {
24
33
headers: {