API Overview

The Flux API lets you write code that interacts with Flux documents and the circuit simulator. This page tells you what you need to get started.

Prerequisites

In Flux, you write code in JavaScript or TypeScript. The Flux API hides much of the complexity of electronic circuit design and simulation. You need some basic experience with JavaScript or TypeScript to build things in Flux.

If you have no experience in programming or web development, free educational resources can help you. Some popular resources are:

For more complex projects, use TypeScript. TypeScript makes the API easier to navigate and helps you write robust models. You do not need to learn TypeScript before you start.

Set up

You write and run your code in the built-in code editor of a Flux project. The code of a document is a model. For example, a model can simulate the behavior of a basic electrical part.

To learn how the code editor runs your model, see How the API works.

The flux global object

Most of the model API is accessible from the flux global object. This includes functions to create nodes, access the nodes, show notifications, and much more.

flux.print("Hello world!");
flux.createOutputNode();

For the full list of methods and properties, see Flux global object.

Access the document

Most models access objects in the document, called nodes, to read or change them.

  • To get all nodes of the document, use flux.nodes.

  • To find one node by its uid, use flux.getNodeById().

For the node types, see Object types.

Edit properties

To edit a node property that is a number or a string, change the property on the node object. For example, this code moves a node 10 pixels to the right:

node.x += 10;

Properties that are arrays or objects are readonly. For example, this code does not work:

// error: object is not extensible
flux.properties.push(someProperty);

// error: Cannot assign to read only property 'name' of object
const name = element.name;
name = "some new name";

To change one of these properties, set the entire property. Make a copy of the original property, change the copy, and then set the property to the copy.

To copy an array of nodes like properties, but not the nodes themselves, use array.slice(). Other array methods that return a new array include array.concat() and array.map(). Most array methods, such as array.push(), do not create a new array. Use them on a copy of the array.

const properties = flux.properties.slice();
properties.push(someProperty);
flux.properties = properties;

Next steps