Skip to content

Commit

Permalink
Highlighted the category div with a yellow background when an overlap…
Browse files Browse the repository at this point in the history
… is detected.
  • Loading branch information
Mahimahto committed Nov 28, 2024
1 parent dc22409 commit 97c847e
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions uli-word-cloud/js/script3.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
const dragingDiv = document.querySelector("#dragingDiv");
const categoryDivs = document.querySelectorAll(".category");

let isDragging = false;

dragingDiv.addEventListener("mousedown", (e) => {
isDragging = true;
});

let width = parseInt(dragingDiv.style.width)
let height = parseInt(dragingDiv.style.height)
// console.log(height/2);
// console.log(width/2)
let width = parseInt(dragingDiv.style.width);
let height = parseInt(dragingDiv.style.height);

document.addEventListener("mousemove", (e) => {
if (isDragging) {
dragingDiv.style.position = "absolute";
const x = e.clientX;
const y = e.clientY;

dragingDiv.style.left = `${x - width / 2}px`;
dragingDiv.style.top = `${y - width / 2}px`;
dragingDiv.style.top = `${y - height / 2}px`;

categoryDivs.forEach(categoryDiv => {
const categoryRect = categoryDiv.getBoundingClientRect();
const draggingRect = dragingDiv.getBoundingClientRect();

This comment has been minimized.

Copy link
@dennyabrain

dennyabrain Nov 28, 2024

@Mahimahto no need to put const draggingRect = dragingDiv.getBoundingClientRect(); in a loop. We can do it once outside the loop.

const isOverlap = !(
draggingRect.right < categoryRect.left ||
draggingRect.left > categoryRect.right ||
draggingRect.bottom < categoryRect.top ||
draggingRect.top > categoryRect.bottom
);

if (isOverlap) {
console.log("Overlap detected with categoryDiv:", categoryDiv);
categoryDiv.style.backgroundColor = "yellow";
} else {
categoryDiv.style.backgroundColor = "";
}
});
}
});

Expand Down

0 comments on commit 97c847e

Please sign in to comment.