Circuit Simulator JavaScript Interface Documentation

This documentation outlines how to interact with the Circuit Simulator using JavaScript when it's embedded in an HTML page, particularly focusing on the use of an iframe to host the simulator.

Overview

To use the JavaScript interface, the simulator and the controlling JavaScript page must be served from the same origin (domain). This is required by the same-origin policy enforced by browsers for security reasons.

To run the simulator on your website, get the offline windows version, and put the contents of the circuitjs1/resources/app/war directory on your website.

You can run the CircuitJS1 simulation in an iframe. The CircuitJS1 object provides methods to interact with the simulator.

See a running example here.

Setup

<iframe id="circuitFrame" src="circuitjs.html?startCircuit=jsinterface.txt" width="800" height="550"></iframe>

...

<script>

var iframe = document.getElementById("circuitFrame");

// Wait for the circuit simulator to load
iframe.contentWindow.oncircuitjsloaded = function() {
    // Simulator is loaded, initialize your code here
    sim = iframe.contentWindow.CircuitJS1;

    // Set up callbacks for updates, analysis, and time steps (all optional)
    sim.onupdate = didUpdate;
    sim.ontimestep = didStep;
    sim.onanalyze = didAnalyze;
};

Available CircuitJS1 Methods

setSimRunning(boolean run)

getTime()

getTimeStep()

setTimeStep(double ts) (Deprecated)

getMaxTimeStep()

setMaxTimeStep(double ts)

isRunning()

getNodeVoltage(String n)

setExtVoltage(String n, double v)

getElements()

getCircuitAsSVG()

exportCircuit()

importCircuit(String circuit, boolean subcircuitsOnly)

Circuit Element Methods

getType()

getInfo()

getVoltageDiff()

getVoltage(n)

getCurrent()

getLabelName() (For LabeledNodeElm)

getPostCount()

Callbacks