◆ Reference · JavaScript
Reference › JavaScript
JavaScript
The npm package milkflow is the core compiled to WebAssembly with a thin JS layer. Node ≥ 18 (ES modules) and browsers, directly or through a bundler (Vite, webpack 5, Parcel, Rollup). TypeScript types in milkflow.d.ts.
npm install milkflow
Example (Node)
import { writeFileSync } from "node:fs";
import { init, flowmap } from "milkflow";
await init();
const { svg, spec } = flowmap(
{
nodes: [
{ id: "centro", x: 0, y: 0, label: "Centro" },
{ id: "porto", x: 3200, y: -1400, label: "Porto" },
{ id: "serra", x: -3800, y: 3600, label: "Serra" },
{ id: "olaria", x: -400, y: -3900, label: "Olaria" },
],
flows: [
{ origin: "serra", destination: "centro", value: 410 },
{ origin: "porto", destination: "centro", value: 260 },
{ origin: "olaria", destination: "centro", value: 180 },
{ origin: "centro", destination: "porto", value: 90 },
{ origin: "serra", destination: "porto", value: 35 },
],
},
{ k: 2 },
);
writeFileSync("mapa.svg", svg);
console.log(spec.meta.accounting);
In the browser, without a bundler
<script type="module">
import { init, flowmap } from "./node_modules/milkflow/index.js";
await init(); // fetches milkflow_bg.wasm next to the script
const { svg } = flowmap(input, { k: 2, style: { theme: "dark" } });
document.querySelector("#map").innerHTML = svg;
</script>
ES modules and WebAssembly do not load over file://: serve the folder (for example, python3 -m http.server). The Playground page does exactly that.
API
| Function | Returns |
|---|---|
init(source?) | Promise<void>. Loads the WebAssembly; needed once in browsers (resolves at once in Node). source overrides where the .wasm comes from (URL, Response, bytes or WebAssembly.Module). |
flowmap(input, options?) | { spec, svg, schematicSpec, schematicSvg, schematicError, tikz, schematicTikz, html } (the last three null unless tikz: true or html: true). Throws an Error with the library's message when the input or options are refused; non-finite numbers are refused before. |
flowmapJson(inputJson, optionsJson?) | The same as JSON text (schematic_spec etc.), byte for byte the native output. |
version() | The package version. |
input and options have the fields of the reference (nodes may carry groups, and options.group takes true or the name of a level), with the same (snake_case) names as the input and output. Planar coordinates: project longitude and latitude first (Web Mercator or UTM). With html: true, html is the self-contained interactive page (the Playground shows it in an iframe); for your own interaction, the SVG carries data-* hooks.