css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

            
              <canvas id="world"></canvas>

            
          
!
            
              * {
  margin: 0;
  padding: 0;
  box-sizing: border-box; }

body {
  background: #f0f0f0; }

canvas {
  width: 100vw;
  height: 100vh;
  filter: invert(0%) blur(2px) contrast(1); }
            
          
!
            
              const gra = function (min, max) {
    return Math.random() * (max - min) + min;
}
const gri = function (min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
function rgb(r, g, b) {
    return 'rgb(' + r + ',' + b + ',' + g + ')'
}

let c;
const rad = Math.PI / 180;
let grid = []

/**
Given a cell (through column and row coordinates), returns difference between it and a weighted average of a 3x3 convolution
@returns Array
*/
function laplacian(grid, x, y, parameter) {
    /*
|.05 |.2|.05|
| .2 |-1| .2|
|.05 |.2|.05|
*/
    let m = .2;
    let r = .05;
    let sum = 0;
    sum += grid[x][y][parameter] * -1; // the center
    sum += grid[x - 1][y][parameter] * m; // to the left
    sum += grid[x + 1][y][parameter] * m; // to the right
    sum += grid[x][y + 1][parameter] * m; // above
    sum += grid[x][y - 1][parameter] * m; // below
    sum += grid[x - 1][y - 1][parameter] * r; // top left
    sum += grid[x + 1][y - 1][parameter] * r; // top right
    sum += grid[x - 1][y + 1][parameter] * r; // bottom left
    sum += grid[x + 1][y + 1][parameter] * r; // bottom right

    return sum;
}

var config = {
    xCount: 0,
    yCount: 0,
    cellSize: 8,
    a: {
        diffusion: .6
    },
    b: {
        diffusion: .3
    },
    feed: 0.016,
    kill: 0.05,
    deltaT: 1
}


function Cell(a, b) {
    this.a = a;
    this.b = b;
}

function seed() {
    // center 
    let a = Math.floor(config.xCount / 2)
    let b = Math.floor(config.yCount / 2)
    let r = Math.floor(config.xCount / 20);
    for (let x = 0; x < config.xCount; x++) {
        for (let y = 0; y < config.yCount; y++) {
            if ((x - a) * (x - a) + (y - b) * (y - b) <= r * r) {
                grid[x][y].b = gra(0, 1);
            }

            // cut some lines
            let n = 3;
            if ((x > a - n && x < a + n) || (y > b - n && y < b + n)) {
                // grid[x][y].b = 0;
            }
        }
    }

}

function init() {
    c = document.getElementById('world').getContext('2d');
    c.canvas.width = window.innerWidth - 10;
    c.canvas.height = window.innerHeight - 10;
    window.requestAnimationFrame(draw);
    // initialize grid
    config.xCount = Math.floor(c.canvas.width / config.cellSize)
    config.yCount = Math.floor(c.canvas.height / config.cellSize)
    for (let x = 0; x < config.xCount; x++) {
        grid.push([])
    }
    for (let x = 0; x < grid.length; x++) {
        for (let y = 0; y < config.yCount; y++) {
            grid[x].push(new Cell(1, 0))
        }
    }
    // Seed
    for (let i = 0; i < 1; i++) {
        seed()
    }
    window.setInterval(update, 1);
}

function update() {
    console.log('Update')
    for (let x = 1; x < config.xCount - 1; x++) {
        for (let y = 1; y < config.yCount - 1; y++) {
            let cell = grid[x][y]
            // Gray-Scott below:
            cell.a = cell.a + ((config.a.diffusion * laplacian(grid, x, y, 'a') - (cell.a * cell.b * cell.b) + config.feed * (1 - cell.a))) * config.deltaT;
            cell.b = cell.b + ((config.b.diffusion * laplacian(grid, x, y, 'b') + (cell.a * cell.b * cell.b) - (config.kill + config.feed) * cell.b)) * config.deltaT;
        }
    }
}

function draw() {
    c.clearRect(0, 0, c.canvas.width, c.canvas.height)
    // Draw the grid
    // We refer to the config object for performance
    for (let x = 0; x < config.xCount - 2; x++) {
        for (let y = 0; y < config.yCount - 2; y++) {
            let cell = grid[x][y]
            let val = (cell.a - cell.b) * 240;
            c.fillStyle = rgb(val, val, val)
            //c.fillStyle = rgb(cell.a * 250, 100, cell.b * 250)
            c.fillRect(x * config.cellSize, y * config.cellSize, config.cellSize, config.cellSize)
        }
    }

    window.requestAnimationFrame(draw);
}

window.addEventListener('DOMContentLoaded', init)
window.addEventListener('click', function () {

})
            
          
!
999px
🕑 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.
Loading ..................

Console