Class HtmlSimulation
java.lang.Object
com.eco.bio7.spatial.HtmlSimulation
Exports an interactive simulation to an HTML page (a single file without
dependencies, WebGL for 3D): Play/Pause, Step, Reset, the speed and controls
for the settings (sliders, number fields, text fields, check boxes, choices
and buttons) with a 2D grid view (e.g., a cellular automaton), a 3D view
(e.g., agents of a swarm model) or a dynamic 3D surface and an optional chart
of values over time.
The simulation runs in the web browser: the model is written in JavaScript with the functions setup(sim) (called at the start and with Reset) and step(sim) (one time step). The settings are available as sim.p.name. The view displays (an optional function draw(sim) is called after the view is drawn):
- GRID_2D: sim.cells (an array of sim.width * sim.height values, index y * width + x) with sim.palette (colors of the integer states, e.g., ['#ffffff', '#000000']) or the color map (or the colors of sim.ramp) with sim.min and sim.max and optional agents sim.agents = {x: [], y: [], color: '#d62728', radius: 0.5} (cell units).
- SCENE_3D: sim.box = [w, h, d] and sim.points = {xyz: [x0, y0, z0, x1, ...], colors: [r, g, b, ...] (0 - 1, optional), color: '#1f77b4', radius: 0.01} and sim.lines = {xyz: [...], color: '#555555'} (pairs of points).
- SURFACE_3D: sim.surface = {nx, ny, z: [...] (index i * ny + j), min, max} colored with the color map (and points and lines like SCENE_3D).
Example:
new HtmlSimulation("Game of Life").view(HtmlSimulation.View.GRID_2D).slider("density", "Start density", 0, 1, 0.01, 0.3)
.number("size", "Grid size", 100).restartOn("size", "density").chart("Population").code(js).export("/path/life.html");
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionHtmlSimulation(String title) Creates a simulation with a title (the math markup of the plots, e.g., x^2, N_0, is possible). -
Method Summary
Modifier and TypeMethodDescriptionThe accent color of the page: "blue" (default), "teal", "violet", "orange" or "graphite".asynchronous(boolean asynchronous) The functions setup(sim), step(sim) and the functions of the buttons can return a Promise (e.g., a model of R in webR): the calls run one after the other (default false: synchronous functions).autoplay(boolean autoplay) Starts the simulation when the page is opened (default false).autoRotate(boolean rotate) Rotates the camera of the 3D view slowly (also a switch of the page).background(String color) The background color of the view (e.g., "#ffffff", null: the color of the theme).A button which calls a function of the code with the simulation (e.g., "clear" for function clear(sim)).Displays a chart of the values of sim.chart(name, value) over the steps.A check box (sim.p.name is true or false).A choice of options (sim.p.name is the selected option).The JavaScript code of the model with the functions setup(sim) and step(sim).The JavaScript code of the model from a file (the code is embedded in the page, see scriptFile).colorMap(int[] ramp) The colors of the color map (e.g., PlotLegend.VIRIDIS) for continuous values.description(String description) A description below the title (plain text, lines separated with \n).embedScript(boolean embed) Merges the script file (see scriptFile) into the page when it is exported: one HTML file without the script file (the script file stays the source to edit the model).Writes the HTML page.A heading between the controls.inputs(Collection<String> events) The input of the view for the model: "click", "drag" and/or "key".The layout of the page: "classic" (a sidebar with the controls, the default), "dashboard" (a top bar with Play/Pause, the view, the settings on the right), "focus" (the view fills the window, floating panels which can be hidden) or "story" (one column: the title, the view, the controls, the settings in a grid, the chart).map(double south, double west, double north, double east, String basemap, double opacity, String attribution) Shows the 2D grid on a map (online base maps with Leaflet): the grid covers the geographic bounds (WGS84 degrees; the grid is drawn in Web Mercator, exact for a grid in EPSG:3857).A number field (sim.p.name).static voidOpens a file (e.g., an exported page) with the default application of the system (the web browser).The settings which restart the simulation (setup) if they are changed (e.g., the size of a grid).The look of the 3D views (SCENE_3D, SURFACE_3D).sceneControls(boolean show) Shows the "View" panel of the 3D views in the page (default: true).scriptFile(String file) The JavaScript code of the model in a separate file (e.g., edited in an editor of Bio7): the page loads the file (script src) and export copies the file next to the page (if it is in another folder).seed(int seed) The seed of the random numbers of the setup (default 42; sim.seed in the code; the page has a field Seed: the same seed gives the same run).A slider for a number (sim.p.name).speed(double stepsPerSecond) The default speed in steps per second (default 20).A text field (sim.p.name is a string).The theme of the page: "auto" (the light or dark setting of the system, the default), "light" or "dark".timeline(int frames) Displays a timeline slider (0 to frames - 1) for the steps: moving the slider calls the function seek(sim, step) of the code (e.g., to display a recorded frame, see SimulationRecorder) and sets sim.t.toHtml()Returns the HTML page.view(HtmlSimulation.View view) The view (default GRID_2D).static booleanwriteScriptTemplate(String file) Writes a template of a JavaScript file of a model (setup and step with comments of the available values) if the file does not exist (e.g., to edit the model in an editor of Bio7, see scriptFile).
-
Constructor Details
-
HtmlSimulation
Creates a simulation with a title (the math markup of the plots, e.g., x^2, N_0, is possible).
-
-
Method Details
-
description
A description below the title (plain text, lines separated with \n). -
view
The view (default GRID_2D). -
colorMap
The colors of the color map (e.g., PlotLegend.VIRIDIS) for continuous values. -
seed
The seed of the random numbers of the setup (default 42; sim.seed in the code; the page has a field Seed: the same seed gives the same run). -
speed
The default speed in steps per second (default 20). -
asynchronous
The functions setup(sim), step(sim) and the functions of the buttons can return a Promise (e.g., a model of R in webR): the calls run one after the other (default false: synchronous functions). -
inputs
The input of the view for the model: "click", "drag" and/or "key". The page calls the function input(sim, ev) of the code with ev = {type: 'click', x, y} (a grid: cells, x and y from the top left; the 3D view: the point on the floor), {type: 'drag', x, y} (the 2D view: a drag, the 3D view: Shift + drag) or {type: 'key', key} (the key of the keyboard event). Default: none. -
autoplay
Starts the simulation when the page is opened (default false). -
layout
The layout of the page: "classic" (a sidebar with the controls, the default), "dashboard" (a top bar with Play/Pause, the view, the settings on the right), "focus" (the view fills the window, floating panels which can be hidden) or "story" (one column: the title, the view, the controls, the settings in a grid, the chart). All layouts adapt to narrow windows (phones). -
accent
The accent color of the page: "blue" (default), "teal", "violet", "orange" or "graphite". -
theme
The theme of the page: "auto" (the light or dark setting of the system, the default), "light" or "dark". The page has a button to change it. -
scene
public HtmlSimulation scene(String background, String lighting, String shape, String floor, boolean shadows) The look of the 3D views (SCENE_3D, SURFACE_3D). The page has a "View" panel to change it (see sceneControls).- Parameters:
background- "auto" (studio or night as the theme), "studio", "night", "sky", "sunset", "ocean", "paper" or "plain"lighting- "studio", "soft", "dramatic" or "flat"shape- the shape of the points: "sphere", "cube", "cone", "glider" (cones and gliders point in the direction of the movement) or "point" (fast point sprites)floor- "grid", "plane" or "none"shadows- soft shadows of the points on the floor
-
map
public HtmlSimulation map(double south, double west, double north, double east, String basemap, double opacity, String attribution) Shows the 2D grid on a map (online base maps with Leaflet): the grid covers the geographic bounds (WGS84 degrees; the grid is drawn in Web Mercator, exact for a grid in EPSG:3857). NaN bounds: the map shows sim.mapBounds of the model (e.g., agents with sim.geoAgents = {lon, lat, colors, sizes, labels}). The page has a "Map" panel (the base map, the opacity, the map on or off). Colors with an alpha (#rrggbbaa) show the map.- Parameters:
basemap- "topo" (OpenTopoMap, CC-BY-SA) or "osm" (OpenStreetMap: only for a page on a web server, its tile servers block a local file); both need no key and show their attributionopacity- the opacity of the grid (0 - 1)attribution- the source of the data (shown on the map), e.g., "Terrain Tiles"
-
autoRotate
Rotates the camera of the 3D view slowly (also a switch of the page). -
sceneControls
Shows the "View" panel of the 3D views in the page (default: true). -
background
The background color of the view (e.g., "#ffffff", null: the color of the theme). -
timeline
Displays a timeline slider (0 to frames - 1) for the steps: moving the slider calls the function seek(sim, step) of the code (e.g., to display a recorded frame, see SimulationRecorder) and sets sim.t. -
chart
Displays a chart of the values of sim.chart(name, value) over the steps. -
slider
public HtmlSimulation slider(String name, String label, double min, double max, double step, double value) A slider for a number (sim.p.name). -
number
A number field (sim.p.name). -
text
A text field (sim.p.name is a string). -
checkbox
A check box (sim.p.name is true or false). -
choice
A choice of options (sim.p.name is the selected option). -
button
A button which calls a function of the code with the simulation (e.g., "clear" for function clear(sim)). -
heading
A heading between the controls. -
restartOn
The settings which restart the simulation (setup) if they are changed (e.g., the size of a grid). -
code
The JavaScript code of the model with the functions setup(sim) and step(sim). -
codeFile
The JavaScript code of the model from a file (the code is embedded in the page, see scriptFile).- Throws:
IOException
-
embedScript
Merges the script file (see scriptFile) into the page when it is exported: one HTML file without the script file (the script file stays the source to edit the model). Default false: the page loads the script file next to it.- Parameters:
embed- true for one HTML file- Returns:
- the simulation
-
scriptFile
The JavaScript code of the model in a separate file (e.g., edited in an editor of Bio7): the page loads the file (script src) and export copies the file next to the page (if it is in another folder). The page and the script file belong together (the script file is loaded when the page is opened).- Parameters:
file- the JavaScript file with setup(sim) and step(sim) (see writeScriptTemplate)- Returns:
- the simulation
-
writeScriptTemplate
Writes a template of a JavaScript file of a model (setup and step with comments of the available values) if the file does not exist (e.g., to edit the model in an editor of Bio7, see scriptFile).- Parameters:
file- the JavaScript file- Returns:
- true if the template was written (false if the file exists)
- Throws:
IOException- if the file cannot be written
-
toHtml
Returns the HTML page. -
export
Writes the HTML page.- Parameters:
file- the HTML file- Returns:
- the simulation
- Throws:
IOException- if the file cannot be written
-
open
Opens a file (e.g., an exported page) with the default application of the system (the web browser).
-