Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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

              
                <div id="two"></div>
              
            
!

CSS

              
                html, body {
  height: 100%;
  background: #151644;
  overflow: hidden;
}

canvas {
  position: absolute;
  margin: auto;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: #151644;
  transform: rotate(45deg);
  border-radius: 50%;
}
              
            
!

JS

              
                // Feel free to change these five variables, everything should still work as long as you don't change them _too_ much!

// integer; size of each cell
var unitsize = 2;
// integer; number of cells per side
var s = 80;
// integer; number of cells to try and find a home for each frame;
var growthRate = 15;
// k1: [1 : 100] initial percentage concentration of cells on the grid
var k1 = 60;
// k2: number of seed cells
var k2 = 100;

// Basic explanation of how this works: each tick, a random cell will be picked. If that cell is of the 'mobile' / k1 type, it will move to a nearby (8-directional) spot. If that new spot is adjacent to a 'fixed' / k2 type cell, the cell that just moved will become fixed + get a color based on the fixed cell it latched onto.

var rs = [];

// max value
var q = 14;

var k1abs = (k1 / 100) * (s * s);


var numCols = s;
var numRows = s;

// Create an instance of Two.js
// two.js.org
var elem = document.getElementById('two');
var two = new Two({type: Two.Types.canvas, width: (numCols * unitsize) + unitsize * .5, height: (numRows * unitsize) + unitsize * .5 }).appendTo(elem);

var arrLen = numRows * numCols;
var used = 0;

var radius = s / 2 * unitsize;
var xc = radius;
var yc = radius;

function isOutside(x1, y1) {
  return Math.pow(x1 - xc, 2) + Math.pow(y1 - yc, 2) > Math.pow(radius, 2);
}

var colors = [
  "#1E1F51",
  "#EBFFEB",
  "#B0FFD4",
  "#8AF8A8",
  "#2DF8A8",
  "#2CEBB2",
  "#2BD8C0",
  "#2AC3D0",
  "#29ABE2",
  "#2C97CA",
  "#2F83B4",
  "#32719F",
  "#39486E"
];

// neighbor-finding functions
function ne(i, top, right) {
  if (top && right) {
    return rs[arrLen - numCols];
  } else if (top) {
    return rs[i + 1 + ((numRows - 1) * numCols)];
  } else if (right) {
    return rs[i - (numCols * 2) + 1];
  } else {
    return rs[i - numCols + 1];
  }
}

function nw(i, top, left) {
  if (top && left) {
    return rs[arrLen - 1];
  } else if (top) {
    return rs[((numRows - 1) * numCols) + i - 1];
  } else if (left) {
    return rs[i - 1];
  } else {
    return rs[i - numCols - 1];
  }
}


function se(i, bottom, right) {
  if (bottom && right) {
    return rs[0];
  } else if (bottom) {
    return rs[i - (numCols * (numRows - 1)) + 1];
  } else if (right) {
    return rs[i + 1];
  } else {
    return rs[i + numCols + 1];
  }
}

function sw(i, bottom, left) {
  if (bottom && left) {
    return rs[numCols -1];
  } else if (bottom) {
    return rs[i - ((numRows - 1) * numCols) - 1];
  } else if (left) {
    return rs[i+ (2 * numCols) - 1];
  } else {
    return rs[i + numCols - 1];
  }
}

// Constructor for each square in the grid
function Pt(x, y, unitsize, i) {
  // Initial setup for each square
  this.setup = () => {
    this.r = two.makeRectangle(x, y, unitsize, unitsize);
    this.x = x;
    this.y = y;
    this.isTop = (this.y - (.5 * unitsize)  == 0);
    this.isBot = (this.y == (unitsize * numRows) - (.5 * unitsize));
    this.isLef = (this.x == 0 + (.5 * unitsize));
    this.isRig = (this.x == (unitsize * numCols) - (.5 * unitsize));
    // Random initial state for each square
    this.state = 0;
    this.type = false;
    this.r.noStroke();
    this.r.fill = "#000";
  }
    
  this.setup();

  // Store the 8 neighbors of each cell
  this.findNeighbors = () => {
    this.n = this.isTop ? rs[i + arrLen - numCols] : rs[i - numCols];
    this.s = this.isBot ? rs[i - arrLen + numCols] : rs[i + numCols];
    this.e = this.isRig ? rs[i - numCols + 1] : rs[i + 1];
    this.w = this.isLef ? rs[i + numCols - 1] : rs[i - 1];
    this.ne = ne(i, this.isTop, this.isRig);
    this.nw = nw(i, this.isTop, this.isLef);
    this.se = se(i, this.isBot, this.isRig);
    this.sw = sw(i, this.isBot, this.isLef);
    this.neighbors = [this.n, this.s, this.e, this.w, this.ne, this.nw, this.se, this.sw];
  }

  // Find the average state for the neighborhood (this square + the 8 around it)
  this.avg = () => {
    return (this.state + this.n.state + this.ne.state + this.e.state + this.se.state + this.s.state + this.sw.state + this.w.state + this.nw.state) / 9;
  }
}

