React-sim
GitHub

A single-page reference of all the APIs used throughout React-Sim.

Model

Usage

import {Model} from 'react-sim';
function initData = params => { /* ... */ }
function updateData =
({data, params, tick, ...args}) => { /* ... */ }
const params = { /* ... */};
function Frame =
({data, params, tick, ...args}) => <>{ /* ... */ }</>;
export function MyModel = () => <Model
initalParams={params}
initData={initData}
updateData={updateData}
>
<Frame />
</Model>

State

NameDescription
data
any
Represents the current state of the simulation.
tick
integer
A measure of time in the simulation. Each time the simulation updates, ticks increases by one.
params
Object
The parameters of the simulation.
isPlaying
boolean
Whether the simulation is running or not.
results
any
Is updated each time the simulation finishes, and contains information on the past runs.

The difference between data and params is that data is being updated at each simulation tick, without user intervention, through the updateData function, whereas params is only modified when the user interacts with the controls.

The built-in parameters are:

NameDescription
delay
number
delay between animation refreshes in miliseconds.
maxTime
integer
the value of tick at which the animation stops.
minTime
integer
the value of tick when the animation starts.
ticksPerAnimation
integer
the number of simulation ticks per animation refresh. If more than one, the simulation will update several times before it's rendered again.

Properties

NameDescription
initialTick
number, optional, default 0
The initial value for tick.
initalParams
object, optional
The initial value for the params of the simulation. When the simulation starts, it is used to populate params in the state.

The following properties can be used as is, or be passed inside initialParams,i.e.

<Model delay={100} />

or

<Model initialParams={{ delay: 100 }} />

initialParams takes precedence if both are used. They provide an initial value for the corresponding parts of the state.

NameDescription
delay
number, optional, default 0
The delay between each animation refresh. If not specified, animations happen as fast as possible (up to 60 times per second).
minTime
integer, optional, default 0
The value of tick when the animation starts
maxTime
integer, optional, default 100
The value of tick at which the animation stops
ticksPerAnimation
integer, optional, default 1
The number of simulation ticks per animation refresh. If more than one, the simulation will update several times before it's rendered again.
isPlaying
boolean, default false
Whether the simulation plays automatically or not. If this is updated by external components, the simulation will stop or resume.
loop
boolean, default false
If set to true, the simulation restarts whenever it's stopped playing.
nocache
boolean, default false
If set to true, the simulation won't cache its data. If data is cached, then the user can go back in time without having to recalculate data.
controls
control object, optional
Describes the UI of the simulation. Controls is how the params in the state can be modified.
noControls
boolean, optional, default false
If set to true, the default controls won't be shown.
showTimer
boolean, optional, default true
If set to false, the default timer won't be displayed.
showTime
boolean, optional, default true
If set to false, only the play, stop and step-in button will be displayed, the slider and the indication of time won't be shown.
showTimerSlider
boolean, optional, default true
If set to false, the timer slider won't be displayed, though the indication of time may be.
initData
function, optional
The function that initializes the data of the model, when it first starts or when it's reset.
It takes params (in the state) as input, and outputs a value for data (which it puts in the state).
updateData
function, optional
This function updates the data at each tick of the simualtion

Its arguments are of the form: ({cachedData, complete, data, params, pause, stop}). Its output will replace data in the state.

NameDescription
data
any
Represents the current value of data.
tick
integer
Represents the new tick.
params
object
Represents the current parameters.
cachedData
object
An object so that for each past value of tick, cachedData[tick] represents the value of data at that moment of the simulation.
complete
function
A function that signals the simulation is complete (it will also stop).
pause
function
A function that will stop the simulation from automatically updating and animating.
stop
function
A function that will stop the simulation from automatically updating and animating, and will reset it.

Style

Model uses the rebass component system. Rebass is part of a larger ecosystem that includes theme-ui and the styled system.

As such, Model can take a theme-ui theme and styled system sx props.

NameDescription
theme
object, optional
A theme-ui theme.
sx
object, optional
A styled system style prop.
export const _frontmatter =

For more information on controls and the syntax of the control object, see Controls.

Frame Helpers

withFrame

Usage

import {Model, withFrame} from 'react-sim';
const FrameComponent = props => <>{ /* ... */ }</>;
const Frame = withFrame(FrameComponent);
export function MyModel = () => <Model>
<Frame />
</Model>

API

withFrame is a wrapper around a Frame component.

Wrapping a component with withFrame will expose the connected component to the following properties: These properties come from the state of the Model this component is a descendant of.

