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%)
	cursor: pointer

              
            
!

JS

              
                const hexRadius = 20;
const hexLineWeight = 2;
const hexMargin = 2;
const drawHex = true;
const drawPoints = false;
const drawStraightLines = false;
const drawCurves = true;
const drawAnts = true;

const xenoSway = 0.2;

let hexHeight, hexWidth, columns, rows;
let hexagons = [];
let ants = [];
let antCount;

function setup() {
	// calculate width and height of hexagons
  hexWidth = hexRadius * 2;
  hexHeight = Math.sqrt(3)*hexRadius;
	
	// set rows and columns to overlap page edge
  columns = Math.ceil(window.innerWidth / (hexRadius * 3));
  rows = Math.ceil(window.innerHeight / (hexHeight / 2)) + 1;
	antCount = 2;//Math.ceil(columns * rows * 0.003);
  
	// set up canvas
  createCanvas((columns + 1/4) * (hexRadius * 3), (rows + 1) * (hexHeight / 2));
  frameRate(120);
  fill(255, 100);
  stroke(255);
  strokeWeight(5);
  noStroke();
  
  // initialise 2D array of hexagons
  for (let x = 0; x < columns; x++) {
    hexagons.push([]);
    for (let y = 0; y < rows; y++) {
      hexagons[x].push(new Hex(x, y));
    }
  }
  // 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);
    }
  }
  
  // initialise ants
  for (let i = 0; i < antCount; i++) {
		// randomly place near crntre of screen
		let x = Math.round(columns * (0.3 + random(0.4)));
		let y = Math.round(rows * (0.3 + random(0.4)));
		
		// alternate evilness
		let evil = i % 2;
    ants.push(new Ant(i, x, y, evil));
  }
}

function draw() {
  background(30);
	
	if (drawHex) {
		for (let y = 0; y < rows; y++) {
			for (let x = 0; x < columns; x++) {
				hexagons[x][y].drawHex();
			}
		}
	}
	
  if (drawAnts) {
    for (let i = 0; i < antCount; i++) {
      ants[i].draw();
    }
  }
	
  for (let y = 0; y < rows; y++) {
    for (let x = 0; x < columns; x++) {
      hexagons[x][y].drawLines();
      hexagons[x][y].checkActive();
    }
  }
  update();
}

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


class Ant {
  constructor(i, x, y, evil) {
    this.i = i;
		
		// set random position on the grid
    this.x = x;
    this.y = y;
		
		// set random direction 0-5
		this.dir = Math.floor(random(0, 6));
    this.pixelPos = createVector(0, 0);
		
		// set its morality
		this.evil = evil;
  }
  
  draw() {
    fill(255);
		if (this.evil) fill(255, 0, 0);
    this.pixelPos.x = hexWidth * (1.5 * this.x + 0.5 + this.y % 2 * 0.75);
    this.pixelPos.y = hexHeight * (this.y * 0.5 + 0.5);
    push();
    translate(this.pixelPos.x, this.pixelPos.y);
    noStroke();
		if (this.evil) {
			fill(100, 40, 0, 120);
		}
		else {
			fill(255, 40);
		}
		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);
    pop();
  }
  
  update() {
		// get current hexagon by x, y
		var curHex = hexagons[this.x][this.y];
		
		// change it
		if (this.evil) {
			curHex.nextActive = false;
		}
		else {
			curHex.nextActive = true;
		}
		
		// randomly chose direction -1 to 1
		this.dir += -1 + Math.floor(random(3));
		// make direction wrap around 0-5
		this.dir = (this.dir + 6) % 6;
		
		// get next hexagon from current's neighbours
		var nextHex = curHex.neighbours[this.dir];
		
		// if next hexagon doesn't exist turn around
		if (nextHex === false) {
			this.dir = wrap6(this.dir + 3);
			nextHex = curHex.neighbours[this.dir];
			// if that doesn't work it's a corner
			// return and try again next round
			if (nextHex === false) return;
		}
		
		// update x and y from next hexagon
		this.x = nextHex.pos.x;
		this.y = nextHex.pos.y;
	}
}


