JSFiddle - React, Tailwind, and code Playground
by elssar
HTML
<canvas id = "golCanvas" width = "800px" height = "400px">
</canvas>
<form>
<input type = "button" name = "start" value = "Start" onclick = "runGame()" />
<input type = "button" name = "pause" value = "Pause" onclick = "pauseGame()" />
<input type = "button" name = "clear" value = "Clear" onclick = "clearGrid()" />
</form>
JavaScript
/*
gameofLife.js
Javascript code to emulate Conways Game of Life, using html5 Canvas.
Copyright (C) 2011, Akshay Bist.
Date : Wednesday, July 6, 2011.
Licence : GPL 3.0
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
For any suggestions, comments or queries contact - [email protected]
*/
//Global variables
var cell = new Array(); //cell
var cellsAlive = 0; //number of cells alive
var generation = 0; //number of current generation
var run = false; //boolean variable to control the main loop
var gameCanvas;
var gameContext;
var speed = 100;
//var statistics = document.getElementById("stats");
//Cell initialization function
var cellPrototype = function() {
this.isAlive = false;
this.neighboursAlive = 0;
this.toggle = function() {
this.isAlive = !(this.isAlive);
if(this.isAlive)
cellsAlive++;
else
cellsAlive--;
};
};
//Creating the cells
for(var i=0;i<80;i++) {
cell[i] = new Array();
for(var j=0;j<40;j++) {
cell[i][j] = new cellPrototype();
}
}
//Function to calculate the number of neighbours alive for a cell
var checkNeighbours = function() {
var a;
var b;
var c, d;
for(var i=0;i<80;i++)
for(var j=0;j<40;j++) {
cell[i][j].neighboursAlive = 0;
a = (i>0)?(i-1) : i;
...