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.
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.
<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;
};
setSimRunning(boolean run)run (boolean): true to start the simulation, false to stop it.CircuitJS1.setSimRunning(true);getTime()var time = CircuitJS1.getTime();getTimeStep()var timeStep = CircuitJS1.getTimeStep();setTimeStep(double ts) (Deprecated)setMaxTimeStep.ts (double): The new time step to set.CircuitJS1.setTimeStep(1e-6); // This is deprecatedgetMaxTimeStep()var maxTimeStep = CircuitJS1.getMaxTimeStep();setMaxTimeStep(double ts)ts (double): The new maximum time step to set.CircuitJS1.setMaxTimeStep(1e-3);isRunning()true if the simulation is running, false otherwise.if (CircuitJS1.isRunning()) { ... }getNodeVoltage(String n)n (String): The name of the labeled node.var nodeVoltage = CircuitJS1.getNodeVoltage("vsense");setExtVoltage(String n, double v)n (String): The name of the external voltage element to set the voltage for.v (double): The voltage value to set.CircuitJS1.setExtVoltage("extsin", 5);getElements()var elements = CircuitJS1.getElements();getCircuitAsSVG()onsvgrendered callback.sim.onsvgrendered to a function that receives (sim, svg) before calling this method.function svgRendered(sim, svg) {
console.log('here is svg data:', svg);
}
sim.onsvgrendered = svgRendered;
sim.getCircuitAsSVG();
exportCircuit()var circuitExport = CircuitJS1.exportCircuit();importCircuit(String circuit, boolean subcircuitsOnly)circuit (String): The string representation of the circuit to import (as returned by exportCircuit()).subcircuitsOnly (boolean): If true, only subcircuits are imported.CircuitJS1.importCircuit(circuitString, false);getType()var elementType = element.getType();getInfo()var elementInfo = element.getInfo();getVoltageDiff()var voltageDiff = element.getVoltageDiff();getVoltage(n)n (number): The index of the node to check.var nodeVoltage = element.getVoltage(0);getCurrent()var current = element.getCurrent();getLabelName() (For LabeledNodeElm)var labelName = labelElm.getLabelName();getPosition() (For switches, including LogicInputElm)var pos = element.getPosition();setPosition(value), toggle() (For LogicInputElm)value is 0/false for low and 1/true for high (or 0, 1, 2 for a ternary input). toggle() switches to the next state, like clicking it.for (const e of sim.getElements()) if (e.getType() == "LogicInputElm") e.setPosition(1);loadContents(data, startAddr) (For SRAMElm and ROMElm)data is one of: an array of values (stored starting at startAddr, default 0); an array of runs, [addr, [values...], addr, [values...], ...] (startAddr is ignored); or a string in the same format as the Contents field in the edit dialog ("addr: val val val...", one line per run). Any previous contents are cleared. Throws an error if the data can't be parsed.ram.loadContents([0x3E, 0x05, 0xC9]);, ram.loadContents([0, [1, 2, 3], 16, [4, 5]]); or ram.loadContents("0: 1 2 3\n16: 4 5");getContents(), getValue(addr), setValue(addr, value), getAddressBits(), getDataBits() (For SRAMElm and ROMElm)getContents() returns the contents as text (Contents field format). getValue/setValue read or write a single address. getAddressBits/getDataBits return the memory's size.var v = ram.getValue(16); ram.setValue(16, v+1);getPostCount()var postCount = element.getPostCount();sim.onupdate = didUpdate;
...
function didUpdate(sim) {
// Update UI with simulation data
}
sim.ontimestep = didStep;
...
function didStep(sim) {
// Perform actions per time step
}
sim.onanalyze = didAnalyze;
...
function didAnalyze(sim) {
// React to changes in circuit
}
setPosition() or toggle().
sim.onswitch = didSwitch;
...
function didSwitch(sim, elm) {
console.log(elm.getType() + " is now in position " + elm.getPosition());
}