master
AdityaAngajala Publish 0.1.0 da40167 3mo ago
#!/usr/bin/env node
// Generate a seamless circuit-pattern tile SVG from the command line.
//
//   node generate-tile.mjs --seed nitrogen --density 1 --out tile.svg
//   node generate-tile.mjs --seed foo --transparent > tile.svg
//
// The output tiles seamlessly in both axes (the router runs on a torus), so
// it can be used directly as a repeating CSS background-image.

import { writeFileSync } from "node:fs";
import { generateCircuitTile, tileSvg } from "./circuit-background.js";

const args = process.argv.slice(2);
const flag = (name) => {
  const index = args.indexOf(`--${name}`);
  return index >= 0 ? args[index + 1] : undefined;
};

// Tunable generation parameters, exposed as kebab-case flags
// (e.g. --turn-penalty 0.8 --bus-width 1.4 --combs 0).
const TUNABLES = [
  "density",
  "formations",
  "busWidth",
  "turnPenalty",
  "weave",
  "cramp",
  "horizontalBias",
  "netReach",
  "jogBias",
  "stubChance",
  "inlineViaChance",
  "combs",
];
const kebab = (key) => key.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`);

if (args.includes("--help") || args.includes("-h")) {
  console.log(
    "Usage: node generate-tile.mjs [--seed <string>] [--background <css-color>] " +
      "[--transparent] [--out <file>]\n\nTuning flags (numbers):\n" +
      TUNABLES.map((key) => `  --${kebab(key)}`).join("\n"),
  );
  process.exit(0);
}

const seed = flag("seed") ?? "nitrogen";
const background = args.includes("--transparent") ? "" : flag("background") ?? "#0d1117";
const out = flag("out");

const options = { seed };
for (const key of TUNABLES) {
  const value = flag(kebab(key));
  if (value !== undefined) options[key] = Number(value);
}

const tile = generateCircuitTile(options);
const svg = tileSvg(tile, { background, holeColor: flag("background") ?? "#0d1117" });

if (out) {
  writeFileSync(out, svg);
  const vias = tile.traces.reduce((total, trace) => total + trace.vias.length, 0);
  console.error(`OK: wrote ${out} (seed=${seed} traces=${tile.traces.length} vias=${vias})`);
  console.error("Hint: the tile repeats seamlessly; use it as background-image with background-repeat: repeat.");
  console.error("Hint: run with --help to see the tuning flags (bus width, turn penalty, weave, ...).");
} else {
  process.stdout.write(svg);
}