birth: Swirling Particles in Spiral
This commit is contained in:
parent
644af8aced
commit
20f09593c8
1 changed files with 246 additions and 0 deletions
246
index.html
Normal file
246
index.html
Normal file
|
|
@ -0,0 +1,246 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Flow Field Organism</title>
|
||||
<style>
|
||||
body {
|
||||
margin: 0;
|
||||
overflow: hidden;
|
||||
background-color: #0a0a0a;
|
||||
font-family: 'Courier New', monospace;
|
||||
}
|
||||
canvas {
|
||||
display: block;
|
||||
}
|
||||
#attribution {
|
||||
position: fixed;
|
||||
bottom: 10px;
|
||||
left: 10px;
|
||||
color: rgba(255, 255, 255, 0.3);
|
||||
font-size: 10px;
|
||||
z-index: 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');
|
||||
|
||||
// Set canvas to full window size
|
||||
function resizeCanvas() {
|
||||
canvas.width = window.innerWidth;
|
||||
canvas.height = window.innerHeight;
|
||||
}
|
||||
window.addEventListener('resize', resizeCanvas);
|
||||
resizeCanvas();
|
||||
|
||||
// Flow field parameters
|
||||
const PARAMS = {
|
||||
motion: 0.5,
|
||||
density: 0.5,
|
||||
complexity: 0.5,
|
||||
connectedness: 0.5,
|
||||
lifespan: 0.5,
|
||||
pulse: 1.06,
|
||||
dryness: 0.90
|
||||
};
|
||||
|
||||
// Flow field implementation
|
||||
const FIELD_SIZE = 32;
|
||||
const field = new Array(FIELD_SIZE * FIELD_SIZE);
|
||||
let time = 0;
|
||||
|
||||
// Initialize flow field
|
||||
function initField() {
|
||||
for (let i = 0; i < field.length; i++) {
|
||||
field[i] = {
|
||||
vx: 0,
|
||||
vy: 0,
|
||||
strength: 0,
|
||||
angle: 0
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Update flow field
|
||||
function updateField() {
|
||||
const width = canvas.width;
|
||||
const height = canvas.height;
|
||||
const centerX = width / 2;
|
||||
const centerY = height / 2;
|
||||
const maxDist = Math.sqrt(centerX * centerX + centerY * centerY);
|
||||
|
||||
for (let y = 0; y < FIELD_SIZE; y++) {
|
||||
for (let x = 0; x < FIELD_SIZE; x++) {
|
||||
const idx = y * FIELD_SIZE + x;
|
||||
const posX = (x / FIELD_SIZE) * width;
|
||||
const posY = (y / FIELD_SIZE) * height;
|
||||
|
||||
// Calculate distance from center
|
||||
const dx = posX - centerX;
|
||||
const dy = posY - centerY;
|
||||
const dist = Math.sqrt(dx * dx + dy * dy);
|
||||
|
||||
// Create spiral pattern
|
||||
const angle = Math.atan2(dy, dx) + time * 0.01 * PARAMS.motion;
|
||||
const radius = dist / maxDist * 200;
|
||||
|
||||
field[idx].vx = Math.cos(angle) * radius * 0.01 * PARAMS.motion;
|
||||
field[idx].vy = Math.sin(angle) * radius * 0.01 * PARAMS.motion;
|
||||
field[idx].strength = Math.min(1, PARAMS.density * (1 - dist/maxDist));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Particle system
|
||||
const particles = [];
|
||||
const particleCount = Math.floor(100 + Math.random() * 200 * PARAMS.density);
|
||||
|
||||
function initParticles() {
|
||||
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,
|
||||
lifetime: 0,
|
||||
maxLifetime: 100 + Math.random() * 200 * (1 - PARAMS.lifespan),
|
||||
hue: 0,
|
||||
saturation: 0,
|
||||
brightness: 100 * PARAMS.dryness,
|
||||
trail: []
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function updateParticles() {
|
||||
particles.forEach(particle => {
|
||||
// Get flow field value at particle position
|
||||
const fieldX = Math.floor((particle.x / canvas.width) * FIELD_SIZE) % FIELD_SIZE;
|
||||
const fieldY = Math.floor((particle.y / canvas.height) * FIELD_SIZE) % FIELD_SIZE;
|
||||
const fieldIdx = fieldY * FIELD_SIZE + fieldX;
|
||||
|
||||
// Apply flow field forces
|
||||
particle.vx += field[fieldIdx].vx * PARAMS.motion * 0.5;
|
||||
particle.vy += field[fieldY].vy * PARAMS.motion * 0.5;
|
||||
|
||||
// Add some randomness based on complexity
|
||||
particle.vx += (Math.random() - 0.5) * 0.05 * PARAMS.complexity;
|
||||
particle.vy += (Math.random() - 0.5) * 0.05 * PARAMS.complexity;
|
||||
|
||||
// Update position
|
||||
particle.x += particle.vx;
|
||||
particle.y += particle.vy;
|
||||
|
||||
// Boundary check
|
||||
if (particle.x < 0 || particle.x > canvas.width) {
|
||||
particle.x = particle.x < 0 ? canvas.width : 0;
|
||||
particle.vx *= -0.8;
|
||||
}
|
||||
if (particle.y < 0 || particle.y > canvas.height) {
|
||||
particle.y = particle.y < 0 ? canvas.height : 0;
|
||||
particle.vy *= -0.8;
|
||||
}
|
||||
|
||||
// Update hue based on position
|
||||
particle.hue = (particle.x / canvas.width * 360) % 360;
|
||||
|
||||
// Update lifetime
|
||||
particle.lifetime++;
|
||||
if (particle.lifetime > particle.maxLifetime) {
|
||||
particle.lifetime = 0;
|
||||
particle.x = Math.random() * canvas.width;
|
||||
particle.y = Math.random() * canvas.height;
|
||||
particle.vx = 0;
|
||||
particle.vy = 0;
|
||||
}
|
||||
|
||||
// Add to trail
|
||||
particle.trail.push({x: particle.x, y: particle.y});
|
||||
if (particle.trail.length > 20) {
|
||||
particle.trail.shift();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function drawParticles() {
|
||||
ctx.strokeStyle = 'hsla(0, 0%, 100%, 0.1)';
|
||||
ctx.lineWidth = 1;
|
||||
|
||||
particles.forEach(particle => {
|
||||
// Draw trail
|
||||
ctx.beginPath();
|
||||
particle.trail.forEach((point, i) => {
|
||||
const alpha = (i / particle.trail.length) * 0.8;
|
||||
ctx.moveTo(point.x, point.y);
|
||||
ctx.lineTo(
|
||||
point.x + ((particle.x - point.x) * 0.3),
|
||||
point.y + ((particle.y - point.y) * 0.3)
|
||||
);
|
||||
});
|
||||
ctx.stroke();
|
||||
|
||||
// Draw particle
|
||||
ctx.fillStyle = `hsl(${particle.hue}, 80%, ${particle.brightness}%)`;
|
||||
ctx.beginPath();
|
||||
ctx.arc(particle.x, particle.y, particle.size, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
});
|
||||
}
|
||||
|
||||
function drawFlowField() {
|
||||
const cellWidth = canvas.width / FIELD_SIZE;
|
||||
const cellHeight = canvas.height / FIELD_SIZE;
|
||||
|
||||
for (let y = 0; y < FIELD_SIZE; y++) {
|
||||
for (let x = 0; x < FIELD_SIZE; x++) {
|
||||
const idx = y * FIELD_SIZE + x;
|
||||
const vx = field[idx].vx;
|
||||
const vy = field[idx].vy;
|
||||
|
||||
if (field[idx].strength > 0.1) {
|
||||
ctx.strokeStyle = `rgba(255, 255, 255, ${field[idx].strength * 0.3})`;
|
||||
ctx.lineWidth = 1;
|
||||
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(x * cellWidth, y * cellHeight);
|
||||
ctx.lineTo(
|
||||
x * cellWidth + vx * 20,
|
||||
y * cellHeight + vy * 20
|
||||
);
|
||||
ctx.stroke();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function animate() {
|
||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||
|
||||
// Update and draw flow field
|
||||
updateField();
|
||||
drawFlowField();
|
||||
|
||||
// Update and draw particles
|
||||
updateParticles();
|
||||
drawParticles();
|
||||
|
||||
// Update time
|
||||
time += 0.01 * PARAMS.motion;
|
||||
|
||||
requestAnimationFrame(animate);
|
||||
}
|
||||
|
||||
// Start animation
|
||||
initField();
|
||||
initParticles();
|
||||
animate();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Add table
Reference in a new issue