Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                body
	margin: 0
	background: #000
	overflow: hidden
	min-height: 100%
canvas
	position: absolute
	top: 50%
	left: 50%
	transform: translateX(-50%) translateY(-50%)

              
            
!

JS

              
                const hexRadius = 10;
const hexLineWeight = 2;
const hexMargin = 1;
const drawHex = true;
const drawPoints = true;
const drawStraightLines = false;
let hexHeight, hexWidth, columns, rows;
let hexagons = [];

function setup() {
	hexWidth = hexRadius * 2;
	hexHeight = Math.sqrt(3)*hexRadius;
	columns = Math.ceil(window.innerWidth / (hexRadius * 3));
	rows = Math.ceil(window.innerHeight / (hexHeight / 2)) + 1;
	
  createCanvas((columns + 1/4) * (hexRadius * 3), (rows + 1) * (hexHeight / 2));
  frameRate(0);
	fill(255, 100);
  stroke(255);
  strokeWeight(5);
	noStroke();
	
	for (let x = 0; x < columns; x++) {
		hexagons.push([]);
		for (let y = 0; y < rows; y++) {
			const active = Math.round(random()*0.6);
		  hexagons[x].push(new Hex(x, y, active));
		}
	}
	// neighbouring needs to be done after they're all initialised
	for (let x = 0; x < columns; x++) {
		for (let y = 0; y < rows; y++) {
		  hexagons[x][y].initialiseNeighbours(x, y);
		}
	}
}

function draw() {
  background(60, 50);
	for (let y = 0; y < rows; y++) {
		for (let x = 0; x < columns; x++) {
			let hex = hexagons[x][y];
			hex.draw();
			hex.checkActive();
		}
	}
	update();
}

function update() {
	for (let y = 0; y < rows; y++) {
		for (let x = 0; x < columns; x++) {
			let hex = hexagons[x][y];
			hex.updateActive();
		}
	}
}

class Hex {
	constructor(x, y, active) {
		
		// establish grid position
		this.pos = createVector(x, y);
		
		// establish pixel position
		this.pixelPos = createVector(0, 0);
		this.pixelPos.x = hexWidth * (1.5 * x + 0.5 + y % 2 * 0.75);
		this.pixelPos.y = hexHeight * (y * 0.5 + 0.5);
		
		// establish state
		this.active = active;
		this.nextActive = false;
		
		// establish neighbours
		this.neighbours = [];
	}
	
	initialiseNeighbours(x, y) {
		let n = [false, false, false, false, false, false];
		const odd = y%2;
		
		// above
		if (y >= 2) {
			n[0] = hexagons[x][y-2];
		}
		
		// top right
		if (y >= 1) {
			if (!odd || x < columns-1) {
				n[1] = hexagons[x+odd][y-1];
			}
		}
		
		// bottom right
		if (y < rows-1) {
			if (!odd || x < columns-1) {
				n[2] = hexagons[x+odd][y+1];
			}
		}
		
		// bottom
		if (y < rows-2) {
			n[3] = hexagons[x][y+2];
		}
		
		// bottom left
		if (y < rows-1) {
			if (odd || x >= 1) {
				n[4] = hexagons[x-1+odd][y+1];
			}
		}
		
		// top left
		if (y >= 1) {
			if (odd || x >= 1) {
				n[5] = hexagons[x-1+odd][y-1];
			}
		}

		this.neighbours = n;
	}
	
	updateActive() {
		this.active = this.nextActive;
	}
	
	countActiveNeighbours() {
		let activeNeighbours = 0;
		for (let i = 0; i < 6; i++) {
			if (this.neighbours[i] && this.neighbours[i].active) {
				activeNeighbours++;
			}
		}
		return activeNeighbours;
	}
	
	getActiveNeighbours() {
		let activeNeighbours = [];
		for (let i = 0; i < 6; i++) {
			if (this.neighbours[i] && this.neighbours[i].active) {
				activeNeighbours.push(true);
			} else {
				activeNeighbours.push(false);
			}
		}
		return activeNeighbours;
	}
	
	checkActive() {
		let activeNeighbours = this.countActiveNeighbours();
		if (this.active) {
			// two or less it dies
			if (activeNeighbours < 3) this.nextActive = false;
			// five or more it dies
			if (activeNeighbours > 4) this.nextActive = false; 
		} else {
			// 2 it comes alive
			if (activeNeighbours == 2) this.nextActive = true;
		// 	// 1 it comes alive 2% of the time
		// 	if (activeNeighbours == 1) {
		// 		if (Math.random() < 0.02) this.nextActive = true;
		// 	}
			// 0 it comes alive 1% of the time
			if (activeNeighbours == 0) {
				if (Math.random() < 0.01) this.nextActive = true;
			}
		}
	}
	
	checkNeighbours() {
		return true;
	}
	
	draw() {
		
		push();
		translate(this.pixelPos.x, this.pixelPos.y);
		
		// draw hexagon
		noStroke();
		if (this.active) {
			let activeNeighbours = this.countActiveNeighbours();
			fill(80-10*activeNeighbours, 20+40*activeNeighbours, 130+180*activeNeighbours, 100);
		} else {
			fill(0, 40);
		}
		if (drawHex) {
			beginShape();
			for (let i = 0; i < 6; i++) {
				vertex((hexRadius-hexMargin/2)*cos(i*Math.PI/3), (hexRadius-hexMargin/2)*sin(i*Math.PI/3));
			}
			endShape(CLOSE);
		}
		
		// draw shapes
		if (this.active) {
			let activeNeighboursCount = this.countActiveNeighbours();
			let activeNeighbours = this.getActiveNeighbours();
			stroke(255);
			strokeWeight(hexLineWeight);
			noFill();
			if (activeNeighboursCount == 0) {
				// for one
				if (drawPoints) {
					point(0, 0);
					// ellipse(0, 0, hexRadius/2);
				}
			} else if (activeNeighboursCount == 1) {
				// for two
				if (drawStraightLines) {
					var pos = getEdgePos(activeNeighbours.indexOf(true));
					line(0, 0, pos.x, pos.y);
				}
			} else {
				// for three
				for (var i = 0; i < 6; i++) {
					if (activeNeighbours[i]) {
						for (var j = i+1; j < 6; j++) {
							if (activeNeighbours[j]) {
								var pos1 = getEdgePos(i);
								var pos2 = getEdgePos(j);
								beginShape();
								vertex(pos1.x, pos1.y);
								quadraticVertex(0, 0, pos2.x, pos2.y);
								endShape();
							}
						}
					}
				}
			}
		}
		pop();
	}
}

function getEdgePos(i) {
	var pos = createVector(0, -hexHeight/2);
	pos.rotate(i*Math.PI/3);
	return pos;
}

              
            
!
999px

Console