NameDescription
data
anything
the current state of the simulation
tick
integer
the progress in the simulation
params
object
all the parameters set by the user/author
cachedData
object
data from previous ticks of the simulation
results
array
data from previous completed runs of the simulation
initData
function
the Frame can reinitialize data
setData
function
the Frame can change the data (so for instance the Frame can be used to let the user enter an initial state of data)

For convenience, a component which is a direct child of a Model automatically receives these properties without withFrame.

import {Model, withFrame} from 'react-sim';
const FrameComponent = props => <>{ /* ... */ }</>;
const Frame = withFrame(FrameComponent);
export function MyModel = () => <Model>
{/* this component has access to frame props */}
<FrameComponent />
<div>
{/* this one doesn't, because it's not a direct child */}
<FrameComponent />
{/* this component has access to frame props, because it's connected */}
<Frame />
</div>
</Model>

Grid

see Grid guide

Usage

import {Model, Grid} from 'react-sim';
export function MyModel = () => <Model>
<Grid />
</Model>

API

NameDescription
data
Array
It expects data to be a 2-dimensional array of data.
accessor
function
How to determine how to color each cell. The inputs of the function are:
cell (content of the cell), x , y. The output is: a color. The default value is:
cell => cell ? 0 : 'none' : '#000'
size
string/integer, default: 12
How big the squares will be in pixels.
cellProps
anything
Any extra prop to pass to each individual square

Any other prop will be passed to the Grid.

GridComponent

import { GridComponent } from 'react-sim';

Grid is wrapped with withFrame so it can be used anywhere in the tree. The corresponding, unwrapped component, GridComponent, can also be exported from react-sim.

CanvasFrame

see CanvasFrame guide

Usage

import {Model, CanvasFrame} from 'react-sim';
function draw({ctx, data, ...args}) => {
/* canvas operations */
}
export function MyModel = () => <Model>
<CanvasFrame draw={draw} />
</Model>

API

All the connected Frame context props, plus:

NameDescription
draw
function, optional
A function which will be called each time CanvasFrame is updated (ie at each animation cycle)
height
number, optional
The height of the canvas. If ignored, CanvasFrame will used params.height.
width
number, optional
The width of the canvas. If ignored, CanvasFrame will used params.width.

Circle

circle takes an object as argument with the following properties:

All the CanvasFrame properties, plus:

NameDescription
x
number
x coordinate of circle center
y
number
y coordinate of circle center
r
number
Radius of circle

RoundRectangle

roundRectangle takes an object as argument with the following properties:

All the CanvasFrame properties, plus:

NameDescription
x
number
x coordinate of top-left corner of rectangle
y
number
y coordinate of top-left corner of rectangle
width
number
Width of rectangle
height
number
Height of rectangle
tl
number, optional
Radius of top-left corner
tr
number, optional
Radius of top-right corner
bl
number, optional
Radius of bottom-left corner
br
number, optional
Radius of botrom-right corner
r
number, optional
Radius of all unspecified corners

Draw

All the CanvasFrame properties, plus:

NameDescription
canvas
DOM Node
The actual canvas node
ctx
Canvas rendering context
the corresponding 2d context.
Exactly what you'd get if you typed
const ctx = canvas.getContext('2d');
circle
Helper function
see circle API
roundRectangle
Helper function
see roundRectangle API

TimeSeries

see TimeSeries guide.

Series

series is an object with three properties:

NameDescription
label
string
Describes the series,
color
string
Color of the series,
accessor
function
Function that turns a data object into the value that you want to plot.

TimeSeries

NameDescription
series
series object or array
Either a single series object or an array of series. the accessor function is going to be called on every item in cachedData and the result will be plotted.
stacked
boolean, default: false
If true, represents the time series as full-height, stacked area charts. If false, the series are line charts.
height
number, optional
Height of the chart.
padding
number, optional
Padding of the chart.
width
number, optional
Width of the chart.

Counter

NameDescription
series
series object or array
Either a single series object or an array of series. the accessor function is going to be called on every item in data and a single number will be shown per series.
height
number, optional
Height of the counter card.
padding
number, optional
Padding of the counter card.
width
number, optional
Width of the counter card.

Indicator

NameDescription
series
series object or array
A single series object. The accessor function is going to be called on every item in data and a single number will be shown per series.
height
number, optional
Height of the indicator card.
padding
number, optional
Padding of the indicator card.
width
number, optional
Width of the indicator card.

Controls

See Controls guide.

withControls

Components wrapped with withControls gain access to the following properties:

NameDescription
isPlaying
boolean
tells your custom control if the simulation is playing.
params
object
any and all of the simulation parameters.
pause
function
call this function to pause the simulation (it stops playing, but is not reset.)
play
function
call this function to start or resume playing the simulation.
stop
function
call this function to stop the simulation (stops playing, time is reset to default, data of simulation is reset.)
updateTime
fuction
call with a number to update the tick value in the simulation.

