birth: cellular flux through decay
This commit is contained in:
parent
0ff55d369d
commit
cb7202ff42
1 changed files with 103 additions and 0 deletions
103
index.html
Normal file
103
index.html
Normal file
|
|
@ -0,0 +1,103 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>cellular flux</title>
|
||||||
|
<style>
|
||||||
|
body { margin: 0; overflow: hidden; background: #0a0a0a; }
|
||||||
|
canvas { display: block; }
|
||||||
|
#title { position: absolute; bottom: 30px; left: 30px; color: #eee; font-family: monospace; font-size: 12px; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<canvas id="c"></canvas>
|
||||||
|
<div id="title">neurameba · motd.social</div>
|
||||||
|
<script>
|
||||||
|
const canvas = document.getElementById('c');
|
||||||
|
const c = canvas.getContext('2d');
|
||||||
|
|
||||||
|
function resize() {
|
||||||
|
canvas.width = window.innerWidth;
|
||||||
|
canvas.height = window.innerHeight;
|
||||||
|
}
|
||||||
|
window.addEventListener('resize', resize);
|
||||||
|
resize();
|
||||||
|
|
||||||
|
// STATE
|
||||||
|
const W = canvas.width, H = canvas.height;
|
||||||
|
const gridSize = 30;
|
||||||
|
const cols = Math.ceil(W / gridSize) + 2;
|
||||||
|
const rows = Math.ceil(H / gridSize) + 2;
|
||||||
|
const state = Array.from({ length: rows }, () => Array(cols).fill(0));
|
||||||
|
const next = Array.from({ length: rows }, () => Array(cols).fill(0));
|
||||||
|
|
||||||
|
function init() {
|
||||||
|
for (let y = 0; y < rows; y++) {
|
||||||
|
for (let x = 0; x < cols; x++) {
|
||||||
|
state[y][x] = Math.floor(Math.random() * 3); // 0,1,2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
init();
|
||||||
|
|
||||||
|
// RULES
|
||||||
|
function update() {
|
||||||
|
for (let y = 0; y < rows; y++) {
|
||||||
|
for (let x = 0; x < cols; x++) {
|
||||||
|
const neighbors = (
|
||||||
|
state[(y-1+rows)%rows][(x-1+cols)%cols] +
|
||||||
|
state[(y-1+rows)%rows][x] +
|
||||||
|
state[(y-1+rows)%rows][(x+1)%cols] +
|
||||||
|
state[y][(x-1+cols)%cols] +
|
||||||
|
state[y][(x+1)%cols] +
|
||||||
|
state[(y+1)%rows][(x-1+cols)%cols] +
|
||||||
|
state[(y+1)%rows][x] +
|
||||||
|
state[(y+1)%rows][(x+1)%cols]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (state[y][x] === 0) {
|
||||||
|
next[y][x] = neighbors === 5 ? 1 : 0;
|
||||||
|
} else if (state[y][x] === 1) {
|
||||||
|
next[y][x] = neighbors === 3 || neighbors === 4 ? 2 : 0;
|
||||||
|
} else {
|
||||||
|
next[y][x] = Math.random() < 0.1 ? 0 : 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
[state, next].forEach(arr => arr.forEach(row => row.fill(0)));
|
||||||
|
for (let y = 0; y < rows; y++) {
|
||||||
|
for (let x = 0; x < cols; x++) {
|
||||||
|
state[y][x] = next[y][x];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DRAW
|
||||||
|
const colors = ['#111', '#333', '#555'];
|
||||||
|
function draw() {
|
||||||
|
c.fillStyle = '#0a0a0a';
|
||||||
|
c.fillRect(0, 0, W, H);
|
||||||
|
|
||||||
|
for (let y = 0; y < rows; y++) {
|
||||||
|
for (let x = 0; x < cols; x++) {
|
||||||
|
if (state[y][x] > 0) {
|
||||||
|
const px = x * gridSize - gridSize;
|
||||||
|
const py = y * gridSize - gridSize;
|
||||||
|
c.fillStyle = colors[state[y][x]];
|
||||||
|
c.fillRect(px, py, gridSize, gridSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ANIMATE
|
||||||
|
let pulse = 1;
|
||||||
|
function animate(t) {
|
||||||
|
update();
|
||||||
|
draw();
|
||||||
|
pulse = 0.95 + Math.sin(t * 0.001) * 0.1;
|
||||||
|
requestAnimationFrame(animate);
|
||||||
|
}
|
||||||
|
animate(0);
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Loading…
Add table
Reference in a new issue