A JavaScript implementation of Conway's Game of Life and other types of cellular automata. View demo here: codepen.io.
-
Add JavaScript code containing class
CellularAutomata
in an HTML document:<script src="js/cellular-automata.js" type="text/javascript"></script>
-
Add HTML element for the game in
body
section:<div id="ca-container"></div>
-
Сreate an instance of class
CellularAutomata
after theDOMContentLoaded
event and call methodstart
:document.addEventListener("DOMContentLoaded", () => { ca = new CellularAutomata("ca-container", 6, 100); ca.start(); });
Class
CellularAutomata
constructor parameters:containerId
— ID of HTML element for the game;cellSizePx
— size of cell in pixels, default 10 pixels;speedMs
— iteration time in milliseconds, default 1000 milliseconds.
To add new types modify this method of class CellularAutomata
:
class CellularAutomata {
...
setDefaultProperties() {
...
this.settings = {
...
types: [
{
name: "B3S23",
rules: { },
genFirstState: (cells, cellsCountX, cellsCountY) => { }
}