Common Controls Properties

Unless otherwise specified, all built-in controls have the following properties:

NameDescription
type
string, optional, 'range' by default
The kind of control we're describing. Can be one of: checkbox, input, radio, range, select, timer or toggle.
label
string, optional
The label which will describe the control.
name
string, optional
The name of the control. If not specified, label will be used.
param
string, optional
The param that this control will update.
resetOnChange
boolean, optional, false by default
Whether the model will be reset if the control changes.
setParams
function, optional
See below.
setValue
function
This function will be called whenever the control changes to update its value.
value
any
The current value of the control.

setParams

By default, a control, when changed, will take its value and use it to update the param of the control, as is. Instead, a setParams function can be created that takes, as input, the value of the control, and which will output an object with one or more params as keys. With a setParams function, a control can update several params at once, or have a transformation between the value of the control and what is passed to the params.

Range

See the Range guide.

All Common control props properties, plus:

NameDescription
maxValue
number, optional, default 100
The value corresponding to the end of the range.
minValue
number, optional, default 0
The value corresponding to the beginning of the range.
name
string, optional
The name of the control. If not specified, label is used.
shouldDisplayLabel
boolean, default true
Whether the label should be displayed.
shouldDisplayMaxValue
boolean, default true
Whether the maximum value should be displayed to the right of the range.
shouldDisplayMinValue
boolean, default true
Whether the minimum value should be displayed to the left of the range.
shouldDisplayValue
boolean, default true
Whether the value should be displayed next to the label.
shouldDisplaySlider
boolean, default true
Whether the slider should be displayed. If the slider is not displayed, the min and max values are also hidden.
step
number, optional, default 1
The amount by which the value will be incremeneted or decremented when moving the range cursor.

Timer

Default Timer

See the Default timer guide.

By default a Model comes with a built-in Timer control. For this defaut Timer, the following props come directly from Model (see Model properties.)

NameDescription
minTime
integer, 0 by default
at which position the timer starts. Can be negative.
maxTime
integer, 100 by default
at which position the timer ends.
showTime
boolean, true by default
Whether the time block (i.e. slider and time value) is going to be shown.
showTimeSlider
boolean, true by default
Whether the time slider is going to be shown.
showTimer
boolean, true by default
Whether the Timer is shown at all. If the Timer is not shown, you will have to provide another way to start the simulation (or effectively force the user to stay on the initial state).

General case

See the Timer guide.

If the Timer is defined anywhere else, these properties can be passed to the Timer directly.

The Default timer properties, plus:

NameDescription
maxTime
integer, 100 by default
At which position the timer ends.
minTime
integer, 0 by default
At which position the timer starts. Can be negative.
label
string, optional
The label which will describe the control.
name
string, optional
The name of the control. If not specified, label is used.
shouldDisplayLabel
boolean, default true
Whether the label should be displayed.
shouldDisplayMaxValue
boolean, default true
Whether the maximum value should be displayed to the right of the range.
shouldDisplayMinValue
boolean, default true
Whether the minimum value should be displayed to the left of the range.
shouldDisplayValue
boolean, default true
Whether the value should be displayed next to the label.
showTime
boolean, true by default
Whether the time block (i.e. slider and time value) is going to be shown.
showTimeSlider
boolean, true by default
Whether the time slider is going to be shown.
time
number
The current value of time shown on this Timer
updateTime
function
The function that will update this Timer's time when the

Unlike the other built-in controls, Timer does not take a value or a setValue parameter.

Checkbox

See the Checkbox guide.

Its API is solely the Common Control Props.

Input

See the Input guide.

Its API is solely the Common Control Props.

Radio

See the Radio guide.

The Common controls props properties, plus:

NameDescription
vertical
boolean, optional, false by default
Whether the options are shown horizontally or vertically.
NameDescription
options
array option objects
options is an array of option objects, which have label and value properties. label is a string, value can be a string, a number or a boolean. label is what is displayed, value is the value that will be passed when the control changes. If label and value are identical, instead of an object a simple string can be used.

Select

See the Select guide.

The Common controls props properties, plus:

NameDescription
vertical
boolean, optional, false by default
Whether the options are shown horizontally or vertically.
NameDescription
options
array option objects
options is an array of option objects, which have label and value properties. label is a string, value can be a string, a number or a boolean. label is what is displayed, value is the value that will be passed when the control changes. If label and value are identical, instead of an object a simple string can be used.

Toggle

See the Toggle guide.

Its API is solely the Common Control Props.

Edit this page on GitHub
Previous:
Time Series
Next:
Examples
React-SimGitHub