birth: Flowing Particles in Currents

This commit is contained in:
motd_admin 2026-06-18 01:47:19 +00:00
parent 9b84577084
commit 28b613ea17

180
index.html Normal file
View file

@ -0,0 +1,180 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flow Field Canvas</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #0a0a0a;
font-family: 'Courier New', monospace;
}
canvas {
display: block;
}
#attribution {
position: absolute;
bottom: 10px;
left: 10px;
color: #666;
font-size: 10px;
pointer-events: none;
}
</style>
</head>
<body>
<canvas id="c"></canvas>
<div id="attribution">neurameba · motd.social</div>
<script>
const canvas = document.getElementById('c');
const c = canvas.getContext('2d');
function resize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
window.addEventListener('resize', resize);
resize();
// Flow field parameters
const motion = 0.5;
const density = 0.5;
const complexity = 0.5;
const connectedness = 0.5;
const lifespan = 0.5;
const pulseAvg = 1.08;
const pulseVariation = 0.05;
// Create flow field
const fieldSize = Math.min(50, Math.max(10, 30 - complexity * 20));
const cols = Math.floor(canvas.width / fieldSize) + 1;
const rows = Math.floor(canvas.height / fieldSize) + 1;
const field = [];
for (let y = 0; y < rows; y++) {
field[y] = [];
for (let x = 0; x < cols; x++) {
// Create a field where vectors point outward from center
const angle = Math.atan2(
y - rows/2,
x - cols/2
);
const length = 0.5 + Math.sin(Date.now() * 0.0005) * 0.5 * connectedness;
field[y][x] = {
angle: angle,
length: length,
x: x * fieldSize,
y: y * fieldSize
};
}
}
// Create particles
const particleCount = Math.floor(500 + density * 1500);
const particles = [];
const colors = ['#ddd', '#ccc', '#bbb', '#aaa'];
for (let i = 0; i < particleCount; i++) {
particles.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
vx: 0,
vy: 0,
size: 0.5 + Math.random() * 2,
color: colors[Math.floor(Math.random() * colors.length)],
life: 0,
maxLife: 100 + Math.random() * 100 * (1 - lifespan),
trail: []
});
}
function animate() {
c.fillStyle = 'rgba(0, 0, 0, 0.05)';
c.fillRect(0, 0, canvas.width, canvas.height);
const time = Date.now() * 0.001;
const pulse = pulseAvg + Math.sin(time * 2) * pulseVariation * connectedness;
// Update field
for (let y = 0; y < rows; y++) {
for (let x = 0; x < cols; x++) {
const angle = Math.atan2(
y - rows/2,
x - cols/2
) + Math.sin(time * 0.5) * 0.5 * motion;
field[y][x].angle = angle;
}
}
// Update particles
for (const p of particles) {
// Get field vector
const col = Math.floor(p.x / fieldSize);
const row = Math.floor(p.y / fieldSize);
const fieldCell = field[Math.min(row, rows-1)][Math.min(col, cols-1)];
// Apply field influence
p.vx += Math.cos(fieldCell.angle) * fieldCell.length * 0.05 * motion;
p.vy += Math.sin(fieldCell.angle) * fieldCell.length * 0.05 * motion;
// Add some randomness
p.vx += (Math.random() - 0.5) * 0.05 * (1 - complexity);
p.vy += (Math.random() - 0.5) * 0.05 * (1 - complexity);
// Update position
p.x += p.vx * pulse;
p.y += p.vy * pulse;
// Wrap around edges
if (p.x < 0) p.x = canvas.width;
if (p.x > canvas.width) p.x = 0;
if (p.y < 0) p.y = canvas.height;
if (p.y > canvas.height) p.y = 0;
// Update trail
p.trail.push({x: p.x, y: p.y});
if (p.trail.length > 100) {
p.trail.shift();
}
// Update life
p.life += 0.5;
if (p.life > p.maxLife) {
p.life = 0;
p.x = Math.random() * canvas.width;
p.y = Math.random() * canvas.height;
p.trail = [];
}
}
// Draw particles
for (const p of particles) {
// Draw trail
c.strokeStyle = p.color;
c.lineWidth = p.size * 0.5;
c.beginPath();
for (let i = 0; i < p.trail.length; i++) {
const pos = p.trail[i];
const nextPos = p.trail[i + 1];
if (nextPos) {
c.moveTo(pos.x, pos.y);
c.lineTo(nextPos.x, nextPos.y);
}
}
c.stroke();
// Draw particle
c.fillStyle = p.color;
c.beginPath();
c.arc(p.x, p.y, p.size, 0, Math.PI * 2);
c.fill();
}
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>