for (var y = 0; y < numCols; y++) {
  for (var x = 0; x < numRows; x++) {
    rs.push(new Pt(unitsize * x + (.5 * unitsize), unitsize * y + (.5 * unitsize), unitsize, rs.length));
  }
}

for (var i = 0, l = rs.length; i < l; i++) {
  rs[i].findNeighbors();
  rs[i].r.fill = colors[0];
}

var kCells = [];

while (kCells.length < k1abs + k2) {
  var ind = Math.floor(Math.random() * rs.length);
  if (kCells.indexOf(ind) === -1) {
    kCells.push(ind);
  }
}

for (var i = 0; i < k1abs + k2; i++) {
  if (i < k1abs) {
    rs[kCells[i]].type = 'mobile';
    rs[kCells[i]].state = 1;
    rs[kCells[i]].r.fill = colors[0];
  } else { // k2, seed cell/s
    rs[kCells[i]].type = 'fixed';
    rs[kCells[i]].state = q;
    rs[kCells[i]].r.fill = colors[q - 2];
  }
}


function fixedNeighbors(neighb) {
  return neighb.type === 'fixed';
}
function emptyNeighbors(neighb) {
  return !(neighb.type);
}

function reset() {
  for (var i = 0, l = rs.length; i < l; i++) {
    rs[i].state = 0;
    rs[i].type = false;
    rs[i].r.fill = colors[0];
  }
  var kCells = [];

  while (kCells.length < k1abs + k2) {
    var ind = Math.floor(Math.random() * rs.length);
    if (kCells.indexOf(ind) === -1) {
      kCells.push(ind);
    }
  }

  for (var i = 0; i < k1abs + k2; i++) {
    if (i < k1abs) {
      rs[kCells[i]].type = 'mobile';
      rs[kCells[i]].state = 1;
      rs[kCells[i]].r.fill = colors[0];
    } else { // k2, seed cell/s
      rs[kCells[i]].type = 'fixed';
      rs[kCells[i]].state = q;
      rs[kCells[i]].r.fill = colors[q - 2];
    }
  }

  used = 0;
}

var resetCounter = 0;


// The animation loop
two.bind('update', function(frameCount, timeDelta) {
for (var i = 0; i < growthRate; i++) {
  var loc = rs[Math.floor(Math.random() * arrLen)];
  if (loc.type === 'mobile') {
    let fixedNeighb = loc.neighbors.find(fixedNeighbors);
    if (fixedNeighb) {
      used += 1;
      loc.type = "fixed";
      loc.state = Math.min(fixedNeighb.state - 1);
      if (loc.state < 1) {
        loc.state = 0;
        loc.type = false;
        loc.r.fill = colors[0];
      } else {
        loc.r.fill = colors[loc.state - 1];
      }
    } else {
      let availNeighbs = loc.neighbors.filter(emptyNeighbors);
      if (availNeighbs.length) {
        let target = availNeighbs[Math.floor(Math.random() * availNeighbs.length)];
        //console.log('moving, target is', target);
        if (target) {
          target.state = loc.state;
          target.type = loc.type;
          target.r.fill = loc.r.fill;
        }
        loc.state = 0;
        loc.type = false;
        loc.r.fill = colors[0];
      }
    }
  }
}
if (used == k1abs) {
  reset();
  resetCounter = 0;
}
}).play();
              
            
!
999px

Console