cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              var dimx = 256;
var dimy = dimx;

var field = new field2D(dimx, dimy);
var past = new field2D(dimx, dimy);

// @see http://www.sciencedirect.com/science/article/pii/016727898990081X#
// This typically goes through several stages:
// first a mass pulsing, more or less synchronized with a textured brain-like pattern
//) { circular pulsing pockets begin to appear and grow, with waves pushing to larger regions at different phases
//) { spiral patterns begin to appear and overcome the still growing pulsations
// the spirals break down under their own advection into smaller spirals while the bigger waves consume the remaining space
// the crucial parameter to vary these behaviors is sickness_rate
var sickness_rate = 0.11;
var infection_rate_infected = 1 / 3;
var infection_rate_ill = 1;
var initial_infection = 1 / 255;

function initialize() {
  return 1 - random() * random()
}

field.set(initialize);

// how to r}er the scene
function draw(ctx) {
  // draw the field (by default it fills the canvas):
  field.draw();
}

// a cell is infected if (the value is greater than zero:
var isinfected = Math.ceil;
// a cell is ill if (the value is equal or greater than 1:
var isill = Math.floor;

var floor = Math.floor;
var min = Math.min;

// the rule for an individual cell (at position x, y) in the field:
function transition(x, y) {

  // check my own previous state:
  var C = past.get(x, y);

  // check out the neighbors' previous states:
  var N = past.get(x, y + 1);
  var NE = past.get(x + 1, y + 1);
  var E = past.get(x + 1, y);
  var SE = past.get(x + 1, y - 1);
  var S = past.get(x, y - 1);
  var SW = past.get(x - 1, y - 1);
  var W = past.get(x - 1, y);
  var NW = past.get(x - 1, y + 1);

  if (C >= 1) {
    // all ill cells are healed automatically:
    C = 0;
  } else if (C > 0) {
    // infected
    var nearbyinfection = C + N + NE + E + SE + S + SW + W + NW;
    var nearbyinfected = 1 + isinfected(N) + isinfected(NE) + isinfected(E) + isinfected(SE) + isinfected(S) + isinfected(SW) + isinfected(W) + isinfected(NW);
    var averageinfection = nearbyinfection / nearbyinfected;
    C = sickness_rate + averageinfection;
    C = min(1, C);
  } else {
    // healthy cell:

    // number of var infected:
    var nearbyinfected = (
      isinfected(N) + isinfected(NE) + isinfected(E) + isinfected(SE) + isinfected(S) + isinfected(SW) + isinfected(W) + isinfected(NW)
    );
    // calculate number of var ill:
    var nearbyill = (
      isill(N) + isill(NE) + isill(E) + isill(SE) + isill(S) + isill(SW) + isill(W) + isill(NW)
    );

    var influence = floor(nearbyinfected * infection_rate_infected) + floor(nearbyill * infection_rate_ill);

    C = initial_infection * influence;
  }
  // a sick cell gets sicker by a fixed amount, plus extra sickness due to infected neighbours. It cannot get sicker than the limit.
  // An uninfected cell may catch infection, depending on its neighbours.
  // At the next 'tick' any ill cells are healed!

  // return the new state:
  return C;
}

function update() {
  var tmp = past;
  past = field;
  field = tmp;

  field.set(transition);
}
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console