Live demo: ngdiagram.dev/templates/electric-circuit
Interactive circuit / schematic editor built with Angular and ngDiagram. Use this project as a starting point for building your own schematic capture tool, node-based editor, or component-library-driven diagram. Lean dependencies: Angular, ngDiagram, and html-to-image (for JPEG export) — no opinionated third-party UI libraries.
Features:
- Drag-and-drop component placement from a searchable, categorized library palette
- A data-driven component catalog (resistor, capacitor, inductor, potentiometer, fuse, crystal, diode, LED, Zener, NPN/PNP transistors, NE555, switch, push button, battery, +5V, ground) rendered as crisp inline SVG
- Free-form canvas: move, select, rotate, and wire components port-to-port
- Orthogonal wire editing — reshape wires by dragging their segments, with grid snapping and port-anchored ends
- Wire junctions (link-to-link) — drop or drag a wire onto another to create a solder-dot junction; branch from a wire, with junction port routing, merge-on-delete, and cleanup of orphaned junctions
- Right-click context menus — copy / cut / paste / delete / rotate ±90° on a node, paste on the background
- Export dropdown: DXF (vector, for AutoCAD / BricsCAD / LibreCAD — with layer mapping and text styles), editable SVG, JPEG snapshot of the canvas, or JSON (components + connections)
- Properties sidebar with per-component-type specification fields (diagram-to-UI integration)
- Minimap and zoom controls; full-height library / properties rails
- Dark / light theme matching a dedicated design token system
- Unique reference designators (R1, R2, C1…) auto-assigned on drop
| Category | Components |
|---|---|
| Passive | Resistor, Capacitor, Inductor, Potentiometer, Fuse, Crystal |
| Semiconductors | Diode, LED, Zener diode, NPN transistor, PNP transistor |
| Integrated circuits | NE555 |
| Electromechanical | Switch, Push button |
| Power & Ground | Battery, +5V source, GND |
The library is fully data-driven: every component is one entry in
component-catalog.ts
describing its symbol size, connection ports, reference prefix, default value,
and editable specification fields. Add a new component by adding a catalog entry
and a @case to the symbol component.
Built against Angular 19.2 and ngDiagram 1.3 (see package.json); Node.js 18.19+, 20.11+ or 22 and npm 10+.
git clone https://github.com/synergycodes/ng-diagram-electric-circuit.git
cd ng-diagram-electric-circuit
npm install
npm startOpen http://localhost:4200 — a sample circuit loads: the classic NE555 astable multivibrator blinking an LED at about 1 Hz (13 components, 4 junctions, 19 wires). Try dragging a resistor from the library onto the canvas, wiring it into the circuit, then dropping a wire onto another wire to create a junction; right-click a component to rotate it. The sample circuit is seed data — replace it in diagram/data.ts.
| Script | Description |
|---|---|
npm start |
Start the dev server (ng serve) |
npm run build |
Production build (ng build) |
npm run watch |
Development build in watch mode |
npm test |
Run unit tests (Vitest) |
npm run lint |
Lint with ESLint (ng lint) |
npm run format |
Format sources with Prettier |
npm run format:check |
Check formatting without writing |
| Concern | API | Where in this repo |
|---|---|---|
| Bootstrap | provideNgDiagram() |
pages/circuit-editor-page.component.ts |
| Diagram surface | <ng-diagram>, <ng-diagram-background> |
diagram/diagram.component.html |
| Node template | NgDiagramNodeTemplate, NgDiagramNodeTemplateMap |
diagram/diagram.component.ts, diagram/node/circuit-node.component.ts |
| Edge template | NgDiagramEdgeTemplate, NgDiagramEdgeTemplateMap, NgDiagramBaseEdgeComponent |
diagram/diagram.component.ts, diagram/wire/wire.component.ts |
| Ports | <ng-diagram-port> |
diagram/node/circuit-node.component.html, diagram/wire/junction/node.component.ts |
| Palette (drag/drop) | <ng-diagram-palette-item>, <ng-diagram-palette-item-preview>, paletteItemDropped |
library-sidebar/components/palette-tile/palette-tile.component.ts, diagram/diagram.component.html |
| Minimap | <ng-diagram-minimap> |
minimap-bar/minimap-bar.component.html |
| Model | initializeModel(), NgDiagramModelService (getConnectedEdges, awaitable addEdges) |
diagram/diagram.component.ts, diagram/wire/junction/topology/ |
| Selection | NgDiagramSelectionService |
diagram/editor-actions.service.ts |
| Viewport | NgDiagramViewportService (awaitable zoomToFit, zoom, scale) |
diagram/diagram.component.ts, diagram/editor-actions.service.ts |
| Linking config | NgDiagramConfig.linking (validateConnection, finalEdgeDataBuilder) |
diagram/diagram.component.ts |
| Edge routing | NgDiagramConfig.edgeRouting (orthogonal) + routingMode: 'manual' for reshaped wires |
diagram/diagram.component.ts, diagram/wire/interactions/reshape/ |
| Middleware | createMiddlewares() — custom junction port-routing middleware |
diagram/diagram.component.ts |
| Events | diagramInit, edgeDrawEnded, selectionMoved, selectionRemoved |
diagram/diagram.component.html |
src/
├── circuit-theme.css # --ce-* color tokens (dark/light) + ng-diagram var remap
├── typography.css # Poppins type scale
└── app/circuit-editor/
├── circuit-editor.config.ts # injectable editor config (viewport)
├── pages/ # page shell: canvas + overlays
├── diagram/
│ ├── diagram.component.* # ng-diagram host, template maps, palette-drop
│ ├── data.ts # seed example circuit
│ ├── editor-actions.service.ts # clipboard / delete / rotate (context-menu actions)
│ ├── model/ # ComponentType, CircuitNodeData, catalog, guards, connectivity
│ ├── node/
│ │ ├── circuit-node.component.* # one template for every component
│ │ └── symbols/ # inline-SVG schematic symbols
│ └── wire/ # everything about wires/links
│ ├── wire.component.* # edge (wire) template
│ ├── geometry/ # pure orthogonal-polyline math + hit testing
│ ├── pointer-drag-controller.ts # shared gesture plumbing
│ ├── junction/ # solder-dot node + topology/ + routing/
│ └── interactions/ # reshape, stretch, branch-from-wire, link-drop-preview
├── library-sidebar/ # searchable component palette
├── properties-sidebar/ # selection-driven properties form
├── minimap-bar/ # bottom-right zoom + collapsible minimap
├── context-menu/ # node & background right-click menus
├── export/ # DXF + SVG + JPEG + JSON export (service + dropdown)
└── top-navbar/ # logo, filename, export, theme toggle
Every color is a --ce-* token defined per theme in
circuit-theme.css, taken straight from the Figma
color sheets (names and values for both dark and light modes). The base
--ngd-* tokens ship with ngDiagram itself, so that file only adds the --ce-*
tokens and remaps the few --ngd-* variables that drive the on-canvas look —
grid background (major line every 10 minor cells), violet port rings, and the
white-50% wire stroke.
- Add a component: add an entry to
COMPONENT_CATALOGand a@caseinComponentSymbolComponent. The palette tile, on-canvas node, and properties form pick it up automatically. - Change the seed circuit: edit
data.ts. - Tune the viewport: edit
CIRCUIT_EDITOR_DEFAULTS(zoom-to-fit padding, zoom step) incircuit-editor.config.ts, or override theCIRCUIT_EDITOR_CONFIGtoken in the page providers. - Theme: adjust the
--ce-*tokens incircuit-theme.css.
export.service.ts offers
four formats: DXF (vector, with layer mapping and text styles, for AutoCAD /
BricsCAD / LibreCAD — renderers in export/dxf-circuit/),
an editable SVG (vector schematic built from the model, black-on-white for
print), a JPEG raster snapshot of the canvas, and JSON. The JSON is a documented ng-diagram-circuit document — a components
array (type, reference, value, specs, position, rotation) plus a connections
array (port-to-port). The connectivity standard for circuits is a SPICE
netlist, but these generic illustrative components don't map cleanly to SPICE
device cards, so a clean JSON format is used instead.
- Composite components: the catalog ships single-primitive components only. Composite parts (e.g. a bridge rectifier — an assembly of diodes plus internal wiring) are out of scope, since each catalog entry maps to one schematic symbol.
- Angular 19 (standalone, signals, zoneless via
provideExperimentalZonelessChangeDetection) - ngDiagram (
ng-diagramon npm) - html-to-image (JPEG export)
- Plain CSS with a custom design-token system (no UI framework)
- Poppins (Google Fonts)
- Issues: GitHub Issues
- ngDiagram Discussions: GitHub Discussions, Discord
- ngDiagram Documentation: ngdiagram.dev/docs
MIT — see LICENSE.
Built with ❤️ by the Synergy Codes team
