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()
var svgData = CircuitJS1.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();
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
}