@ndbx/runtime
Version:
The `@ndbx/runtime` package provides a runtime environment to embed NodeBox visualizations directly into React applications. NodeBox is a powerful tool for creating interactive and generative visualizations, and this runtime allows you to integrate those
34 lines (28 loc) • 695 B
JavaScript
/**
* Upload an SVG file and outputs as shape data.
*
* @category Input/Output
*/
import { parseSVG } from "@ndbx/g";
export default function (node) {
const fileIn = node.fileIn({ name: "file" });
const shapeOut = node.shapeOut({ name: "out" });
node.onRender = (cx) => {
const [file] = [fileIn.value];
if (file === "") {
shapeOut.set([]);
return;
}
const buffer = cx.assetMap.get(file);
if (!buffer) {
throw new Error(`File ${file} is not loaded.`);
}
let data;
if (file.endsWith(".svg")) {
data = parseSVG(buffer);
} else {
throw new Error("Unsupported file format:", file);
}
shapeOut.set(data);
};
}