circuit-to-svg
A TypeScript library for converting Circuit JSON to Schematic, PCB and Assembly SVG representations.
npm add circuit-to-svg
# or...
bun add circuit-to-svg
Overview
This library provides functionality to convert Circuit JSON into SVG (Scalable Vector Graphics) representations. It supports both schematic and PCB (Printed Circuit Board), and Assembly layouts.
Features
- Convert schematic circuit descriptions to SVG
- Convert PCB layouts to SVG
- Support for various circuit elements:
- Components
- Traces
- Text labels
- Net labels
- PCB boards
- PCB components
- PCB traces
- PCB holes and pads
Installation
npm install circuit-to-svg
Usage
import { readFileSync, writeFileSync } from 'fs'
import {
convertCircuitJsonToSchematicSvg,
convertCircuitJsonToPcbSvg,
} from 'circuit-to-svg'
const circuitJson = JSON.parse(readFileSync('circuit.json', 'utf8'))
// Generate a schematic with a grid
const schematicSvg = convertCircuitJsonToSchematicSvg(circuitJson, {
width: 1000,
height: 600,
grid: true,
})
// Generate a PCB image using the board's aspect ratio
const pcbSvg = convertCircuitJsonToPcbSvg(circuitJson, {
matchBoardAspectRatio: true,
backgroundColor: '#1e1e1e',
})
writeFileSync('board.svg', pcbSvg)
API
convertCircuitJsonToSchematicSvg
convertCircuitJsonToSchematicSvg(circuitJson: AnyCircuitElement[], options?): string
Converts a schematic circuit description to an SVG string.
convertCircuitJsonToPcbSvg
convertCircuitJsonToPcbSvg(circuitJson: AnyCircuitElement[], options?): string
Converts a PCB layout description to an SVG string.
Options
width
andheight
– dimensions of the output SVG. Defaults to800x600
.matchBoardAspectRatio
– iftrue
, adjust the SVG dimensions so the resulting aspect ratio matches thepcb_board
found in the circuit JSON.backgroundColor
– fill color for the SVG background rectangle. Defaults to"#000"
.drawPaddingOutsideBoard
– iffalse
, omit the board outline and extra padding around it. Defaults totrue
.
convertCircuitJsonToAssemblySvg
convertCircuitJsonToAssemblySvg(circuitJson: AnyCircuitElement[], options?): string
Converts circuit JSON into an assembly view of the board and components.
Options
width
andheight
– dimensions of the output SVG. Defaults to800x600
.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License.