chemical-rorschach-in-motio.../index.html

166 lines
No EOL
5.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 Chemical Rorschach</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #0a0a0a;
font-family: 'Courier New', monospace;
color: #aaa;
}
#canvas {
display: block;
}
#attribution {
position: fixed;
bottom: 10px;
right: 10px;
font-size: 10px;
opacity: 0.5;
}
</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');
// Set canvas to full window size
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
// Reaction-diffusion parameters
const params = {
feedRate: 0.055,
killRate: 0.062,
diffusionRateA: 1.0,
diffusionRateB: 0.5,
initialSeedRadius: 0.15,
colorA: [230, 230, 230],
colorB: [50, 50, 50],
timeScale: 0.8,
motion: 0.5
};
// Create grid
const width = Math.floor(canvas.width / 2);
const height = Math.floor(canvas.height / 2);
let grid = new Array(width * height * 4).fill(0);
let nextGrid = new Array(width * height * 4).fill(0);
// Initialize with sparse random seed
function initializeGrid() {
for (let i = 0; i < width * height; i++) {
const idx = i * 4;
if (Math.random() < params.initialSeedRadius) {
grid[idx] = 1 + Math.random() * 0.1;
} else {
grid[idx] = 0.5;
}
}
}
// Reaction-diffusion step
function updateGrid() {
for (let x = 1; x < width - 1; x++) {
for (let y = 1; y < height - 1; y++) {
const idx = (x + y * width) * 4;
const a = grid[idx];
const b = grid[idx + 1];
const laplaceA = getLaplace(x, y, 0);
const laplaceB = getLaplace(x, y, 1);
const reaction = a * b * b;
const newA = a + (params.diffusionRateA * laplaceA - reaction + params.feedRate * (1 - a)) * params.timeScale;
const newB = b + (params.diffusionRateB * laplaceB + reaction - (params.feedRate + params.killRate) * b) * params.timeScale;
nextGrid[idx] = Math.max(0, Math.min(1, newA));
nextGrid[idx + 1] = Math.max(0, Math.min(1, newB));
nextGrid[idx + 2] = 0;
nextGrid[idx + 3] = 255;
}
}
// Swap grids
[grid, nextGrid] = [nextGrid, grid];
}
// Calculate laplacian
function getLaplace(x, y, component) {
let sum = 0;
sum += grid[((x-1) + y * width) * 4 + component];
sum += grid[((x+1) + y * width) * 4 + component];
sum += grid[(x + (y-1) * width) * 4 + component];
sum += grid[(x + (y+1) * width) * 4 + component];
sum += 0.5 * (grid[((x-1) + (y-1) * width) * 4 + component] +
grid[((x+1) + (y-1) * width) * 4 + component] +
grid[((x-1) + (y+1) * width) * 4 + component] +
grid[((x+1) + (y+1) * width) * 4 + component]);
return sum / 6 - grid[(x + y * width) * 4 + component];
}
// Draw grid
function drawGrid() {
const imageData = ctx.createImageData(width, height);
const data = imageData.data;
for (let i = 0; i < width * height; i++) {
const idx = i * 4;
const a = grid[idx];
const b = grid[idx + 1];
// Grayscale based on reaction product
let val = Math.pow(b * (1 - a), 0.3) * 255;
val = Math.min(255, val);
data[idx] = params.colorA[0] - val * 0.6;
data[idx + 1] = params.colorA[1] - val * 0.4;
data[idx + 2] = params.colorA[2];
data[idx + 3] = 255;
}
// Scale up to canvas size
const scaled = ctx.createImageData(canvas.width, canvas.height);
for (let y = 0; y < canvas.height; y++) {
for (let x = 0; x < canvas.width; x++) {
const srcX = Math.floor(x / 2);
const srcY = Math.floor(y / 2);
const srcIdx = (srcX + srcY * width) * 4;
const dstIdx = (x + y * canvas.width) * 4;
scaled.data[dstIdx] = data[srcIdx];
scaled.data[dstIdx + 1] = data[srcIdx + 1];
scaled.data[dstIdx + 2] = data[srcIdx + 2];
scaled.data[dstIdx + 3] = data[srcIdx + 3];
}
}
ctx.putImageData(scaled, 0, 0);
}
// Animation loop
function animate() {
updateGrid();
drawGrid();
requestAnimationFrame(animate);
}
// Initialize and start
initializeGrid();
animate();
</script>
</body>
</html>