Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              // Two chemicals
// A (red) is continually added
// B (green) is continually removed
// there is a reaction A+B+B -> B+B+B
// A and B diffuse at different rates
// according to the rate at which A is added and B removed,
// different patterns emerge

// see http://mrob.com/pub/comp/xmorphia
// see http://www.karlsims.com/rd.html

// for a more efficent option, use WebGL shaders:
// see http://mrob.com/pub/comp/xmorphia/ogl/index.html

AB = new field2D(128);
AB.set(function() {
  return [
    random() < 0.9 ? 1 : 0,
    random() < 0.1 ? 1 : 0,
AB1 = AB.clone();

AB.smooth = true;
AB1.smooth = true;

function update() {
  // the diffusion rates (mainly affects scale)
  const dA = 5/AB.width; // diffusion rate of A
  const dB = dA*0.5; // diffusion rate of B

  // reaction term:
  AB1.set(function(x, y) {
    // vary feed & kill rates over the XY space:
    // feed rate, rate at which A is added
    // feed rates try .045 to .07
    const feed = 0.01 + 0.09*y/AB.height;
    // kill rate, rate at which B is removed
    // kill rates try .01 to .1
    const kill = 0.047 + 0.02*x/AB.width; 
    // center cell:
    const C = AB.cell(x, y);
    const a = C[0];
    const b = C[1];
    // the rate of change in A and B concentration 
    // due to reaction A B B -> B B B
    // is a product of the current concentrations:
    const reaction = a * b * b;
    // a basic approximation of the laplacian for diffusion
    const N = AB.cell(x, y+1);
    const S = AB.cell(x, y-1);
    const E = AB.cell(x+1, y);
    const W = AB.cell(x-1, y);
    const NW = AB.cell(x+1, y+1);
    const SW = AB.cell(x+1, y-1);
    const NE = AB.cell(x-1, y+1);
    const SE = AB.cell(x-1, y-1);
    const diag = 0.25;
    const alaplacian = N[0] + S[0] + E[0] + W[0] 
                     + 0.25*(NW[0] + NE[0] + SW[0] + SE[0]) 
                     - 5*a;
    const blaplacian = N[1] + S[1] + E[1] + W[1] 
                     + 0.25*(NW[1] + NE[1] + SW[1] + SE[1]) 
                     - 5*b;
    // compute changes in A and B concentration:
    const da = dA*alaplacian - reaction + feed*(1 - a);
    const db = dB*blaplacian + reaction - (kill + feed)*b;
    // apply these changes:
    return [a + da, b + db, 0];

  // swap:
  const tmp = AB1;
  AB1 = AB;
  AB = tmp;

function draw() {
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.