class Hex {
  constructor(x, y) {
    
    // 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 = false;
    this.nextActive = false;
    
    // establish neighbours
    this.neighbours = [];
    
    // chose random layout (1-3) for dense (4/5/6 neighbours) display
    // regenerated when hex goes from inactive to active
    this.denseLayout = Math.ceil(random(3));
		
		// lazily updating count of active neighbours
		// used to colour hexagons
		this.xenosNeighbours = 0;
  }
  
  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;
  }
  
  update() {
		// randomise layout if hex is becoming active
		if (!this.active && this.nextActive) {
			this.denseLayout = Math.ceil(random(3));
		}
    this.active = this.nextActive;
		
		// add to xenosNeighbours
		if (this.xenosNeighbours == 0) {
			// make accurate if 0
			this.xenosNeighbours = this.countActiveNeighbours();
		} else {
			this.xenosNeighbours = this.xenosNeighbours*(1-xenoSway) + xenoSway*this.countActiveNeighbours();
		}
  }
  
  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() {
		if (this.nextActive != this.active) {
			this.nextActive = this.active;
		}
  }
  
  checkNeighbours() {
    return true;
  }
  
  drawHex() {
    push();
    translate(this.pixelPos.x, this.pixelPos.y);
    noStroke();
		fill(5*this.xenosNeighbours,
				 10*Math.pow(this.xenosNeighbours, 1.7),
				 30*this.xenosNeighbours);
		if (!this.active) fill(0);
		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);
    pop();
	}

  drawLines() {
    push();
    translate(this.pixelPos.x, this.pixelPos.y);
    // draw shapes
    if (this.active) {
      let activeNeighboursCount = this.countActiveNeighbours();
      let activeNeighbours = this.getActiveNeighbours();
      stroke(255);
      strokeWeight(hexLineWeight);
      noFill();
      
      // no neighbours
      if (activeNeighboursCount == 0) {
        if (drawPoints) {
          point(0, 0);
          // ellipse(0, 0, hexRadius/2);
        }
      }
      
      // one neighbour
      else if (activeNeighboursCount == 1) {
        if (drawStraightLines) {
          var pos = getEdgePos(activeNeighbours.indexOf(true));
          line(0, 0, pos.x, pos.y);
        }
      }
			
      // two or three neighbours
      else if (activeNeighboursCount == 2 || activeNeighboursCount == 3) {
        if (drawCurves) {
          for (var i = 0; i < 6; i++) {
            if (activeNeighbours[i]) {
              for (var j = i+1; j < 6; j++) {
                if (activeNeighbours[j]) {
                  drawCurveThroughOrigin(i, j);
                }
              }
            }
          }
        }
      }
      
      // four neighbours
      else if (activeNeighboursCount == 4) {
        if (drawCurves) {
					// get the index of each inactive edge
          let skipped1 = activeNeighbours.indexOf(false);
					let skipped2 = activeNeighbours.slice(skipped1+1).indexOf(false) + skipped1 + 1;
					
					// make list of active edge positions
					var positions = [];
					for (let i = 0; i < 6; i++) {
						if (i != skipped1 && i != skipped2) {
							positions.push(i);
						}
					}
					
					// skips are adjacent
					if (skipped2 - skipped1 == 1) {
						if (this.denseLayout == 3) {
							// connect edges to adjacent edges
              drawCurveThroughOrigin(positions[0], positions[1]);
              drawCurveThroughOrigin(positions[1], positions[2]);
              drawCurveThroughOrigin(positions[2], positions[3]);
            }
            else if (this.denseLayout == 2) {
              // cross over curves
              drawCurveThroughOrigin(positions[0], positions[2]);
              drawCurveThroughOrigin(positions[1], positions[3]);
            }
            else {
              // pair edges with adjacent edges
              drawCurveThroughOrigin(positions[0], positions[1]);
              drawCurveThroughOrigin(positions[2], positions[3]);
						}
					}
					
					// 1 and 3 situation
					// or 2 and 2
					else {
						if (this.denseLayout == 3) {
							// connect edges to adjacent edges
              drawCurveThroughOrigin(positions[0], positions[1]);
              drawCurveThroughOrigin(positions[1], positions[2]);
              drawCurveThroughOrigin(positions[2], positions[3]);
              drawCurveThroughOrigin(positions[3], positions[0]);
						}
            else if (this.denseLayout == 2) {
              // pair edges with adjacent edges
              drawCurveThroughOrigin(positions[3], positions[0]);
              drawCurveThroughOrigin(positions[1], positions[2]);
            }
            else {
              // pair edges with adjacent edges alt
              drawCurveThroughOrigin(positions[0], positions[1]);
              drawCurveThroughOrigin(positions[2], positions[3]);
						}
					}
				}
			}
      
      // five neighbours
      else if (activeNeighboursCount == 5) {
        if (drawCurves) {
          let skipped = activeNeighbours.indexOf(false);
          if (this.denseLayout == 3) {
            // connect edges to adjacent edges
            for (var i = skipped; i < 5 + skipped; i++) {
              var edge1 = (i == skipped) ? wrap6(i+5) : i;
              drawCurveThroughOrigin(edge1, wrap6(i+1));
            }
          }
          else if (this.denseLayout == 2) {
						// batman logo
						// curve between two adjacent to skipped one
						drawCurveThroughOrigin(wrap6(skipped+1), wrap6(skipped+5));
						// connect other 3 to eachother
						drawCurveThroughOrigin(wrap6(skipped+2), wrap6(skipped+3));
						drawCurveThroughOrigin(wrap6(skipped+3), wrap6(skipped+4));
          }
          else if (this.denseLayout == 1) {
						// evil M
						// curve the two skipped-adjacent edges to the opposite edge
						drawCurveThroughOrigin(wrap6(skipped+1), wrap6(skipped+3));
						drawCurveThroughOrigin(wrap6(skipped+5), wrap6(skipped+3));
						// curve the other two edges to the skipped-adjacent edges
						drawCurveThroughOrigin(wrap6(skipped+1), wrap6(skipped+2));
						drawCurveThroughOrigin(wrap6(skipped+5), wrap6(skipped+4));
          }
        }
      }
      
      // full house
      else {
        if (drawCurves) {
          if (this.denseLayout == 3) {
            // connect edges to adjacent edges
            for (var i = 0; i < 6; i++) {
              drawCurveThroughOrigin(i, wrap6(i+1));
            }
          }
          else {
            // pair edges with adjacent edges
            for (var i = this.denseLayout - 1; i < 6; i+=2) {
              drawCurveThroughOrigin(i, wrap6(i+1));
            }
          }
        }
      }
      
    }
    pop();
  }
}

function drawCurveThroughOrigin(edge1, edge2) {
	let pos1 = getEdgePos(edge1);
	let pos2 = getEdgePos(edge2);
  beginShape();
  vertex(pos1.x, pos1.y);
  quadraticVertex(0, 0, pos2.x, pos2.y);
  endShape();
}

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

function wrap6(num) {
	// -1 => 5
	// 0 => 0
	// 5 => 5
	// 6 => 0
	// 7 => 1
	return (num+6) % 6;
}

              
            
!
999px

Console