circuit-json-to-gltf 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,61 @@
1
+ // lib/utils/svg-to-png-browser.ts
2
+ import { Resvg, initWasm } from "@resvg/resvg-wasm";
3
+ var wasmInitialized = false;
4
+ async function ensureWasmInitialized() {
5
+ if (!wasmInitialized) {
6
+ try {
7
+ if (typeof process !== "undefined" && process.versions?.node) {
8
+ const { readFileSync } = await import("fs");
9
+ const { fileURLToPath } = await import("url");
10
+ const { dirname, join } = await import("path");
11
+ const currentDir = dirname(fileURLToPath(import.meta.url));
12
+ const wasmPath = join(currentDir, "../../node_modules/@resvg/resvg-wasm/index_bg.wasm");
13
+ const wasmBuffer = readFileSync(wasmPath);
14
+ await initWasm(wasmBuffer);
15
+ } else {
16
+ try {
17
+ const wasmUrl = await import("@resvg/resvg-wasm/index_bg.wasm?url");
18
+ await initWasm(fetch(wasmUrl.default));
19
+ } catch {
20
+ await initWasm(fetch("https://unpkg.com/@resvg/resvg-wasm@2.6.2/index_bg.wasm"));
21
+ }
22
+ }
23
+ wasmInitialized = true;
24
+ } catch (error) {
25
+ console.error("Failed to initialize WASM:", error);
26
+ throw error;
27
+ }
28
+ }
29
+ }
30
+ async function svgToPng(svgString, options = {}) {
31
+ await ensureWasmInitialized();
32
+ const opts = {
33
+ background: options.background,
34
+ fitTo: options.width ? {
35
+ mode: "width",
36
+ value: options.width
37
+ } : options.height ? {
38
+ mode: "height",
39
+ value: options.height
40
+ } : void 0
41
+ };
42
+ const resvg = new Resvg(svgString, opts);
43
+ const pngData = resvg.render();
44
+ const pngBuffer = pngData.asPng();
45
+ return pngBuffer;
46
+ }
47
+ async function svgToPngDataUrl(svgString, options = {}) {
48
+ const pngBuffer = await svgToPng(svgString, options);
49
+ let binary = "";
50
+ const bytes = new Uint8Array(pngBuffer);
51
+ const len = bytes.byteLength;
52
+ for (let i = 0; i < len; i++) {
53
+ binary += String.fromCharCode(bytes[i]);
54
+ }
55
+ const base64 = btoa(binary);
56
+ return `data:image/png;base64,${base64}`;
57
+ }
58
+ export {
59
+ svgToPng,
60
+ svgToPngDataUrl
61
+ };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "circuit-json-to-gltf",
3
3
  "main": "dist/index.js",
4
4
  "type": "module",
5
- "version": "0.0.1",
5
+ "version": "0.0.2",
6
6
  "scripts": {
7
7
  "test": "bun test tests/",
8
8
  "format": "biome format --write .",