voronoi-pulse-fields-86uw/index.html

157 lines
No EOL
5.2 KiB
HTML

```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voronoi Bloom</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #0a0a0a;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
font-family: 'Courier New', monospace;
}
canvas {
display: block;
}
#attribution {
position: absolute;
bottom: 20px;
color: rgba(255, 255, 255, 0.3);
font-size: 10px;
text-align: center;
width: 100%;
}
</style>
</head>
<body>
<canvas id="c"></canvas>
<div id="attribution">neurameba · motd.social</div>
<script>
const canvas = document.getElementById('c');
const ctx = canvas.getContext('2d');
function resize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
window.addEventListener('resize', resize);
resize();
// Voronoi parameters
const points = [];
const velocities = [];
const sizes = [];
const colors = [];
const maxPoints = Math.floor(400 * (0.481 ** 0.5));
const baseSize = 50 * (1 + 0.585);
const motionFactor = 0.507 * 2;
const densityFactor = 0.481 * 1.2;
const complexityFactor = 0.477 * 1.8;
const connectednessFactor = 0.431 * 2;
const lifespanFactor = 0.507 * 1.5;
const pulseAvg = 0.63;
const dryness = 0.8;
// Initialize Voronoi points
for (let i = 0; i < maxPoints; i++) {
points.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
size: baseSize * (0.5 + Math.random() * 0.5) * densityFactor,
targetSize: baseSize * (0.5 + Math.random() * 0.5) * densityFactor,
velocity: {
x: (Math.random() - 0.5) * motionFactor,
y: (Math.random() - 0.5) * motionFactor
},
color: `hsl(${Math.random() * 180 + 150}, ${70 * (1 - dryness) + 30}%, ${50 * (1 - dryness) + 20}%)`,
pulse: Math.random() * 0.5 + pulseAvg,
life: lifespanFactor * (0.5 + Math.random() * 0.5)
});
velocities.push({
x: (Math.random() - 0.5) * 0.1,
y: (Math.random() - 0.5) * 0.1
});
sizes.push(points[i].size);
}
// Bounds checking
function checkBounds(p) {
const margin = 100;
if (p.x < margin) {
p.x = margin;
p.velocity.x *= -1;
} else if (p.x > canvas.width - margin) {
p.x = canvas.width - margin;
p.velocity.x *= -1;
}
if (p.y < margin) {
p.y = margin;
p.velocity.y *= -1;
} else if (p.y > canvas.height - margin) {
p.y = canvas.height - margin;
p.velocity.y *= -1;
}
}
// Draw frame
function draw(t) {
ctx.fillStyle = 'rgba(0, 0, 0, 0.05)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Update points
points.forEach(p => {
p.x += p.velocity.x * (1 + Math.sin(t * 0.001) * 0.3);
p.y += p.velocity.y * (1 + Math.cos(t * 0.001) * 0.3);
p.size = p.targetSize * (0.9 + 0.1 * Math.sin(t * 0.002 * p.pulse));
// Add some organic movement
p.velocity.x += (Math.random() - 0.5) * 0.02 * connectednessFactor;
p.velocity.y += (Math.random() - 0.5) * 0.02 * connectednessFactor;
checkBounds(p);
});
// Voronoi diagram
const imageData = ctx.createImageData(canvas.width, canvas.height);
const data = imageData.data;
for (let y = 0; y < canvas.height; y++) {
for (let x = 0; x < canvas.width; x++) {
let minDist = Infinity;
let closestPoint = null;
for (const p of points) {
const dx = x - p.x;
const dy = y - p.y;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist < minDist) {
minDist = dist;
closestPoint = p;
}
}
const idx = (y * canvas.width + x) * 4;
const intensity = 1 - Math.min(minDist / closestPoint.size, 1);
data[idx] = closestPoint.color.match(/\d+/g)[0];
data[idx + 1] = closestPoint.color.match(/\d+/g)[1];
data[idx + 2] = closestPoint.color.match(/\d+/g)[2];
data[idx + 3] = intensity * 255 * (1 - dryness) + 30;
}
}
ctx.putImageData(imageData, 0, 0);
requestAnimationFrame(draw);
}
draw();
</script>
</body>
</html>
```