added idle animations

This commit is contained in:
Ludwig Lehnert 2025-04-14 21:34:45 +02:00
parent a0bf73dae6
commit 75acef5630

View File

@ -50,22 +50,42 @@
return others; return others;
}); });
document.getElementById("stars")?.append(...stars); const lineInterval = setInterval(() => {
if (Math.random() < 0.7) return;
function centeredPoint( while (true) {
[x, y]: [number, number], const lidx = chooseRandom(indices);
width: number, const ridx = chooseRandom(indices);
): [number, number] { if (lidx === ridx) continue;
return [x + width / 200, y + width / 120];
const [lx, ly] = points[lidx];
const [rx, ry] = points[ridx];
const d = Math.sqrt(
Math.pow(lx - rx, 2) + Math.pow(ly - ry, 2),
);
if (d > 0.4) continue;
const line = document.getElementById(`${lidx}-${ridx}`);
if (!line) continue;
line.setAttribute("opacity", "1");
setTimeout(
() => line.setAttribute("opacity", "0"),
Math.random() * 4500,
);
break;
} }
}, 100);
document.getElementById("stars")?.append(...stars);
const linesSVG = document.getElementById("stars-lines"); const linesSVG = document.getElementById("stars-lines");
stars.forEach((_, i) => { stars.forEach((_, i) => {
const [x1, y1] = centeredPoint(points[i], sizes[i]); const [x1, y1] = points[i];
connections[i].forEach((conn) => { connections[i].forEach((conn) => {
const [x2, y2] = centeredPoint(points[conn], sizes[conn]); const [x2, y2] = points[conn];
const line = document.createElementNS( const line = document.createElementNS(
"http://www.w3.org/2000/svg", "http://www.w3.org/2000/svg",
"line", "line",
@ -106,17 +126,20 @@
star.addEventListener("mouseleave", leaveListeners[i]); star.addEventListener("mouseleave", leaveListeners[i]);
}); });
const svgResizeListener = () => const svgResizeListener = () => {
linesSVG?.setAttribute( linesSVG?.setAttribute(
"viewBox", "viewBox",
`0 0 ${window.innerWidth} ${window.innerHeight}`, `0 0 ${window.innerWidth} ${window.innerHeight}`,
); );
};
svgResizeListener(); svgResizeListener();
window.addEventListener("resize", svgResizeListener); window.addEventListener("resize", svgResizeListener);
return () => { return () => {
clearInterval(lineInterval);
stars.forEach((star, i) => { stars.forEach((star, i) => {
star.removeEventListener("mouseenter", enterListeners[i]); star.removeEventListener("mouseenter", enterListeners[i]);
star.removeEventListener("mouseleave", leaveListeners[i]); star.removeEventListener("mouseleave", leaveListeners[i]);
@ -130,7 +153,7 @@
<svg <svg
id="stars-lines" id="stars-lines"
aria-hidden="true" aria-hidden="true"
style="position: fixed; left: 0; top: 0; height: 100vh; width: 100vw; z-index: 0;" style="position: fixed; left: 0; top: 0; height: 100%; width: 100%; z-index: 0;"
></svg> ></svg>
<div id="stars" style="position: fixed; left: 0; top: 0; z-index: 2;"></div> <div id="stars" style="position: fixed; left: 0; top: 0; z-index: 2;"></div>
@ -192,6 +215,8 @@
:global(.star) { :global(.star) {
animation: fadeIn 1s forwards; animation: fadeIn 1s forwards;
transition-duration: 500ms;
translate: -50% -50%;
} }
:global(#stars-lines line) { :global(#stars-lines line) {