API Docs for:
Show:

Conway Class

Constructor

Conway

(
  • config
)

Parameters:

  • config Object

    Configuration Object.

Item Index

Properties

Events

Methods

_cellChange

(
  • eventFacade
)
protected chainable

Default function for the cellChange event.

Parameters:

_cellStep

(
  • grid
  • height
  • width
  • index
  • birth
  • life
  • toroidal
)
Object protected

Determines the outcome of one step of the simulation for one cell.

Parameters:

Returns:

Object: If the cell changed, returns an object with the following properties:
cell
Boolean. The current value of the cell.
index
Number. The cell's array index.
x
Number. The cell's x coordinate.
y
Number. The cell's y coordinate.
otherwise returns null.

getCell

(
  • x
  • [y]
)
Boolean

Returns the value of a specific cell. The cell may be specified with x and y coordinates or with a single array index. (index = x + y * width)

Parameters:

  • x Number

    The x coordinate or the array index.

  • [y] Number optional

    Optional. The y coordinate.

Returns:

Boolean: A truthy value means the cell is currently alive.

setCell

(
  • x
  • [y]
  • value
)
chainable

Sets the value of a specific cell. The cell may be specified with x and y coordinates or with a single array index. (index = x + y * width)

Parameters:

  • x Number

    The x coordinate orthe array index.

  • [y] Number optional

    Optional. The y coordinate.

  • value Boolean

    The new value for the cell. A truthy value means the cell is currently alive.

step

(
  • [callbackFunction]
)
chainable

Runs one step of the simulation. The simulation is resolved asynchronously and will call the callback function when it completes. Once this method is called, it should not be called again until the first call is complete. Calling getCell, setCell, or step while the simulation is still being resolved may cause unexpected side effects. If you wish to ensure that unexpected side effects never occur and don't mind incurring a small overhead cost, use gallery-mutex to obtain an exclusive lock when calling these methods.

Parameters:

  • [callbackFunction] Function optional

    Optional. This function will be called once the simulation is resolved. This function will be passed a single argument, an array. For each cell that changed value during the simulation, there will be an object in the array with the following properties:

    cell
    Boolean. The current value of the cell.
    index
    Number. The cell's array index.
    x
    Number. The cell's x coordinate.
    y
    Number. The cell's y coordinate.

Properties

_history

Object protected

This object's keys are the indices of cells that have changed value since the previous step of the simulation. This is used to improve performance while resolving the next step of the simulation.

Attributes

birth

[Number]

In Conway's Game of Life, a cell is born (becomes alive) if it is not currently alive and if it has exactly three living neighbors. This is the default functionality but it can be modified. For example, if this attribute is set to [1,4,6] a cell will be born if it is not currently alive and if it has exactly one, four, or six living neighbors.

Default: [3]

Fires event birthChange

Fires when the value for the configuration attribute birth is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

grid

[Boolean]

The grid of cell values. The array values should never be manipulated directly; doing so may cause unexpected side effects.

Fires event gridChange

Fires when the value for the configuration attribute grid is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

height

Number

The height of the grid.

Fires event heightChange

Fires when the value for the configuration attribute height is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

life

[Number]

In Conway's Game of Life, a living cell remains alive if it currently has exactly two or three living neighbors. Otherwise the cell will die either from loneliness or overcrowding. This is the default functionality but it can be modified. For example, if this attribute is set to [1,3,5] a living cell will remain alive if it has exactly one, three, or five living neighbors.

Default: [2,3]

Fires event lifeChange

Fires when the value for the configuration attribute life is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

toroidal

Boolean

Since the grid is a fixed size, there is a good chance that the simulation will have to interact with the boundaries of the grid. By default, the grid exists on an infinite two-dimensional plane but all cells beyond the limits of the grid are considered to be dead. When this attribute is set to a truthy value, the universe of the simulation will become a three-dimensional torus instead of a two-dimensional plane. The grid will represent the entire surface area of the torus. Basically all this means is, when the grid is toroidal, the left and right edges of the grid will be connected and the top and bottom edges of the grid will be connected, so while there is still a finite area there are no real boundaries.

Default: false

Fires event toroidalChange

Fires when the value for the configuration attribute toroidal is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

width

Number

The width of the grid.

Fires event widthChange

Fires when the value for the configuration attribute width is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

Events

cellChange

If this event is prevented, the cell's value will not be changed.

Event Payload:

  • cell Boolean

    The current value of the cell. Note that the cell's value is changed by the default function so the value of this property will be different between on and after subscriptions.

  • height Number

    The current height of the grid.

  • index Number

    The cell's array index.

  • toroidal Boolean

    True if the grid is currently toroidal.

  • width Number

    The current width of the grid.

  • x Number

    The cell's x coordinate.

  • y Number

    The cell's y coordinate.