JSFiddle - React, Tailwind, and code Playground
HTML
<input id="start" type="button" value="start">
<input id="stop" type="button" value="stop">
<input id="random" type="button" value="randomize">
<input id="clear" type="button" value="clear">
CSS
.shapeHexagon {
background-color: black;
height: 8px;
width: 16px;
position: absolute;
}
.shapeUp {
background-color: black;
height: 8px;
width: 16px;
position: absolute;
}
.shapeUp:after, .shapeHexagon:before {
content:"";
position: absolute;
top: -8px;
left: 0px;
width: 0;
height: 0;
border-style: solid;
border-color: transparent transparent black;
border-width: 0px 8px 8px 8px;
}
.shapeAlive.shapeUp {
background-color: green;
}
.shapeAlive.shapeUp:after {
border-color: transparent transparent green;
}
.shapeDown {
background-color: black;
height: 8px;
width: 16px;
position: absolute;
}
.shapeDown:after, .shapeHexagon:after {
content:"";
position: absolute;
top: 8px;
left: 0px;
width: 0;
height: 0;
border-style: solid;
border-color: black transparent transparent transparent;
border-width: 8px 8px 0 8px;
}
.shapeAlive.shapeUp:after, .shapeAlive.shapeHexagon:before {
border-color: transparent transparent green;
}
.shapeAlive.shapeDown, .shapeAlive.shapeHexagon {
background-color: green;
}
.shapeAlive.shapeDown:after, .shapeAlive.shapeHexagon:after {
border-color: green transparent transparent transparent;
}
JavaScript
//-- Prepare --
var topX = 0;
var topY = 42;
var sizeX = 40;
var sizeY = 10;
var patternSizeX = 17;
var patternSizeY = 43;
var patternElements = 3;
var neighbourTopLeft = -(sizeX + 1) * patternElements;
var neighbourTop = -(sizeX) * patternElements;
var neighbourTopRight = -(sizeX - 1) * patternElements;
var neighbourLeft = -patternElements;
var neighbourRight = +patternElements;
var neighbourBottomLeft = +(sizeX - 1) * patternElements;
var neighbourBottom = +(sizeX) * patternElements;
var neighbourBottomRight = +(sizeX + 1) * patternElements;
var patternNeighbours = [
[neighbourTopLeft + 2, neighbourTop + 2, neighbourTopRight + 2, neighbourLeft, neighbourLeft + 1, 1, neighbourRight],
[neighbourLeft + 1, 0, 2, neighbourRight, neighbourRight + 1, neighbourRight + 2],
[neighbourLeft + 1, neighbourLeft + 2, 1, neighbourRight + 2, neighbourBottomLeft, neighbourBottom, neighbourBottomRight]
];
for (i = 0; i < sizeX; i++) {
for (j = 0; j < sizeY; j++) {
var tileId = (j * sizeX + i) * patternElements;
$("body").append('<div id="t' + (tileId) + '" class="shapeDown" style="left:' + topX + patternSizeX * i + 'px;top:' + (topY + patternSizeY * j) + 'px;">');
$("body").append('<div id="t' + (tileId + 1) + '" class="shapeHexagon" style="left:' + (8 + topX + patternSizeX * i) + 'px;top:' + (17 + topY + patternSizeY * j) + 'px;">');
$("body").append('<div id="t' + (tileId + 2) + '" class="shapeUp" style="left:' + topX + patternSizeX * i + 'px;top:' + (34 + topY + patternSizeY * j) + 'px;">');
}
}
//-- Populate --
function clearField() {
$(".shapeAlive").removeClass("shapeAlive");
};
// Random
function randomizeField() {
for (i = 0; i < (patternElements * sizeX * sizeY) / 5; i++) {
$("#t" + Math.floor((Math.random() * (patternElements * sizeX * sizeY)))).addClass("shapeAlive");
};
};
// Oscillator Windrose
//$("#t304,#t303,#t305,#t307").addClass("shapeAlive");
// Spaceship...