164 lines
No EOL
5.3 KiB
HTML
164 lines
No EOL
5.3 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Chemical Dreams</title>
|
|
<style>
|
|
body {
|
|
margin: 0;
|
|
overflow: hidden;
|
|
background: #0a0a0a;
|
|
color: #ccc;
|
|
font-family: 'Courier New', monospace;
|
|
}
|
|
#canvas {
|
|
display: block;
|
|
}
|
|
#attribution {
|
|
position: absolute;
|
|
bottom: 10px;
|
|
right: 10px;
|
|
font-size: 10px;
|
|
color: #555;
|
|
text-shadow: 0 0 5px rgba(0,0,0,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,
|
|
diffusionRateB: 0.5,
|
|
width: Math.floor(canvas.width / 2),
|
|
height: Math.floor(canvas.height / 2),
|
|
cellSize: 2
|
|
};
|
|
|
|
// Initialize grids
|
|
let grid = new Array(params.width * params.height);
|
|
let nextGrid = new Array(params.width * params.height);
|
|
|
|
// Initialize with random values
|
|
function initGrid() {
|
|
for (let i = 0; i < grid.length; i++) {
|
|
grid[i] = Math.random() * 0.1;
|
|
nextGrid[i] = grid[i];
|
|
}
|
|
|
|
// Add some initial patterns
|
|
for (let i = 0; i < params.width * params.height * 0.01; i++) {
|
|
const idx = Math.floor(Math.random() * grid.length);
|
|
grid[idx] = 1;
|
|
}
|
|
}
|
|
|
|
initGrid();
|
|
|
|
// Reaction-diffusion kernel
|
|
function updateGrid() {
|
|
for (let x = 1; x < params.width - 1; x++) {
|
|
for (let y = 1; y < params.height - 1; y++) {
|
|
const idx = x + y * params.width;
|
|
const a = grid[idx];
|
|
const b = nextGrid[idx];
|
|
|
|
// Apply reaction-diffusion equations
|
|
const reaction = a * b * b;
|
|
const feed = params.feedRate * (1 - a);
|
|
const kill = params.killRate * a;
|
|
const newA = a + (params.diffusionRateA * laplacian(x, y, 'a') - reaction + feed - kill) * 0.1;
|
|
const newB = b + (params.diffusionRateB * laplacian(x, y, 'b') + reaction - kill) * 0.1;
|
|
|
|
nextGrid[idx] = newA;
|
|
nextGrid[idx + params.width * params.height] = newB;
|
|
}
|
|
}
|
|
|
|
// Swap grids
|
|
[grid, nextGrid] = [nextGrid, grid];
|
|
}
|
|
|
|
// Calculate Laplacian for a given cell
|
|
function laplacian(x, y, type) {
|
|
let sum = 0;
|
|
const centerIdx = x + y * params.width;
|
|
|
|
for (let dx = -1; dx <= 1; dx++) {
|
|
for (let dy = -1; dy <= 1; dy++) {
|
|
const idx = (x + dx) + (y + dy) * params.width;
|
|
if (type === 'a') {
|
|
sum += grid[idx];
|
|
} else {
|
|
sum += nextGrid[idx + params.width * params.height];
|
|
}
|
|
}
|
|
}
|
|
|
|
return (sum - 9 * (type === 'a' ? grid[centerIdx] : nextGrid[centerIdx])) / 9;
|
|
}
|
|
|
|
// Draw the grid
|
|
function drawGrid() {
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
// Create image data for better performance
|
|
const imageData = ctx.createImageData(params.width, params.height);
|
|
const data = imageData.data;
|
|
|
|
for (let i = 0; i < grid.length; i++) {
|
|
const valA = Math.min(1, Math.max(0, grid[i]));
|
|
const valB = Math.min(1, Math.max(0, nextGrid[i + params.width * params.height]));
|
|
|
|
// Convert reaction-diffusion values to color
|
|
const r = Math.floor(valA * 255);
|
|
const g = Math.floor((valA + valB) * 127);
|
|
const b = Math.floor(valB * 255);
|
|
|
|
const idx = i * 4;
|
|
data[idx] = r;
|
|
data[idx + 1] = g;
|
|
data[idx + 2] = b;
|
|
data[idx + 3] = 255;
|
|
}
|
|
|
|
// Draw to canvas with scaling
|
|
const tempCanvas = document.createElement('canvas');
|
|
const tempCtx = tempCanvas.getContext('2d');
|
|
tempCanvas.width = params.width;
|
|
tempCanvas.height = params.height;
|
|
tempCtx.putImageData(imageData, 0, 0);
|
|
|
|
ctx.imageSmoothingEnabled = false;
|
|
ctx.drawImage(tempCanvas,
|
|
0, 0, params.width, params.height,
|
|
0, 0, canvas.width, canvas.height);
|
|
}
|
|
|
|
// Animation loop
|
|
function animate() {
|
|
updateGrid();
|
|
drawGrid();
|
|
requestAnimationFrame(animate);
|
|
}
|
|
|
|
animate();
|
|
</script>
|
|
</body>
|
|
</html> |