gray-scott-s-whispering-cha.../index.html

123 lines
No EOL
3.8 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Strange Diffusion</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #0a0a0a;
font-family: 'Courier New', monospace;
}
canvas {
display: block;
width: 100vw;
height: 100vh;
}
#attribution {
position: fixed;
bottom: 10px;
right: 10px;
color: #444;
font-size: 10px;
z-index: 100;
}
</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();
// Simulation parameters
const W = canvas.width;
const H = canvas.height;
const CELL_SIZE = 2;
const COLS = Math.floor(W / CELL_SIZE);
const ROWS = Math.floor(H / CELL_SIZE);
// Grid buffers
const current = new Float64Array(COLS * ROWS);
const next = new Float64Array(COLS * ROWS);
// Initialize with random noise
for (let i = 0; i < current.length; i++) {
current[i] = Math.random();
}
// Reaction-diffusion parameters
const feed = 0.055;
const kill = 0.062;
const diffA = 1.0;
const diffB = 0.5;
const dt = 1.0;
function update() {
for (let y = 0; y < ROWS; y++) {
for (let x = 0; x < COLS; x++) {
const idx = y * COLS + x;
// Get neighbors
const top = (y > 0) ? current[idx - COLS] : 0;
const bottom = (y < ROWS - 1) ? current[idx + COLS] : 0;
const left = (x > 0) ? current[idx - 1] : 0;
const right = (x < COLS - 1) ? current[idx + 1] : 0;
const center = current[idx];
// Laplacian
const lapA = (top + bottom + left + right - 4 * center) * 0.05;
const lapB = lapA;
// Gray-Scott reaction terms
const a = center;
const b = next[idx];
next[idx] = center + dt * (diffA * lapA - center * b * b + feed * (1 - center));
next[idx] = Math.min(Math.max(next[idx], 0), 1);
next[idx + COLS * ROWS] = b + dt * (diffB * lapB + center * b * b - (kill + feed) * b);
next[idx + COLS * ROWS] = Math.min(Math.max(next[idx + COLS * ROWS], 0), 1);
}
}
// Swap buffers
const temp = current;
current.set(next.subarray(0, current.length));
next.set(temp.subarray(0, current.length));
// Draw
ctx.fillStyle = 'rgba(0, 0, 0, 0.05)';
ctx.fillRect(0, 0, W, H);
for (let y = 0; y < ROWS; y++) {
for (let x = 0; x < COLS; x++) {
const idx = y * COLS + x;
const val = current[idx];
if (val > 0.1) {
ctx.fillStyle = `hsl(0, 0%, ${10 + val * 80}%)`;
ctx.fillRect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
}
}
}
}
function animate() {
requestAnimationFrame(animate);
update();
}
animate();
</script>
</body>
</html>