diffusive-patterns-unfoldin.../index.html

148 lines
No EOL
4.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 Reaction Diffusion</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #0a0a1a;
font-family: 'Courier New', monospace;
}
canvas {
display: block;
}
#credits {
position: fixed;
bottom: 10px;
right: 10px;
color: #555;
font-size: 10px;
text-shadow: 0 0 5px rgba(0,0,0,0.5);
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div id="credits">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();
// Grid dimensions
const cols = Math.floor(canvas.width / 4);
const rows = Math.floor(canvas.height / 4);
// Reaction-diffusion parameters
const params = {
feedRate: 0.055,
killRate: 0.062,
diffusionA: 1.0,
diffusionB: 0.5,
timeStep: 1.0,
threshold: 0.15,
palette: [
'#ffffff', // dryness (monochrome)
'#e0e0e0',
'#a0a0a0'
]
};
// Main grid and buffer
let grid = new Array(cols * rows);
let next = new Array(cols * rows);
// Initialize with random values
function initGrid() {
for (let i = 0; i < grid.length; i++) {
grid[i] = Math.random() * 0.2;
next[i] = 0;
}
}
// Moore neighborhood
function getNeighbors(x, y) {
const neighbors = [];
for (let dy = -1; dy <= 1; dy++) {
for (let dx = -1; dx <= 1; dx++) {
if (dx === 0 && dy === 0) continue;
const nx = (x + dx + cols) % cols;
const ny = (y + dy + rows) % rows;
neighbors.push(grid[ny * cols + nx]);
}
}
return neighbors;
}
// Reaction-diffusion step
function updateGrid() {
for (let y = 0; y < rows; y++) {
for (let x = 0; x < cols; x++) {
const idx = y * cols + x;
const neighbors = getNeighbors(x, y);
const sumA = neighbors.reduce((a, b) => a + b, 0);
const currentA = grid[idx];
const currentB = 1 - currentA;
// Gray-Scott model equations
const reaction = currentA * currentB * currentB;
const newA = currentA +
(params.diffusionA * sumA - reaction) * params.timeStep -
params.feedRate * currentA;
const newB = currentB +
(params.diffusionB * sumA + reaction) * params.timeStep -
params.killRate * currentB * currentB +
params.feedRate * currentA;
next[idx] = Math.min(1, Math.max(0, newA));
}
}
// Swap buffers
[grid, next] = [next, grid];
}
// Render function
function render() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let y = 0; y < rows; y++) {
for (let x = 0; x < cols; x++) {
const idx = y * cols + x;
const val = grid[idx];
if (val > params.threshold) {
const brightness = Math.floor(255 * (val - params.threshold) / (1 - params.threshold));
const color = params.palette[Math.floor((brightness / 255) * (params.palette.length - 1))];
ctx.fillStyle = color;
const px = x * 4;
const py = y * 4;
ctx.fillRect(px, py, 4, 4);
}
}
}
}
// Animation loop
function animate() {
updateGrid();
render();
requestAnimationFrame(animate);
}
initGrid();
animate();
</script>
</body>
</html>