create-gojs-kit
Version:
A CLI for downloading GoJS samples, extensions, and docs
529 lines (479 loc) • 21.6 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover"/>
<meta name="description" content="A cellular automation simulation in GoJS" />
<meta itemprop="description" content="A cellular automation simulation in GoJS" />
<meta property="og:description" content="A cellular automation simulation in GoJS" />
<meta name="twitter:description" content="A cellular automation simulation in GoJS" />
<link rel="preconnect" href="https://rsms.me/">
<link rel="stylesheet" href="../assets/css/style.css">
<!-- Copyright 1998-2025 by Northwoods Software Corporation. -->
<meta itemprop="name" content="Game of Life Cellular Automation" />
<meta property="og:title" content="Game of Life Cellular Automation" />
<meta name="twitter:title" content="Game of Life Cellular Automation" />
<meta property="og:image" content="https://gojs.net/latest/assets/images/screenshots/gameoflife.png" />
<meta itemprop="image" content="https://gojs.net/latest/assets/images/screenshots/gameoflife.png" />
<meta name="twitter:image" content="https://gojs.net/latest/assets/images/screenshots/gameoflife.png" />
<meta property="og:url" content="https://gojs.net/latest/samples/gameOfLife.html" />
<meta property="twitter:url" content="https://gojs.net/latest/samples/gameOfLife.html" />
<meta name="twitter:card" content="summary_large_image" />
<meta property="og:type" content="website" />
<meta property="twitter:domain" content="gojs.net" />
<title>
Game of Life Cellular Automation | GoJS Diagramming Library
</title>
</head>
<body>
<!-- This top nav is not part of the sample code -->
<nav id="navTop" class=" w-full h-[var(--topnav-h)] z-30 bg-white border-b border-b-gray-200">
<div class="max-w-screen-xl mx-auto flex flex-wrap items-start justify-between px-4">
<a class="text-white bg-nwoods-primary font-bold !leading-[calc(var(--topnav-h)_-_1px)] my-0 px-2 text-4xl lg:text-5xl logo"
href="../">
GoJS
</a>
<div class="relative">
<button id="topnavButton" class="h-[calc(var(--topnav-h)_-_1px)] px-2 m-0 text-gray-900 bg-inherit shadow-none md:hidden hover:!bg-inherit hover:!text-nwoods-accent hover:!shadow-none" aria-label="Navigation">
<svg class="h-7 w-7 block" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25h16.5" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</button>
<div id="topnavList" class="hidden md:block">
<div class="absolute right-0 z-30 flex flex-col items-end rounded border border-gray-200 p-4 pl-12 shadow bg-white text-gray-900 font-semibold
md:flex-row md:space-x-4 md:items-start md:border-0 md:p-0 md:shadow-none md:bg-inherit">
<a href="../learn/">Learn</a>
<a href="../samples/">Samples</a>
<a href="../intro/">Intro</a>
<a href="../api/">API</a>
<a href="../download.html">Download</a>
<a href="https://forum.nwoods.com/c/gojs/11" target="_blank" rel="noopener">Forum</a>
<a id="tc" href="https://nwoods.com/contact.html"
target="_blank" rel="noopener" onclick="getOutboundLink('https://nwoods.com/contact.html', 'contact');">Contact</a>
<a id="tb" href="https://nwoods.com/sales/index.html"
target="_blank" rel="noopener" onclick="getOutboundLink('https://nwoods.com/sales/index.html', 'buy');">Buy</a>
</div>
</div>
</div>
</div>
</nav>
<script>
window.addEventListener("DOMContentLoaded", function () {
// topnav
var topButton = document.getElementById("topnavButton");
var topnavList = document.getElementById("topnavList");
if (topButton && topnavList) {
topButton.addEventListener("click", function (e) {
topnavList
.classList
.toggle("hidden");
e.stopPropagation();
});
document.addEventListener("click", function (e) {
// if the clicked element isn't the list, close the list
if (!topnavList.classList.contains("hidden") && !e.target.closest("#topnavList")) {
topButton.click();
}
});
// set active <a> element
var url = window
.location
.href
.toLowerCase();
var aTags = topnavList.getElementsByTagName('a');
for (var i = 0; i < aTags.length; i++) {
var lowerhref = aTags[i]
.href
.toLowerCase();
if (lowerhref.endsWith('.html'))
lowerhref = lowerhref.slice(0, -5);
if (url.startsWith(lowerhref)) {
aTags[i]
.classList
.add('active');
break;
}
}
}
});
</script>
<div class="flex flex-col prose">
<div class="w-full max-w-screen-xl mx-auto">
<!-- * * * * * * * * * * * * * -->
<!-- Start of GoJS sample code -->
<script src="https://cdn.jsdelivr.net/npm/gojs@3.1.0"></script>
<div id="allSampleContent" class="p-4 w-full">
<script id="code">
// two dimensional array which will represent the board state
// this will be filled with Nodes once they are created
const goLGrid = [];
// the size of the board
const rows = 40;
const cols = 40;
const interval = 15; // the interval between steps in ms when the simulation is enabled
let initializing = true;
let enabled = false; // flag to turn the simulation on or off
function init() {
myDiagram = new go.Diagram('myDiagramDiv', {
'animationManager.isEnabled': false,
// disable movement/edit controls, since the myDiagram is a static grid
'panningTool.isEnabled': false,
isReadOnly: true,
allowZoom: false,
allowSelect: false,
hasHorizontalScrollbar: false,
hasVerticalScrollbar: false,
initialAutoScale: go.AutoScale.Uniform
});
const nodeSize = 25;
const nodeDataArray = []; // array to hold Node data for the model
// populate data array by initializing Nodes in a grid and setting their "isAlive" state to false
for (let i = 0; i < rows; i++) {
const row = new Array(cols);
for (let j = 0; j < cols; j++) {
nodeDataArray.push({ location: new go.Point(j * nodeSize, i * nodeSize), row: i, col: j, isAlive: false });
}
goLGrid.push(row);
}
// stroke color and width of the grid lines
const gridStroke = '#A2A2A2';
const gridStrokeWidth = 1;
// define the node template, which also includes interactive functionality, such as clicking to toggle a node's state
myDiagram.nodeTemplate =
new go.Part({
isLayoutPositioned: false,
mouseEnter: (e, part, prev) => {
// set mouseover border
if (!initializing && !enabled) {
const shape = part.elt(0);
if (shape) {
shape.stroke = 'steelblue';
shape.strokeWidth = 3;
}
part.zOrder = 2; // ensure that the selection border is in front of all other Nodes by increasing its zOrder
// drag with a button down to add or erase cells
if ((e.buttons === 1 && !part.data.isAlive) || (e.buttons === 2 && part.data.isAlive)) {
select(part);
}
}
},
mouseLeave: (e, part, next) => {
// restore to original borders
const shape = part.elt(0);
if (shape) {
shape.stroke = gridStroke;
shape.strokeWidth = gridStrokeWidth;
}
part.zOrder = 1;
},
click: (e, part) => {
// left click to toggle cell
initializing = false;
if (!enabled) {
select(part);
}
},
contextClick: (e, part) => {
// right click to clear cell
initializing = false;
if (!enabled && part.data.isAlive) {
select(part);
}
},
zOrder: 1
})
.bind('location')
.add(
new go.Shape({
figure: 'Rectangle',
fill: 'white',
stroke: gridStroke,
strokeWidth: gridStrokeWidth,
width: nodeSize,
height: nodeSize
})
);
// use a simple model for our node data
myDiagram.model = new go.Model(nodeDataArray);
// myDiagram.parts is populated after a model is assigned;
// populate the internal gamestate array with the newly created nodes
for (const it = myDiagram.parts.iterator; it.next(); ) {
const part = it.value;
goLGrid[part.data.row][part.data.col] = part;
}
// load the default sample
const e = document.getElementById('samplePatterns');
loadSample(e.options[e.selectedIndex].value);
}
// toggles a given node's state, both visually and in the internal gamestate
function select(part) {
const shape = part.elt(0);
if (shape) {
if (shape.fill === 'white') {
shape.fill = 'steelblue';
} else {
shape.fill = 'white';
}
}
part.data.isAlive = !part.data.isAlive;
}
// toggles the state of the simulation, changing the button text from "Start" to "Pause" or back again
function toggleSimulation() {
initializing = false;
const button = document.getElementById('start');
if (!enabled) {
button.value = 'Pause';
enabled = true;
goLStep();
} else {
button.value = 'Start';
enabled = false;
}
}
// the callback for the step button, only steps forward if the simulation is currently stopped
function stepOnclick() {
initializing = false;
if (enabled) { toggleSimulation(); return; }
goLStep(true);
}
// performs a single step in the Game of Life
function goLStep(isManualStep) {
if (goLGrid.length === 0) {
return; // don't do anything if things aren't initialized yet
}
let isAlive = false;
const toSelect = [];
let liveCellCount = 0; // count the number of live cells to determine if there are no more left
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (isAlive) {
liveCellCount++;
}
// count the number of cells in the 8 squares adjacent to this one
let total = 0;
const above = goLGrid[i > 0 ? i - 1 : rows - 1];
const below = goLGrid[i + 1 < rows ? i + 1 : 0];
const left = j > 0 ? j - 1 : cols - 1;
const right = j + 1 < cols ? j + 1 : 0;
total += above[left].data.isAlive;
total += above[j].data.isAlive;
total += above[right].data.isAlive;
total += goLGrid[i][left].data.isAlive;
total += goLGrid[i][right].data.isAlive;
total += below[left].data.isAlive;
total += below[j].data.isAlive;
total += below[right].data.isAlive;
// toggle the cell if necessary according to the three rules
const part = goLGrid[i][j];
isAlive = part.data.isAlive;
if ((total <= 1 && isAlive) || (total > 3 && isAlive) || (total === 3 && !isAlive)) {
if (!isAlive) {
liveCellCount++;
} else {
liveCellCount--;
}
toSelect.push(part); // don't actually toggle the cell yet, this happens all at once after everything is done
}
}
}
// change the board state according the earlier loop
if (enabled || isManualStep) {
for (let i = 0; i < toSelect.length; i++) {
select(toSelect[i]);
}
}
if (enabled) {
if (liveCellCount === 0) {
toggleSimulation(); // stop the simulation if there are no more live cells
} else {
setTimeout(goLStep, interval); // queue another step if the simuation is still enabled
}
}
}
// clear the board of all live cells, stopping the simulation if it's currently enabled
function goLClear() {
if (enabled) {
toggleSimulation();
}
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
const part = goLGrid[i][j];
if (part.data.isAlive) {
select(part);
}
}
}
}
// this function contains all of the data for the four included sample patterns as well as the logic for drawing them
function loadSample(value) {
goLClear(); // clear the board first, stopping the simulation if it's enabled
// select the correct sample data based on the option value passed to the function
let sampleData = [];
switch (value) {
case 'symm4':
sampleData = [
[, , , , , , , , , , 1, 1, 1, , , , , , , , 0],
[, , , , , , , , , , 1, , , 1, , , , , , , 0],
[, , , , , , , , , , 1, , , 1, , , , , , , 0],
[, , , , , , , , , , , , 1, 1, , , , , , , 0],
[, , , , , , , , , , , , , , , , , , , , 0],
[, , , , , , , , , , , , , , , , , , , , 0],
[, , , , , , , , , , , , , , , , , , , , 0],
[, 1, 1, 1, , , , , , , , , , , , , , , , , 0],
[1, , , 1, , , , , , , , , , , , , , , , , 0],
[1, , , , , , , , , , , , , , , , , , , , 0],
[1, 1, 1, , , , , , , , , , , , , , , , 1, 1, 1],
[, , , , , , , , , , , , , , , , , , , , 1],
[, , , , , , , , , , , , , , , , , 1, , , 1],
[, , , , , , , , , , , , , , , , , 1, 1, 1, 0],
[, , , , , , , , , , , , , , , , , , , , 0],
[, , , , , , , , , , , , , , , , , , , , 0],
[, , , , , , , , , , , , , , , , , , , , 0],
[, , , , , , , 1, 1, , , , , , , , , , , , 0],
[, , , , , , , 1, , , 1, , , , , , , , , , 0],
[, , , , , , , 1, , , 1, , , , , , , , , , 0],
[, , , , , , , , 1, 1, 1, , , , , , , , , , 0]
];
break;
case 'pulsar':
sampleData = [
[, 1, 1, 1, , , , 1, 1, 1, 0],
[1, , , , 1, , 1, , , , 1],
[1, , , , 1, , 1, , , , 1],
[1, , , , 1, , 1, , , , 1],
[, 1, 1, 1, , , , 1, 1, 1, 0],
[, , , , , , , , , , 0],
[, 1, 1, 1, , , , 1, 1, 1, 0],
[1, , , , 1, , 1, , , , 1],
[1, , , , 1, , 1, , , , 1],
[1, , , , 1, , 1, , , , 1],
[, 1, 1, 1, , , , 1, 1, 1, 0]
];
break;
case 'spaceships':
sampleData = [
[1, , , 1, , , , , , , , , , , , , 0],
[, , , , 1, , , , , , , , , , , , 0],
[1, , , , 1, , , , , , , , , , , , 0],
[, 1, 1, 1, 1, , , , , , , , , , , , 0],
[, , , , , , , , , , , , , , , , 0],
[, , , , , , , , , , , , , , , , 0],
[, , , , , , , , , , , , , , , , 0],
[, , , , , , , , , , , , , , , , 0],
[, , , , , , , , , , , , , , , , 0],
[, , , , , , , , , , , , , , , , 0],
[, , , , , , , , , , , , , , 1, , 0],
[, , , , , , , , , , , , 1, , , , 1],
[, , , , , , , , , , , 1, , , , , 0],
[, , , , , , , , , , , 1, , , , , 1],
[, , , , , , , , , , , 1, 1, 1, 1, 1, 0],
[, , , , , , , , , , , , , , , , 0],
[, , , , , , , , , , , , , , , , 0],
[, , , , , , , , , , , , , , , , 0],
[, , , , , , , , , , , , , , , , 0],
[, , , , , , , , , , , , , , , , 0],
[, , , , , , , , , , , , , , , , 0],
[, , , , , , , , , , , , , , , , 0],
[, , , , , 1, , 1, 1, , , , , , , , 0],
[, , , , 1, , , , , , , 1, , , , , 0],
[, , , 1, 1, , , , 1, , , 1, , , , , 0],
[1, 1, , 1, , , , , , 1, 1, , , , , , 0],
[1, 1, , 1, , , , , , 1, 1, , , , , , 0],
[, , , 1, 1, , , , 1, , , 1, , , , , 0],
[, , , , 1, , , , , , , 1, , , , , 0],
[, , , , , 1, , 1, 1, , , , , , , , 0]
];
break;
case 'bigGliders':
sampleData = [
[, , , , , , , , , , , , , 1, 1, 1, , , , , , , , , , , , 0],
[, , , , , , , , , , , , , 1, , , 1, 1, 1, , , , , , , , , 0],
[, , , , , , , , , , , , , , 1, , 1, , , , , , , , , , , 0],
[, , , , , , , , , , 1, 1, , , , , , , , 1, , , , , , , , 0],
[, , , , , , , , , , 1, , 1, , , , , 1, , , 1, , , , , , , 0],
[, , , , , , , , , , 1, , , , , , , , , 1, 1, , , , , , , 0],
[, , , , , , , , , , , 1, 1, , , , , , , , , , , , , , , 0],
[, , , , , , , , , , , 1, , , 1, , , , , , 1, , 1, 1, , , , 0],
[, , , , , , , , , , , 1, , , , , , , , , , 1, 1, , 1, , , 0],
[, , , , , , , , , , , , , 1, , 1, , , , , , , 1, 1, , , 1, 0],
[, , , , , , , , , , , , , , 1, 1, , 1, , , , , 1, 1, , , , 1],
[, , , , , , , , , , , , , , , , , , 1, , , , , , , , 1, 0],
[, , , , , , , , , , , , , , , , , 1, 1, 1, 1, , , , 1, , 1, 0],
[, , , , , , , , , , , , , , , , , 1, , 1, 1, , , , 1, 1, 1, 1],
[, , , , , , , , , , , , , , , , , , 1, , , , 1, 1, , 1, , 0],
[, , , , , , , , 1, 1, , , , , , , , , , , , , , 1, 1, , , 0],
[, , , , , , , 1, 1, , , , , , , , , , , 1, , 1, 1, 1, , , , 0],
[, , , , , , , , , 1, , , , , , , , , , , 1, , , 1, , , , 0],
[, , , , , , , , , , , 1, 1, , , , , , , , , , , , , , , 0],
[, , , , , , , , , , 1, , , , , , , , , , , , , , , , , 0],
[, , , , , , , , , , , , , , , , , , , , , , , , , , , 0],
[, , , , , , , , , 1, , , 1, , , , , , , , , , , , , , , 0],
[, 1, 1, , , , , , 1, 1, , , , , , , , , , , , , , , , , , 0],
[1, 1, , , , , , 1, , , , , , , , , , , , , , , , , , , , 0],
[, , 1, , , , , 1, , 1, , , , , , , , , , , , , , , , , , 0],
[, , , , 1, 1, , , 1, , , , , , , , , , , , , , , , , , , 0],
[, , , , 1, 1, , , , , , , , , , , , , , , , , , , , , , 0]
];
break;
}
// draw the sample pattern in the middle of the board
const startRow = Math.floor(rows / 2 - sampleData.length / 2);
const startCol = Math.floor(cols / 2 - sampleData[0].length / 2);
for (let i = startRow; i < startRow + sampleData.length; i++) {
for (let j = startCol; j < startCol + sampleData[0].length; j++) {
if (sampleData[i - startRow][j - startCol] === 1) {
select(goLGrid[i][j]);
}
}
}
}
window.addEventListener('DOMContentLoaded', init);
</script>
<div id="sample">
<div id="myDiagramDiv" style="border: solid 1px black; width: 500px; height: 500px"></div>
<p>
Game Controls:
<input id="start" onclick="toggleSimulation()" type="button" value="Start" />
<input id="step" onclick="stepOnclick()" type="button" value="Step" />
<input id="clear" onclick="goLClear()" type="button" value="Clear" style="margin-bottom: 10px" />
Sample patterns:
<select id="samplePatterns" onchange="loadSample(this.options[this.selectedIndex].value)" style="margin-bottom: 10px">
<option value="symm4">Symmetry</option>
<option value="pulsar">Pulsar</option>
<option value="spaceships">Spaceships</option>
<option value="bigGliders">Big gliders</option>
</select>
</p>
<p>
This sample shows an implementation of <a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life">Conway's Game of Life</a> in GoJS. Conway's Game of
Life is a simple cellular automaton devised by British mathematician John Horton Conway in 1970. To start or advance the simulation, use the controls above.
</p>
<p>
Whether or not a given cell lives, dies, or is born in a step is determined by the number of live cells in the 8 squares adjacent to it. For a cell
<i>x</i> with <i>n</i> adjacent live cells:
</p>
<ul>
<li>If <i>n</i> <= 1, cell <i>x</i> dies or stays dead (from underpopulation).</li>
<li>If <i>n</i> > 3, cell <i>x</i> dies or stays dead (from overpopulation).</li>
<li>If <i>n</i> = 3, then <i>x</i> is born or stays alive.</li>
<li>If <i>n</i> = 2, then <i>x</i> maintains its status.</li>
</ul>
<p>
Though the rules are simple, they can produce complex patterns, some of which are shown in the dropdown above. To create your own patterns, click or drag
anywhere on the grid when the simulation is not running.
</p>
<p>Each cell is implemented by a simple <a>Part</a> holding a small square <a>Shape</a>.</p>
</div>
</div>
<!-- * * * * * * * * * * * * * -->
<!-- End of GoJS sample code -->
</div>
<div id="allTagDescriptions" class="p-4 w-full max-w-screen-xl mx-auto">
<hr/>
<h3 class="text-xl">GoJS Features in this sample</h3>
</div>
</div>
</body>
<!-- This script is part of the gojs.net website, and is not needed to run the sample -->
<script src="../assets/js/goSamples.js"></script>
</html>