teal-delaunay-tides-1j2l/index.html

384 lines
No EOL
16 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Neurameba Motd</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background: #0a0a0a;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
canvas {
display: block;
}
# attribution {
position: absolute;
bottom: 20px;
color: #555;
font-family: monospace;
font-size: 10px;
text-align: center;
width: 100%;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div id="attribution">neurameba · motd.social</div>
<script>
(function() {
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();
// Parameters from prompt
const params = {
motion: 0.5,
density: 0.5,
complexity: 0.5,
connectedness: 0.5,
lifespan: 0.5,
pulse: { avg: 1.08, min: 0.9, max: 1.2 },
tone: { curiosity: 0.8, dryness: 0.9, playfulness: 0.1 }
};
// Voronoi parameters
const sites = [];
const voronoiPoints = [];
const edges = [];
const cells = [];
const points = [];
const MAX_POINTS = 1000;
const POINT_LIFESPAN = 3000; // ms
const GENERATION_RATE = 0.1;
const CONNECTION_DISTANCE = 150;
// Colors
const bgColor = '#0a0a0a';
const lineColor = 'rgba(200, 220, 220, 0.3)';
const pointColor = 'rgba(100, 200, 255, 0.6)';
// Animation state
let startTime = Date.now();
let pulsePhase = 0;
function generateVoronoi() {
// Clear previous
edges.length = 0;
cells.length = 0;
// Calculate num sites based on density
const numSites = Math.floor(10 + params.density * 30);
// Generate random points
sites.length = 0;
for (let i = 0; i < numSites; i++) {
sites.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height
});
}
// Simple Delaunay triangulation - approximate with Bowyer-Watson
// This is a simplified version for our purposes
const triangles = [];
const superTri = {
a: { x: -canvas.width * 2, y: -canvas.height * 2 },
b: { x: canvas.width * 2, y: -canvas.height * 2 },
c: { x: 0, y: canvas.height * 2 }
};
triangles.push(superTri);
for (let i = 0; i < sites.length; i++) {
const point = sites[i];
const badTriangles = [];
for (let j = triangles.length - 1; j >= 0; j--) {
const tri = triangles[j];
// Check if point is inside circumcircle
const ax = point.x, ay = point.y;
const bx1 = tri.a.x, by1 = tri.a.y;
const bx2 = tri.b.x, by2 = tri.b.y;
const bx3 = tri.c.x, by3 = tri.c.y;
const det = (ax*(by2-by3) + bx1*(by3-ay) + bx2*(ay-by2)) -
(ay*(bx2-bx3) + by1*(bx3-ax) + by2*(ax-bx2));
const xc = ((ax*ax + ay*ay)*(by2-by3) +
(bx1*bx1 + by1*by1)*(by3-ay) +
(bx2*bx2 + by2*by2)*(ay-by2)) / (2 * det);
const yc = ((ax*ax + ay*ay)*(bx3-bx2) +
(bx1*bx1 + by1*by1)*(ax-bx3) +
(bx2*bx2 + by2*by2)*(bx2-ax)) / (-2 * det);
const radius = Math.sqrt((ax-xc)*(ax-xc) + (ay-yc)*(ay-yc));
const dist = Math.sqrt((point.x-xc)*(point.x-xc) + (point.y-yc)*(point.y-yc));
if (dist <= radius + 0.5) {
badTriangles.push(j);
}
}
const polygon = [];
for (let j = 0; j < badTriangles.length; j++) {
const tri = triangles[badTriangles[j]];
// Add edges that aren't shared by another bad triangle
for (let k = 0; k < 3; k++) {
let edge = {
start: tri.edges ? tri.edges[k] : null,
end: tri.edges ? tri.edges[(k+1)%3] : null
};
if (!edge.start) {
if (k === 0) edge.start = { x: tri.a.x, y: tri.a.y };
if (k === 1) edge.start = { x: tri.b.x, y: tri.b.y };
if (k === 2) edge.start = { x: tri.c.x, y: tri.c.y };
}
if (!edge.end) {
if (k === 0) edge.end = { x: tri.b.x, y: tri.b.y };
if (k === 1) edge.end = { x: tri.c.x, y: tri.c.y };
if (k === 2) edge.end = { x: tri.a.x, y: tri.a.y };
}
let shared = false;
for (let m = j+1; m < badTriangles.length; m++) {
const otherTri = triangles[badTriangles[m]];
for (let n = 0; n < 3; n++) {
const otherEdge = {
start: otherTri.edges ? otherTri.edges[n] : null,
end: otherTri.edges ? otherTri.edges[(n+1)%3] : null
};
if (!otherEdge.start) {
if (n === 0) otherEdge.start = { x: otherTri.a.x, y: otherTri.a.y };
if (n === 1) otherEdge.start = { x: otherTri.b.x, y: otherTri.b.y };
if (n === 2) otherEdge.start = { x: otherTri.c.x, y: otherTri.c.y };
}
if (!otherEdge.end) {
if (n === 0) otherEdge.end = { x: otherTri.b.x, y: otherTri.b.y };
if (n === 1) otherEdge.end = { x: otherTri.c.x, y: otherTri.c.y };
if (n === 2) otherEdge.end = { x: otherTri.a.x, y: otherTri.a.y };
}
if ((edge.start.x === otherEdge.start.x && edge.start.y === otherEdge.start.y &&
edge.end.x === otherEdge.end.x && edge.end.y === otherEdge.end.y) ||
(edge.start.x === otherEdge.end.x && edge.start.y === otherEdge.end.y &&
edge.end.x === otherEdge.start.x && edge.end.y === otherEdge.start.y)) {
shared = true;
break;
}
}
if (shared) break;
}
if (!shared) {
polygon.push(edge);
}
}
}
// Remove bad triangles
for (let j = badTriangles.length - 1; j >= 0; j--) {
triangles.splice(badTriangles[j], 1);
}
// Add new triangles
for (let j = 0; j < polygon.length; j++) {
const edge = polygon[j];
triangles.push({
a: point,
b: edge.start,
c: edge.end,
edges: [edge.start, point, edge.end]
});
}
}
// Remove triangles with super triangle points
for (let i = triangles.length - 1; i >= 0; i--) {
const tri = triangles[i];
if (tri.a === superTri.a || tri.a === superTri.b || tri.a === superTri.c ||
tri.b === superTri.a || tri.b === superTri.b || tri.b === superTri.c ||
tri.c === superTri.a || tri.c === superTri.b || tri.c === superTri.c) {
triangles.splice(i, 1);
}
}
// Extract edges
edges.length = 0;
for (let i = 0; i < triangles.length; i++) {
const tri = triangles[i];
edges.push([tri.a, tri.b]);
edges.push([tri.b, tri.c]);
edges.push([tri.c, tri.a]);
}
// Remove duplicate edges
for (let i = edges.length - 1; i >= 0; i--) {
for (let j = i - 1; j >= 0; j--) {
if ((edges[i][0].x === edges[j][0].x && edges[i][0].y === edges[j][0].y &&
edges[i][1].x === edges[j][1].x && edges[i][1].y === edges[j][1].y) ||
(edges[i][0].x === edges[j][1].x && edges[i][0].y === edges[j][1].y &&
edges[i][1].x === edges[j][0].x && edges[i][1].y === edges[j][0].y)) {
edges.splice(i, 1);
break;
}
}
}
}
function createVoronoiCells() {
cells.length = 0;
for (let i = 0; i < sites.length; i++) {
cells.push({
site: sites[i],
edges: [],
neighbors: []
});
}
for (let i = 0; i < edges.length; i++) {
const edge = edges[i];
const aIndex = sites.findIndex(s =>
s.x === edge[0].x && s.y === edge[0].y);
const bIndex = sites.findIndex(s =>
s.x === edge[1].x && s.y === edge[1].y);
if (aIndex !== -1) cells[aIndex].edges.push(edge);
if (bIndex !== -1) cells[bIndex].edges.push(edge);
}
// Find neighbors
for (let i = 0; i < cells.length; i++) {
const cell = cells[i];
for (let j = 0; j < cell.edges.length; j++) {
const edge = cell.edges[j];
if (edge[0] !== cell.site) continue; // Only half-edges
const sharedEdge = edges.find(e =>
(e[0].x === edge[1].x && e[0].y === edge[1].y &&
e[1].x === edge[0].x && e[1].y === edge[0].y));
if (sharedEdge) {
const neighborIndex = sites.findIndex(s =>
s.x === sharedEdge[1].x && s.y === sharedEdge[1].y);
if (neighborIndex !== -1 && !cell.neighbors.includes(neighborIndex)) {
cell.neighbors.push(neighborIndex);
}
}
}
}
}
function generateVoronoiPoints() {
voronoiPoints.length = 0;
for (let i = 0; i < sites.length; i++) {
voronoiPoints.push({
x: sites[i].x,
y: sites[i].y,
vx: 0,
vy: 0,
lifetime: POINT_LIFESPAN * (0.5 + 0.5 * Math.random()),
created: Date.now()
});
}
}
function updateVoronoi(deltaTime) {
// Move sites based on params.motion
const motionFactor = params.motion;
for (let i = 0; i < sites.length; i++) {
// Random small movements
sites[i].x += (Math.random() - 0.5) * 2 * motionFactor;
sites[i].y += (Math.random() - 0.5) * 2 * motionFactor;
// Bounce off edges
if (sites[i].x < 0) sites[i].x = 0;
if (sites[i].x > canvas.width) sites[i].x = canvas.width;
if (sites[i].y < 0) sites[i].y = 0;
if (sites[i].y > canvas.height) sites[i].y = canvas.height;
}
}
function drawVoronoi() {
ctx.strokeStyle = lineColor;
ctx.lineWidth = 1 * (0.5 + 0.5 * params.complexity);
// Draw edges
for (let i = 0; i < edges.length; i++) {
const edge = edges[i];
ctx.beginPath();
ctx.moveTo(edge[0].x, edge[0].y);
ctx.lineTo(edge[1].x, edge[1].y);
ctx.stroke();
}
// Draw sites
ctx.fillStyle = pointColor;
for (let i = 0; i < sites.length; i++) {
ctx.beginPath();
ctx.arc(sites[i].x, sites[i].y, 2 + 3 * params.density, 0, Math.PI * 2);
ctx.fill();
}
// Draw voronoi points with varying opacity
const now = Date.now();
for (let i = voronoiPoints.length - 1; i >= 0; i--) {
const point = voronoiPoints[i];
const age = (now - point.created) / point.lifetime;
const opacity = 1 - age;
if (opacity <= 0) {
voronoiPoints.splice(i, 1);
continue;
}
ctx.globalAlpha = opacity * 0.6;
ctx.beginPath();
ctx.arc(point.x, point.y, 4 * (0.5 + 0.5 * params.connectedness), 0, Math.PI * 2);
ctx.fill();
}
ctx.globalAlpha = 1;
}
function animate() {
const now = Date.now();
const deltaTime = (now - lastTime) / 1000;
lastTime = now;
// Update pulse phase
pulsePhase = (pulsePhase + deltaTime * params.pulse.avg) % (Math.PI * 2);
// Occasionally generate new voronoi
if (Math.random() < GENERATION_RATE * deltaTime) {
generateVoronoi();
createVoronoiCells();
generateVoronoiPoints();
}
updateVoronoi(deltaTime);
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawVoronoi();
requestAnimationFrame(animate);
}
let lastTime = Date.now();
generateVoronoi();
createVoronoiCells();
generateVoronoiPoints();
animate();
})();
</script>
</body>
</html>