this repo has no description

Testing with svg2png-wasm instead of resvg-js

+22 -13
+22 -13
routes/api/svg-to-png.ts
··· 1 - // routes/api/svg-to-png.ts 2 1 import { Handlers } from "$fresh/server.ts"; 3 - import { Resvg } from "npm:@resvg/resvg-js"; 2 + import { initialize, svg2png } from "npm:svg2png-wasm"; 3 + 4 + // Initialize the WebAssembly module once 5 + let initialized = false; 6 + const initializeWasm = async () => { 7 + if (!initialized) { 8 + const wasmModule = await fetch( 9 + "https://cdn.jsdelivr.net/npm/svg2png-wasm/svg2png_wasm_bg.wasm", 10 + ).then((res) => res.arrayBuffer()); 11 + 12 + await initialize(new Uint8Array(wasmModule)); 13 + initialized = true; 14 + } 15 + }; 4 16 5 17 export const handler: Handlers = { 6 18 async POST(req) { 7 19 try { 20 + // Make sure WASM is initialized 21 + await initializeWasm(); 22 + 8 23 const svgString = await req.text(); 9 24 10 - // Configure resvg 11 - const resvg = new Resvg(svgString, { 12 - background: "transparent", 13 - fitTo: { 14 - mode: "width", 15 - value: 800, 16 - }, 25 + // Convert SVG to PNG 26 + const pngBuffer = await svg2png(svgString, { 27 + width: 800, // Set width to match your original config 28 + backgroundColor: "transparent", 29 + // Add other options as needed 17 30 }); 18 - 19 - // Render to PNG 20 - const pngData = resvg.render(); 21 - const pngBuffer = pngData.asPng(); 22 31 23 32 return new Response(pngBuffer, { 24 33 headers: {