From b762aa1a283be0e0f69fb3b5e0882412f0cb743d Mon Sep 17 00:00:00 2001 From: Ben Taylor Date: Sat, 6 Jan 2024 08:37:45 +1100 Subject: [PATCH] Fix up starfield infinite loop (#286) --- packages/website/src/components/starfield.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/website/src/components/starfield.ts b/packages/website/src/components/starfield.ts index 0e2875ff..e26ca1d1 100644 --- a/packages/website/src/components/starfield.ts +++ b/packages/website/src/components/starfield.ts @@ -5,7 +5,8 @@ function seenPosition( const [x1, y1] = position; for (const [x2, y2] of seen) { const distance = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2)); - if (distance < 7) { + const distanceInPx = (distance / 100) * window.innerWidth; + if (distanceInPx < 20) { return true; } } @@ -56,7 +57,11 @@ export class StarfieldElement extends HTMLElement { Math.random() * 100, Math.random() * 100, ]; - while (avoidPosition(position) || seenPosition(position, seen)) { + let giveUpCount = 10; + while ( + giveUpCount-- > 0 && + (avoidPosition(position) || seenPosition(position, seen)) + ) { position = [Math.random() * 100, Math.random() * 100]; } clone.style.left = `${position[0]}%`;