Skip to content

Commit

Permalink
Fix up starfield infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
taybenlor committed Jan 5, 2024
1 parent 473c544 commit 4ffec17
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/website/src/components/starfield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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]}%`;
Expand Down

0 comments on commit 4ffec17

Please sign in to comment.