finer-grained text avoidance

Signed-off-by: Naman Sood <mail@nsood.in>
This commit is contained in:
Naman Sood 2024-12-04 19:34:25 -05:00
parent c4e5bddd09
commit 7ddeaad9b2
3 changed files with 15 additions and 6 deletions

View file

@ -25,8 +25,9 @@ function isDarkMode() {
(document.body.classList.length === 0 && matchMedia('(prefers-color-scheme: dark)').matches);
}
function contains(rect, x, y) {
return (rect.left <= x && x <= rect.right) && (rect.top <= y && y <= rect.bottom);
function contains(rect, x, y, buffer) {
return ((rect.left-buffer) <= x && x <= (rect.right+buffer)) &&
((rect.top-buffer) <= y && y <= (rect.bottom+buffer));
}
function Point() {
@ -42,12 +43,12 @@ function Point() {
// moves point to a random location and
// resets its progress
this.init = function() {
const textbox = document.querySelector('.text-container').getBoundingClientRect();
const textboxes = [...document.querySelector('.text-container').children].map(c => c.getBoundingClientRect());
this.progress = initialProgress;
do {
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
} while(contains(textbox, this.x, this.y));
} while(!textboxes.every(t => !contains(t, this.x, this.y, r)));
this.r = 0;
this.rng = Math.random();
}