Page 1 of 1

caLIb - JavaScript CA simulation library

Posted: September 6th, 2025, 8:32 am
by b-engine
caLib is a JavaScript library for cellular automata (and its superset) simulation.
It supports arbitrary dimension and any CA rule, thanks to JavaScript.

How to use
This is supposed to upload to npm, but for some reason it failed. You could still get the library from /src/src.js .

Usage

Code: Select all

// Example: CGoL
import caLib from "caLib";

let grid = caLib.newGrid(10, 2, 0);

grid = caLib.updateCell(grid, [1, 0], 1);
grid = caLib.updateCell(grid, [1, 1], 1);
grid = caLib.updateCell(grid, [1, 2], 1);

const cgol = (cell, coords, getCell) => {
  const [x, y] = coords;

  let live = 0;
  for (let dx = -1; dx <= 1; dx++) {
    for (let dy = -1; dy <= 1; dy++) {
      if (dx === 0 && dy === 0) continue; // skip self
      if (getCell([x + dx, y + dy]) === 1) live++;
    }
  }

  if (cell === 1) {
    return live === 2 || live === 3 ? 1 : 0;
  } else {
    return live === 3 ? 1 : 0;
  }
};

for (let step = 1; step <= 15; step++) {
  grid = caLib.step(grid, cgol);

  console.log(`Step ${step}:`);
  for (let y = 0; y < 5; y++) {
    let row = "";
    for (let x = 0; x < 5; x++) {
      row += caLib.updateCell(grid, [x, y], 0);
      row += caLib.step(grid, cgol) ? "1 " : "0 ";
    }
    console.log(row);
  }
}
Bugs
Kindly report any bugs or issues here.
For other questions, ask in this thread instead.

Re: caLIb - JavaScript CA simulation library

Posted: September 13th, 2025, 12:29 am
by b-engine
Update 0.2.0

Added toRule(), which converts rulestrings to functions usable by caLib. Currently only support B/S notation.

Example before:

Code: Select all

// Example: CGoL
import caLib from "caLib";

let grid = caLib.newGrid(10, 2, 0);

grid = caLib.updateCell(grid, [1, 0], 1);
grid = caLib.updateCell(grid, [1, 1], 1);
grid = caLib.updateCell(grid, [1, 2], 1);

const cgol = (cell, coords, getCell) => {
  const [x, y] = coords;

  let live = 0;
  for (let dx = -1; dx <= 1; dx++) {
    for (let dy = -1; dy <= 1; dy++) {
      if (dx === 0 && dy === 0) continue; // skip self
      if (getCell([x + dx, y + dy]) === 1) live++;
    }
  }

  if (cell === 1) {
    return live === 2 || live === 3 ? 1 : 0;
  } else {
    return live === 3 ? 1 : 0;
  }
};

for (let step = 1; step <= 15; step++) {
  grid = caLib.step(grid, cgol);

  console.log(`Step ${step}:`);
  for (let y = 0; y < 5; y++) {
    let row = "";
    for (let x = 0; x < 5; x++) {
      row += caLib.updateCell(grid, [x, y], 0);
      row += caLib.step(grid, cgol) ? "1 " : "0 ";
    }
    console.log(row);
  }
}
Example after:

Code: Select all

// Example: CGoL
import caLib from "caLib";

let grid = caLib.newGrid(10, 2, 0);

grid = caLib.updateCell(grid, [1, 0], 1);
grid = caLib.updateCell(grid, [1, 1], 1);
grid = caLib.updateCell(grid, [1, 2], 1);

const cgol = toRule("B3/S23");

for (let step = 1; step <= 15; step++) {
  grid = caLib.step(grid, cgol);

  console.log(`Step ${step}:`);
  for (let y = 0; y < 5; y++) {
    let row = "";
    for (let x = 0; x < 5; x++) {
      row += caLib.updateCell(grid, [x, y], 0);
      row += caLib.step(grid, cgol) ? "1 " : "0 ";
    }
    console.log(row);
  }
}

Re: caLib - JavaScript CA simulation library

Posted: November 10th, 2025, 7:57 am
by hth3
Can you use it in Turbowarp and other Scratch mods as an extension?
Also, I want to contribute to it, but I use Codeberg only, not GitHub, how can I contribute?

Re: caLib - JavaScript CA simulation library

Posted: November 10th, 2025, 8:10 am
by b-engine
hth3 wrote: November 10th, 2025, 7:57 am Can you use it in Turbowarp and other Scratch mods as an extension?
I never tried, and guess no. In fact I never tested if it works.
hth3 wrote: November 10th, 2025, 7:57 am Also, I want to contribute to it, but I use Codeberg only, not GitHub, how can I contribute?
This repository is only available on GitHub, sorry.