Class HtmlSimulation

java.lang.Object
com.eco.bio7.spatial.HtmlSimulation

public class HtmlSimulation extends Object
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).
sim.chart('Name', value) adds a value to a series of the chart (one value per step), sim.clearChart() removes the values. sim.t is the number of the steps, sim.random() a random number and sim.pause() pauses the simulation (step(sim) can also return false to stop without counting the step, or 'stop' to stop after the step). An optional (async) function prepare(sim) is called once before the start (e.g., to load data).

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
    Modifier and Type
    Class
    Description
    static enum 
    The view of the simulation.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a simulation with a title (the math markup of the plots, e.g., x^2, N_0, is possible).
  • Method Summary

    Modifier and Type
    Method
    Description
    accent(String accent)
    The 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).
    The background color of the view (e.g., "#ffffff", null: the color of the theme).
    button(String label, String function)
    A button which calls a function of the code with the simulation (e.g., "clear" for function clear(sim)).
    chart(String title)
    Displays a chart of the values of sim.chart(name, value) over the steps.
    checkbox(String name, String label, boolean value)
    A check box (sim.p.name is true or false).
    choice(String name, String label, String[] options, String value)
    A choice of options (sim.p.name is the selected option).
    code(String javaScript)
    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).
    export(String file)
    Writes the HTML page.
    heading(String label)
    A heading between the controls.
    The input of the view for the model: "click", "drag" and/or "key".
    layout(String 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).
    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).
    number(String name, String label, double value)
    A number field (sim.p.name).
    static void
    open(String file)
    Opens a file (e.g., an exported page) with the default application of the system (the web browser).
    restartOn(String... names)
    The settings which restart the simulation (setup) if they are changed (e.g., the size of a grid).
    scene(String background, String lighting, String shape, String floor, boolean shadows)
    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).
    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).
    slider(String name, String label, double min, double max, double step, double value)
    A slider for a number (sim.p.name).
    speed(double stepsPerSecond)
    The default speed in steps per second (default 20).
    text(String name, String label, String value)
    A text field (sim.p.name is a string).
    theme(String theme)
    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.
    Returns the HTML page.
    The view (default GRID_2D).
    static boolean
    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).

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • HtmlSimulation

      public HtmlSimulation(String title)
      Creates a simulation with a title (the math markup of the plots, e.g., x^2, N_0, is possible).
  • Method Details

    • description

      public HtmlSimulation description(String description)
      A description below the title (plain text, lines separated with \n).
    • view

      public HtmlSimulation view(HtmlSimulation.View view)
      The view (default GRID_2D).
    • colorMap

      public HtmlSimulation colorMap(int[] ramp)
      The colors of the color map (e.g., PlotLegend.VIRIDIS) for continuous values.
    • seed

      public HtmlSimulation 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).
    • speed

      public HtmlSimulation speed(double stepsPerSecond)
      The default speed in steps per second (default 20).
    • asynchronous

      public HtmlSimulation 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).
    • inputs

      public HtmlSimulation inputs(Collection<String> events)
      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

      public HtmlSimulation autoplay(boolean autoplay)
      Starts the simulation when the page is opened (default false).
    • layout

      public HtmlSimulation layout(String 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

      public HtmlSimulation accent(String accent)
      The accent color of the page: "blue" (default), "teal", "violet", "orange" or "graphite".
    • theme

      public HtmlSimulation theme(String 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 attribution
      opacity - the opacity of the grid (0 - 1)
      attribution - the source of the data (shown on the map), e.g., "Terrain Tiles"
    • autoRotate

      public HtmlSimulation autoRotate(boolean rotate)
      Rotates the camera of the 3D view slowly (also a switch of the page).
    • sceneControls

      public HtmlSimulation sceneControls(boolean show)
      Shows the "View" panel of the 3D views in the page (default: true).
    • background

      public HtmlSimulation background(String color)
      The background color of the view (e.g., "#ffffff", null: the color of the theme).
    • timeline

      public HtmlSimulation 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.
    • chart

      public HtmlSimulation chart(String title)
      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

      public HtmlSimulation number(String name, String label, double value)
      A number field (sim.p.name).
    • text

      public HtmlSimulation text(String name, String label, String value)
      A text field (sim.p.name is a string).
    • checkbox

      public HtmlSimulation checkbox(String name, String label, boolean value)
      A check box (sim.p.name is true or false).
    • choice

      public HtmlSimulation choice(String name, String label, String[] options, String value)
      A choice of options (sim.p.name is the selected option).
    • button

      public HtmlSimulation button(String label, String function)
      A button which calls a function of the code with the simulation (e.g., "clear" for function clear(sim)).
    • heading

      public HtmlSimulation heading(String label)
      A heading between the controls.
    • restartOn

      public HtmlSimulation restartOn(String... names)
      The settings which restart the simulation (setup) if they are changed (e.g., the size of a grid).
    • code

      public HtmlSimulation code(String javaScript)
      The JavaScript code of the model with the functions setup(sim) and step(sim).
    • codeFile

      public HtmlSimulation codeFile(String file) throws IOException
      The JavaScript code of the model from a file (the code is embedded in the page, see scriptFile).
      Throws:
      IOException
    • embedScript

      public HtmlSimulation 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). Default false: the page loads the script file next to it.
      Parameters:
      embed - true for one HTML file
      Returns:
      the simulation
    • scriptFile

      public HtmlSimulation 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). 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

      public static boolean writeScriptTemplate(String file) throws IOException
      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

      public String toHtml()
      Returns the HTML page.
    • export

      public HtmlSimulation export(String file) throws IOException
      Writes the HTML page.
      Parameters:
      file - the HTML file
      Returns:
      the simulation
      Throws:
      IOException - if the file cannot be written
    • open

      public static void open(String file)
      Opens a file (e.g., an exported page) with the default application of the system (the web browser).