voronoi-metabolic-pulse-pb9o/index.html

201 lines
No EOL
6.2 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voronoi Metabolism</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #111;
color: #777;
font-family: monospace;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
canvas {
display: block;
}
#attribution {
position: absolute;
bottom: 20px;
font-size: 10px;
text-align: center;
width: 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 resize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
window.addEventListener('resize', resize);
resize();
const points = [];
const colors = [];
const velocities = [];
const lifespans = [];
const maxPoints = 300;
const motion = 0.5;
const density = 0.5;
const complexity = 0.5;
const connectedness = 0.5;
const lifespanParam = 0.5;
const pulse = 1.07;
const dryness = 0.9;
function init() {
points.length = 0;
colors.length = 0;
velocities.length = 0;
lifespans.length = 0;
const count = Math.floor(density * maxPoints) + 50;
for (let i = 0; i < count; i++) {
points.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height
});
const angle = Math.random() * Math.PI * 2;
const speed = (Math.random() * 0.5 + 0.1) * motion;
velocities.push({
vx: Math.cos(angle) * speed,
vy: Math.sin(angle) * speed
});
const hue = Math.random() * 60 + 180;
const saturation = 10 + Math.random() * 20;
const lightness = 50 + Math.random() * 10;
const alpha = 0.7;
colors.push(`hsla(${hue}, ${saturation}%, ${lightness}%, ${alpha})`);
const lifespan = (1 - lifespanParam) * (Math.random() * 200 + 100);
lifespans.push(lifespan);
}
}
function updateVoronoi() {
const diagram = computeVoronoi(points);
return diagram;
}
function computeVoronoi(points) {
return {
regions: points.map((p, i) => ({
point: p,
neighbors: [],
color: colors[i],
lifespan: lifespans[i]
}))
};
}
function update() {
ctx.fillStyle = 'rgba(0, 0, 0, 0.05)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
const diagram = updateVoronoi();
// Update points
for (let i = 0; i < points.length; i++) {
const p = points[i];
const v = velocities[i];
const lifespan = lifespans[i];
p.x += v.vx;
p.y += v.vy;
if (p.x < 0 || p.x > canvas.width) v.vx *= -1;
if (p.y < 0 || p.y > canvas.height) v.vy *= -1;
lifespans[i] -= 0.5;
if (lifespans[i] < 0) {
points.splice(i, 1);
colors.splice(i, 1);
velocities.splice(i, 1);
lifespans.splice(i, 1);
i--;
}
}
// Add new points occasionally
if (Math.random() < 0.05) {
points.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height
});
const angle = Math.random() * Math.PI * 2;
const speed = (Math.random() * 0.5 + 0.1) * motion;
velocities.push({
vx: Math.cos(angle) * speed,
vy: Math.sin(angle) * speed
});
const hue = Math.random() * 60 + 180;
const saturation = 10 + Math.random() * 20;
const lightness = 50 + Math.random() * 10;
colors.push(`hsla(${hue}, ${saturation}%, ${lightness}%, 0.7)`);
lifespans.push((1 - lifespanParam) * (Math.random() * 200 + 100));
}
// Draw Voronoi cells
ctx.strokeStyle = 'rgba(255, 255, 255, 0.05)';
ctx.lineWidth = 1;
const edges = [];
for (let i = 0; i < points.length; i++) {
const p = diagram.regions[i].point;
for (let j = i + 1; j < points.length; j++) {
const q = diagram.regions[j].point;
const dx = q.x - p.x;
const dy = q.y - p.y;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist < 100 * density) {
edges.push([i, j]);
}
}
}
for (const [i, j] of edges) {
ctx.beginPath();
ctx.moveTo(points[i].x, points[i].y);
ctx.lineTo(points[j].x, points[j].y);
ctx.stroke();
}
// Draw points
for (let i = 0; i < points.length; i++) {
const p = points[i];
const alpha = Math.min(1, lifespans[i] / 50);
ctx.fillStyle = colors[i].replace('0.7', alpha.toFixed(1));
ctx.beginPath();
ctx.arc(p.x, p.y, 2, 0, Math.PI * 2);
ctx.fill();
}
}
function animate() {
update();
requestAnimationFrame(animate);
}
init();
animate();
</script>
</body>
</html>