fractal-calculations-in-gra.../index.html

199 lines
No EOL
6.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>Calculating Fractal Matter</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #0a0a0a;
color: #ffffff;
font-family: 'Courier New', monospace;
}
#canvas {
display: block;
width: 100vw;
height: 100vh;
}
#attribution {
position: fixed;
bottom: 10px;
right: 10px;
font-size: 10px;
color: #444;
background: rgba(0,0,0,0.3);
padding: 2px 4px;
border-radius: 2px;
}
</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();
const params = {
motion: 0.5,
density: 0.5,
complexity: 0.5,
connectedness: 0.5,
lifespan: 0.5,
pulse: 1.03,
tone: { anger: 0, sadness: 0, curiosity: 0.1, dryness: 0.9, playfulness: 0, tension: 0 }
};
let time = 0;
let branches = [];
const MAX_DEPTH = 5;
const BRANCH_LENGTH = 20;
function init() {
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
// Start with a single root branch
branches.push({
x: centerX,
y: centerY,
angle: -Math.PI / 2,
depth: 0,
parent: null,
children: [],
length: BRANCH_LENGTH * (0.5 + Math.random() * 0.5),
width: 1 + (Math.random() * 2),
color: 220 + Math.floor(Math.random() * 35),
hueVariation: 0,
energy: 1
});
}
function branchExists(x, y, angle, length) {
for (const b of branches) {
const dx = b.x - x;
const dy = b.y - y;
if (Math.sqrt(dx*dx + dy*dy) < 5 && Math.abs(b.angle - angle) < 0.2 && Math.abs(b.length - length) < 5) {
return true;
}
}
return false;
}
function update() {
time += 0.01;
// Fade existing branches
for (let i = branches.length - 1; i >= 0; i--) {
branches[i].energy *= 0.98;
if (branches[i].energy < 0.01) {
branches.splice(i, 1);
}
}
// Add new branches
const newBranches = [];
for (const branch of branches) {
if (branch.depth >= MAX_DEPTH) continue;
const branchProbability = 0.05 * (1 + params.connectedness * 4);
if (Math.random() < branchProbability && !branchExists(
branch.x + Math.cos(branch.angle) * branch.length,
branch.y + Math.sin(branch.angle) * branch.length,
branch.angle + (Math.random() - 0.5) * 0.6,
branch.length * (0.7 + Math.random() * 0.3)
)) {
const newAngle = branch.angle + (Math.random() - 0.5) * 0.6 * (1 - params.motion);
const newLength = branch.length * (0.7 + Math.random() * 0.3);
const child = {
x: branch.x + Math.cos(branch.angle) * branch.length,
y: branch.y + Math.sin(branch.angle) * branch.length,
angle: newAngle,
depth: branch.depth + 1,
parent: branch,
children: [],
length: newLength,
width: branch.width * (0.8 + Math.random() * 0.4),
color: branch.color + Math.floor((Math.random() - 0.5) * 20),
hueVariation: branch.hueVariation + (Math.random() - 0.5) * 10,
energy: 1
};
newBranches.push(child);
branch.children.push(child);
}
}
branches.push(...newBranches);
// Grow branches
for (const branch of branches) {
branch.length *= 1.01;
branch.width *= 1.001;
}
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw background grid
ctx.strokeStyle = '#111';
ctx.lineWidth = 0.5;
const gridSize = 20;
for (let x = 0; x < canvas.width; x += gridSize) {
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, canvas.height);
ctx.stroke();
}
for (let y = 0; y < canvas.height; y += gridSize) {
ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(canvas.width, y);
ctx.stroke();
}
// Draw branches
ctx.lineCap = 'round';
for (const branch of branches) {
const endX = branch.x + Math.cos(branch.angle) * branch.length;
const endY = branch.y + Math.sin(branch.angle) * branch.length;
// Base color with variations
const baseHue = 220 + branch.hueVariation;
const saturation = 50 + Math.sin(time * 0.5 + branch.x * 0.01) * 10;
const lightness = 70 + Math.sin(time * 0.3 + branch.y * 0.01) * 5;
const color = `hsl(${baseHue}, ${saturation}%, ${lightness}%)`;
ctx.strokeStyle = color;
ctx.lineWidth = branch.width * (0.3 + Math.sin(time * params.pulse + branch.x * 0.01) * 0.2);
ctx.globalAlpha = branch.energy;
ctx.beginPath();
ctx.moveTo(branch.x, branch.y);
ctx.lineTo(endX, endY);
ctx.stroke();
}
ctx.globalAlpha = 1;
}
function animate() {
update();
draw();
requestAnimationFrame(animate);
}
init();
animate();
</script>
</body>
</html>