265 lines
8.6 KiB
HTML
265 lines
8.6 KiB
HTML
|
|
<!DOCTYPE html>
|
||
|
|
<html lang="en">
|
||
|
|
<head>
|
||
|
|
<meta charset="UTF-8">
|
||
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
|
|
<title>Neurameba Cellular Automata</title>
|
||
|
|
<style>
|
||
|
|
body {
|
||
|
|
margin: 0;
|
||
|
|
overflow: hidden;
|
||
|
|
background: #0a0a0a;
|
||
|
|
font-family: 'Courier New', monospace;
|
||
|
|
}
|
||
|
|
canvas {
|
||
|
|
display: block;
|
||
|
|
}
|
||
|
|
#attribution {
|
||
|
|
position: absolute;
|
||
|
|
bottom: 10px;
|
||
|
|
right: 10px;
|
||
|
|
color: #555;
|
||
|
|
font-size: 10px;
|
||
|
|
transform: rotate(-0.5deg);
|
||
|
|
}
|
||
|
|
</style>
|
||
|
|
</head>
|
||
|
|
<body>
|
||
|
|
<canvas id="canvas"></canvas>
|
||
|
|
<div id="attribution">neurameba · motd.social</div>
|
||
|
|
<script>
|
||
|
|
const canvas = document.getElementById('canvas');
|
||
|
|
const ctx = canvas.getContext('2d');
|
||
|
|
|
||
|
|
function resizeCanvas() {
|
||
|
|
canvas.width = window.innerWidth;
|
||
|
|
canvas.height = window.innerHeight;
|
||
|
|
}
|
||
|
|
|
||
|
|
window.addEventListener('resize', resizeCanvas);
|
||
|
|
resizeCanvas();
|
||
|
|
|
||
|
|
// Color palette (dryness=monochrome)
|
||
|
|
const palette = [
|
||
|
|
'#1a1a1a', '#2d2d2d', '#4a4a4a',
|
||
|
|
'#6b6b6b', '#999999', '#c0c0c0'
|
||
|
|
];
|
||
|
|
|
||
|
|
// Cellular automata parameters
|
||
|
|
const RULES = {
|
||
|
|
survival: [2, 3],
|
||
|
|
birth: [3, 4],
|
||
|
|
density: 0.457,
|
||
|
|
complexity: 0.438,
|
||
|
|
motion: 0.479,
|
||
|
|
connectedness: 0.501,
|
||
|
|
lifespan: 0.489,
|
||
|
|
pulse: { avg: 0.39, min: 0.30, max: 1.60 }
|
||
|
|
};
|
||
|
|
|
||
|
|
// Grid setup
|
||
|
|
const CELL_SIZE = 4;
|
||
|
|
const GRID_WIDTH = Math.floor(canvas.width / CELL_SIZE);
|
||
|
|
const GRID_HEIGHT = Math.floor(canvas.height / CELL_SIZE);
|
||
|
|
|
||
|
|
let grid = new Array(GRID_HEIGHT).fill().map(() =>
|
||
|
|
new Array(GRID_WIDTH).fill(0).map(() =>
|
||
|
|
Math.random() < RULES.density ? 1 : 0
|
||
|
|
)
|
||
|
|
);
|
||
|
|
|
||
|
|
let nextGrid = JSON.parse(JSON.stringify(grid));
|
||
|
|
let frameCount = 0;
|
||
|
|
let pulseTimer = 0;
|
||
|
|
|
||
|
|
// Cellular automaton rules
|
||
|
|
function updateGrid() {
|
||
|
|
pulseTimer += (Math.random() * 0.1 - 0.05) * RULES.pulse.max;
|
||
|
|
if (pulseTimer > RULES.pulse.max) pulseTimer = RULES.pulse.max;
|
||
|
|
if (pulseTimer < RULES.pulse.min) pulseTimer = RULES.pulse.min;
|
||
|
|
pulseTimer += RULES.pulse.avg * 0.1;
|
||
|
|
|
||
|
|
for (let y = 0; y < GRID_HEIGHT; y++) {
|
||
|
|
for (let x = 0; x < GRID_WIDTH; x++) {
|
||
|
|
const neighbors = countNeighbors(x, y);
|
||
|
|
|
||
|
|
if (grid[y][x] === 1) {
|
||
|
|
nextGrid[y][x] = RULES.survival.includes(neighbors) ? 1 : 0;
|
||
|
|
} else {
|
||
|
|
nextGrid[y][x] = RULES.birth.includes(neighbors) ? 1 : 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Apply complexity variation
|
||
|
|
if (Math.random() < RULES.complexity * 0.1) {
|
||
|
|
if (Math.random() < 0.5) {
|
||
|
|
flipRandomCell();
|
||
|
|
} else {
|
||
|
|
bubbleSortGrid();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Apply connectedness variation
|
||
|
|
if (Math.random() < RULES.connectedness * 0.05) {
|
||
|
|
connectComponents();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Swap grids
|
||
|
|
[grid, nextGrid] = [nextGrid, grid];
|
||
|
|
|
||
|
|
// Apply lifespan effect
|
||
|
|
if (RULES.lifespan > 0.5) {
|
||
|
|
fadeTrails();
|
||
|
|
} else {
|
||
|
|
evaporateCells();
|
||
|
|
}
|
||
|
|
|
||
|
|
frameCount++;
|
||
|
|
}
|
||
|
|
|
||
|
|
function countNeighbors(x, y) {
|
||
|
|
let count = 0;
|
||
|
|
for (let dy = -1; dy <= 1; dy++) {
|
||
|
|
for (let dx = -1; dx <= 1; dx++) {
|
||
|
|
if (dx === 0 && dy === 0) continue;
|
||
|
|
const nx = (x + dx + GRID_WIDTH) % GRID_WIDTH;
|
||
|
|
const ny = (y + dy + GRID_HEIGHT) % GRID_HEIGHT;
|
||
|
|
count += grid[ny][nx];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return count;
|
||
|
|
}
|
||
|
|
|
||
|
|
function flipRandomCell() {
|
||
|
|
const x = Math.floor(Math.random() * GRID_WIDTH);
|
||
|
|
const y = Math.floor(Math.random() * GRID_HEIGHT);
|
||
|
|
nextGrid[y][x] = 1 - nextGrid[y][x];
|
||
|
|
}
|
||
|
|
|
||
|
|
function bubbleSortGrid() {
|
||
|
|
for (let i = 0; i < 10; i++) {
|
||
|
|
const x1 = Math.floor(Math.random() * GRID_WIDTH);
|
||
|
|
const y1 = Math.floor(Math.random() * GRID_HEIGHT);
|
||
|
|
const x2 = Math.floor(Math.random() * GRID_WIDTH);
|
||
|
|
const y2 = Math.floor(Math.random() * GRID_HEIGHT);
|
||
|
|
|
||
|
|
[nextGrid[y1][x1], nextGrid[y2][x2]] = [nextGrid[y2][x2], nextGrid[y1][x1]];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function connectComponents() {
|
||
|
|
for (let i = 0; i < 5; i++) {
|
||
|
|
const x = Math.floor(Math.random() * GRID_WIDTH);
|
||
|
|
const y = Math.floor(Math.random() * GRID_HEIGHT);
|
||
|
|
if (nextGrid[y][x] === 1) {
|
||
|
|
growConnection(x, y);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function growConnection(x, y) {
|
||
|
|
const steps = Math.floor(Math.random() * 5) + 1;
|
||
|
|
for (let i = 0; i < steps; i++) {
|
||
|
|
const nx = (x + Math.floor(Math.random() * 3) - 1 + GRID_WIDTH) % GRID_WIDTH;
|
||
|
|
const ny = (y + Math.floor(Math.random() * 3) - 1 + GRID_HEIGHT) % GRID_HEIGHT;
|
||
|
|
nextGrid[ny][nx] = 1;
|
||
|
|
x = nx;
|
||
|
|
y = ny;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function fadeTrails() {
|
||
|
|
for (let y = 0; y < GRID_HEIGHT; y++) {
|
||
|
|
for (let x = 0; x < GRID_WIDTH; x++) {
|
||
|
|
if (nextGrid[y][x] === 1 && Math.random() > 0.8) {
|
||
|
|
nextGrid[y][x] = 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function evaporateCells() {
|
||
|
|
for (let y = 0; y < GRID_HEIGHT; y++) {
|
||
|
|
for (let x = 0; x < GRID_WIDTH; x++) {
|
||
|
|
nextGrid[y][x] = Math.max(0, nextGrid[y][x] - 0.01);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Drawing
|
||
|
|
function drawGrid() {
|
||
|
|
ctx.fillStyle = '#0a0a0a';
|
||
|
|
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||
|
|
|
||
|
|
const cellPadding = 0.5 * CELL_SIZE * (1 - RULES.motion);
|
||
|
|
const gridAlpha = RULES.lifespan * 0.7 + 0.3;
|
||
|
|
|
||
|
|
for (let y = 0; y < GRID_HEIGHT; y++) {
|
||
|
|
for (let x = 0; x < GRID_WIDTH; x++) {
|
||
|
|
if (grid[y][x] > 0) {
|
||
|
|
const intensity = Math.min(1, grid[y][x] * 0.8 + pulseTimer * 0.2);
|
||
|
|
const baseColor = palette[Math.floor(intensity * (palette.length - 1))];
|
||
|
|
ctx.fillStyle = baseColor;
|
||
|
|
|
||
|
|
const px = x * CELL_SIZE + cellPadding;
|
||
|
|
const py = y * CELL_SIZE + cellPadding;
|
||
|
|
const size = CELL_SIZE - cellPadding * 2;
|
||
|
|
|
||
|
|
ctx.fillRect(px, py, size * intensity, size * intensity);
|
||
|
|
|
||
|
|
if (Math.random() < 0.1 * RULES.complexity) {
|
||
|
|
drawCellDetail(px, py, size, intensity);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function drawCellDetail(px, py, size, intensity) {
|
||
|
|
ctx.strokeStyle = palette[Math.floor(intensity * (palette.length - 1))] + '80';
|
||
|
|
ctx.lineWidth = 0.5;
|
||
|
|
|
||
|
|
switch (Math.floor(Math.random() * 3)) {
|
||
|
|
case 0:
|
||
|
|
// Diagonal lines
|
||
|
|
ctx.beginPath();
|
||
|
|
ctx.moveTo(px, py);
|
||
|
|
ctx.lineTo(px + size, py + size);
|
||
|
|
ctx.stroke();
|
||
|
|
break;
|
||
|
|
case 1:
|
||
|
|
// Small dot
|
||
|
|
ctx.beginPath();
|
||
|
|
ctx.arc(
|
||
|
|
px + size * 0.5,
|
||
|
|
py + size * 0.5,
|
||
|
|
size * 0.2 * Math.random(),
|
||
|
|
0,
|
||
|
|
Math.PI * 2
|
||
|
|
);
|
||
|
|
ctx.fill();
|
||
|
|
break;
|
||
|
|
case 2:
|
||
|
|
// Partial fill
|
||
|
|
ctx.fillRect(
|
||
|
|
px + size * 0.2 * Math.random(),
|
||
|
|
py + size * 0.2 * Math.random(),
|
||
|
|
size * 0.6 * Math.random(),
|
||
|
|
size * 0.6 * Math.random()
|
||
|
|
);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Animation loop
|
||
|
|
function animate() {
|
||
|
|
updateGrid();
|
||
|
|
drawGrid();
|
||
|
|
requestAnimationFrame(animate);
|
||
|
|
}
|
||
|
|
|
||
|
|
animate();
|
||
|
|
</script>
|
||
|
|
</body>
|
||
|
|
</html>
|