Documentation

Learn how to use @skillpet/circuit in your projects.

Installation

npm install @skillpet/circuit

Quick Start

import { renderFromJson, mountFromJson } from '@skillpet/circuit'

// Static SVG string
const svg = renderFromJson({
  elements: [
    { type: 'SourceV', label: 'V₁' },
    { type: 'Resistor', d: 'right', label: '1kΩ' },
    { type: 'Capacitor', d: 'down', label: '10μF' },
    { type: 'Line', d: 'left' },
  ]
})
document.getElementById('root').innerHTML = svg

// Interactive mount
const ctrl = mountFromJson(
  document.getElementById('root'),
  circuitJson,
  { theme: 'auto', interactive: true }
)

ctrl.on('elementClick', (info) => console.log(info))
ctrl.destroy()  // cleanup

JSON Specification

Top-level fields

elementsArray of element objects or stack commands ("push"/"pop")configOptional drawing config: unit, font, lw, colordrawingDrawing options: colorTransition, etc.

Element object

typeComponent name: "Resistor", "Capacitor", "Opamp", etc.dDirection: "right", "up", "left", "down"labelText label (string or {text, loc, ofst} object)atAnchor reference: "R1.end", "U1.in1", etc.anchorElement pin to place at "at" positionidUnique identifier for referencing anchorscolorCSS color string for this elementtooltipTooltip text shown on hover (interactive mode)tox / toyAlign endpoint to another element's x or y coordinate

Colors & Gradients

Set per-element colors and enable gradient transitions between adjacent elements.

{
  "drawing": { "colorTransition": true },
  "elements": [
    { "type": "SourceV", "color": "#3498db", "label": "V₁" },
    { "type": "Resistor", "d": "right", "color": "#e74c3c", "label": "R₁" },
    { "type": "Capacitor", "d": "down", "color": "#2ecc71", "label": "C₁" },
    { "type": "Inductor", "d": "left", "color": "#9b59b6", "label": "L₁" }
  ]
}

Theme System

Built-in themes: default dark print auto

theme: "auto" to read from CSS variables on the host page. Define these variables on :root:

/* Light mode */
:root {
  --circuit-bg: transparent;
  --circuit-stroke: #1a1a1a;
  --circuit-text: #1a1a1a;
}
/* Dark mode */
.dark {
  --circuit-stroke: #c8d6e5;
  --circuit-text: #dfe6e9;
}

Interactive API

const ctrl = mountFromJson(el, json, { interactive: true })

// Events
ctrl.on('elementClick', (info) => { /* { id, type, meta } */ })
ctrl.on('elementHover', (info) => { /* { id, type, meta } */ })

// Highlight
ctrl.highlight('R1')
ctrl.clearHighlight()

// Theme
ctrl.setTheme('dark')
ctrl.setTheme('auto')

// Cleanup
ctrl.destroy()

Component Reference (178)

All available component types, organized by category.

Passive (28)

ResistorResistorIECResistorIEEEResistorVarResistorVarIECThermistorPhotoresistorPhotoresistorIECPotentiometerPotentiometerIECCapacitorCapacitor2CapacitorVarCapacitorTrimInductorInductor2InductorVarInductorVar2CrystalFuseFuseUSFuseIECFuseIEEEBreakerMemristorMemristor2JosephsonSparkGap

Sources (14)

SourceVSourceISourceSinSourceSquareSourceTriangleSourcePulseSourceControlledSourceControlledVSourceControlledIBatteryBatteryCellSolarSourceRampSourceNoise

Semiconductors (41)

DiodeDiodeFullDiodeTunnelDiodeShockleyLEDLED2PhotodiodeZenerSchottkyVaractorDiacBjtNpnBjtPnpBjtNpn2BjtPnp2BjtNpnBulkBjtPnpBulkNpnPhotoPnpPhotoNFetPFetNFet2PFet2NMosPMosNFetDPFetDNJFetPJFetAnalogNFetAnalogPFetHemtNHemtPIgbtNIgbtPOpampOpampDiffSCRTriacRectifierOptocoupler

Logic Gates (21)

AndOrNandNorXorXnorNotBufSchmittAndSchmittNandSchmittNotSchmittBufTristateTristateNotTgateTgate2DFlipFlopJKFlipFlopSRFlipFlopMultiplexerDemultiplexer

ICs & Modules (7)

IcIcDIPIc555Ic555DIPSevenSegmentSevenSegmentDigitVoltageRegulator

Connectors (15)

HeaderHeaderLabelBusConnectBusLineCoaxConnectPlugJackAudioJackRCAUSBDB25DB9DE9DE15OldPhone

Switches (13)

SwitchSwitchSpdtSwitchDpstSwitchDpdtSwitchReedSwitchDIPSwitchRotaryButtonButtonNOButtonNCRelayRelaySPDTRelayDPDT

Lines & Labels (16)

LineArrowWireDotArrowheadLabelTagCurrentLabelCurrentLabelInlineLoopArrowLoopCurrentArcNEncircleEncircleBoxGapAnnotate

Meters (6)

MeterVMeterAMeterIMeterOhmOscilloscopeLamp

Miscellaneous (17)

GroundGroundSignalGroundChassisVddVssVccTransformerWheatstoneMotorGeneratorSpeakerMicrophoneAntennaAntennaLoopAntennaLoop2CableCoax