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

              
                <script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.8/p5.js"></script>     

              
            
!

CSS

              
                body {margin:0px; padding:0px; overflow: hidden}
              
            
!

JS

              
                const N_GRIDS = 64;
let dx = 0.01;
let dt = 1;
let Du = 2e-5;
let Dv = 1e-5;
let f = 0.04, k = 0.06;

let u = [];
let v = [];

function setup() {
  canvas = createCanvas(windowWidth, windowHeight);
  for (let i = 0; i < N_GRIDS; i++) {
    u[i] = new Array(N_GRIDS).fill(1);
    v[i] = new Array(N_GRIDS).fill(0);
  }

  let SQUARE_SIZE = 20;
  for(let i = N_GRIDS / 2 - SQUARE_SIZE / 2; i < N_GRIDS / 2 + SQUARE_SIZE / 2; i++) {
    for(let j = N_GRIDS / 2 - SQUARE_SIZE / 2; j < N_GRIDS / 2 + SQUARE_SIZE / 2; j++) {
      u[i][j] = 0.5;
      v[i][j] = 0.25;
    }
  }

  for (let i = 0; i < N_GRIDS; i++) {
    for (let j = 0; j < N_GRIDS; j++) {
      u[i][j] += Math.random() * 0.1;
      v[i][j] += Math.random() * 0.1;
    }
  }
}

function wrap(n, range) {
  return (n + range) % range;
}

function draw() {
  const laplacian_u = []; 
  const laplacian_v = []; 
  for (let i = 0; i < N_GRIDS; i++) {
    laplacian_u[i] = [];
    laplacian_v[i] = [];
    for (let j = 0; j < N_GRIDS; j++) {
      const leftU = u[wrap(i - 1, N_GRIDS)][j];
      const rightU = u[wrap(i + 1, N_GRIDS)][j];
      const upU = u[i][wrap(j - 1, N_GRIDS)];
      const downU = u[i][wrap(j + 1, N_GRIDS)];

      const leftV = v[wrap(i - 1, N_GRIDS)][j];
      const rightV = v[wrap(i + 1, N_GRIDS)][j];
      const upV = v[i][wrap(j - 1, N_GRIDS)];
      const downV = v[i][wrap(j + 1, N_GRIDS)];

      laplacian_u[i][j] = (leftU + rightU + upU + downU - 4 * u[i][j]) / (dx * dx);
      laplacian_v[i][j] = (leftV + rightV + upV + downV - 4 * v[i][j]) / (dx * dx);
    }
  }

  for (let i = 0; i < N_GRIDS; i++) {
    for (let j = 0; j < N_GRIDS; j++) {
      let dudt = Du * laplacian_u[i][j] - u[i][j] * v[i][j] * v[i][j] + f * (1.0 - u[i][j]);
      let dvdt = Dv * laplacian_v[i][j] + u[i][j] * v[i][j] * v[i][j] - (f + k) * v[i][j];
      u[i][j] += dt * dudt;
      v[i][j] += dt * dvdt;
    }
  }

  const max_side = Math.max(width, height);
  const box_size = max_side / N_GRIDS;
  const offsetX = (width - max_side) / 2;
  const offsetY = (height - max_side) / 2;
  for (let i = 0; i < N_GRIDS; i++) {
    for (let j = 0; j < N_GRIDS; j++) {
      fill(u[i][j] * 255);
      rect(offsetX + i * box_size, offsetY + j * box_size, box_size, box_size);
      
    }
  }
}

              
            
!
999px

Console