You're correct in identifying that using getBoundingClientRect() provides coordinates relative to the viewport (client coordinates), which can lead to inaccuracies when you intend to perform operations based on the entire page's coordinate system (page coordinates). To achieve accurate matching between rect and targetRect in page coordinates, you'll need to adjust how you calculate the positions of elements.
Here's a comprehensive solution to ensure that element positions are correctly computed relative to the entire page:
Overview of the Solution
-
Understanding Coordinate Systems:
- Client Coordinates: Relative to the viewport. Affected by scrolling.
- Page Coordinates: Relative to the entire document. Remain consistent regardless of scrolling.
-
Calculating Page-Relative Positions:
- Use
getBoundingClientRect() combined with window.pageXOffset and window.pageYOffset to convert client coordinates to page coordinates.
- Account for any margins or offsets introduced by parent containers.
-
Adjusting the Target Rectangle Definition:
- Ensure that the target rectangle is defined in page coordinates.
- When updating or generating the target rectangle, account for the entire page's scroll and margins.
-
Refactoring the JavaScript Code:
- Implement helper functions to accurately compute page-relative positions.
- Ensure all position calculations consider the entire page layout.
Step-by-Step Implementation
1. Remove or Adjust Body Margins
Having margins on the <body> can complicate coordinate calculations. For precise positioning, it's advisable to remove these margins or account for them in your calculations.
Modify the CSS:
body {
font-family: Arial, sans-serif;
margin: 0; /* Remove margins for simpler calculations */
position: relative;
}
If you need margins for aesthetic reasons, ensure that all position calculations account for these margins.
2. Implement a Helper Function to Get Page-Relative Coordinates
Create a helper function that accurately computes the page-relative position of any element. This function considers all parent elements and their offsets.
/**
* Calculates the page-relative coordinates of an element.
*
* @param {Element} element - The DOM element.
* @returns {Object} An object containing the x and y coordinates.
*/
function getPageCoordinates(element) {
const rect = element.getBoundingClientRect();
return {
x: rect.left + window.pageXOffset,
y: rect.top + window.pageYOffset,
width: rect.width,
height: rect.height
};
}
3. Update the findMostAncestralElements Function
Modify the findMostAncestralElements function to use the helper function for accurate coordinate calculations.
const findMostAncestralElements = (function () {
/**
* The optimized findMostAncestralElements function.
*
* @param {number} x - The X-coordinate of the target rectangle (relative to the page).
* @param {number} y - The Y-coordinate of the target rectangle (relative to the page).
* @param {number} width - The width of the target rectangle.
* @param {number} height - The height of the target rectangle.
* @param {number} errorRatio - The allowed error ratio (0 to 1) for matching.
* @param {Object} [options] - Optional settings.
* @param {boolean} [options.exhaustive=true] - If true, performs an exhaustive search.
* @param {number} [options.asyncIterations] - Number of iterations per asynchronous batch.
* If null, undefined, or <= 0, executes synchronously.
* @param {Function} [options.onProgress] - Callback function to receive progress updates.
* @returns {Promise<Element[]>|Element[]} A promise that resolves to an array of the most ancestral DOM elements matching the criteria, or the array directly if not asynchronous.
*/
return function findMostAncestralElements(
x,
y,
width,
height,
errorRatio,
options = {}
) {
const {
exhaustive = true,
asyncIterations, // Number of elements per async batch
onProgress,
} = options;
// Determine if the function should execute asynchronously
const isAsync = typeof asyncIterations === 'number' && asyncIterations > 0;
const iterationBatch = isAsync ? asyncIterations : null;
// Clamp errorRatio between 0 and 1
const z = Math.max(0, Math.min(errorRatio, 1));
const target = { x, y, width, height };
const results = new Set();
const queue = [document.body];
const head = { value: 0 }; // Using an object to allow mutation inside closure
// Helper function to check if an element matches the target criteria
function isMatch(rect, target, z) {
// Calculate the coordinates of the intersection rectangle
const xLeft = Math.max(rect.x, target.x);
const yTop = Math.max(rect.y, target.y);
const xRight = Math.min(rect.x + rect.width, target.x + target.width);
const yBottom = Math.min(rect.y + rect.height, target.y + target.height);
// Compute the width and height of the overlapping area
const overlapWidth = xRight - xLeft;
const overlapHeight = yBottom - yTop;
// If there is no overlap, the overlapping area is 0
if (overlapWidth <= 0 || overlapHeight <= 0) {
return false; // No part of rect is inside target
}
// Calculate areas
const overlapArea = overlapWidth * overlapHeight;
const rectArea = rect.width * rect.height;
// Determine if the overlapping area meets the required ratio
return overlapArea >= (1 - z) * rectArea;
}
// Helper function to check if an element overlaps with the target rectangle
function isOverlap(rect, target) {
return (
rect.x < target.x + target.width &&
rect.x + rect.width > target.x &&
rect.y < target.y + target.height &&
rect.y + rect.height > target.y
);
}
// Process a batch of elements asynchronously
function processBatchAsync() {
return new Promise((resolve) => {
let processed = 0;
while (head.value < queue.length && processed < iterationBatch) {
const elem = queue[head.value++];
const rect = getPageCoordinates(elem);
if (isMatch(rect, target, z)) {
results.add(elem);
console.log(elem);
} else if (exhaustive || isOverlap(rect, target)) {
const children = elem.children;
for (let i = 0, len = children.length; i < len; i++) {
queue.push(children[i]);
}
}
processed++;
}
if (onProgress) {
onProgress({
processedElements: head.value,
totalQueued: queue.length,
uniqueResults: results.size,
});
}
resolve();
});
}
// Handle asynchronous processing
if (isAsync) {
return new Promise((resolve) => {
function iterate() {
processBatchAsync().then(() => {
if (head.value < queue.length) {
setTimeout(iterate, 0);
} else {
resolve(Array.from(results));
}
});
}
iterate();
});
} else {
// Synchronous Processing
while (head.value < queue.length) {
const elem = queue[head.value++];
const rect = getPageCoordinates(elem);
if (isMatch(rect, target, z)) {
results.add(elem);
continue; // Skip enqueuing children to maintain ancestry
}
if (exhaustive || isOverlap(rect, target)) {
const children = elem.children;
for (let i = 0, len = children.length; i < len; i++) {
queue.push(children[i]);
}
}
if (onProgress) {
onProgress({
processedElements: head.value,
totalQueued: queue.length,
uniqueResults: results.size,
});
}
}
return Array.from(results);
}
};
})();
Key Changes:
-
Helper Function getPageCoordinates: This function calculates the absolute position of an element relative to the entire page by using getBoundingClientRect() and adding the current scroll offsets (window.pageXOffset and window.pageYOffset).
-
Simplified isMatch and isOverlap Functions:
- These functions now operate using the page-relative coordinates provided by
getPageCoordinates.
-
Removed marginLeft and marginTop:
- Since we've set the body margin to
0, we no longer need to account for body margins in our calculations.
4. Update Target Rectangle Functions
Ensure that all functions related to the target rectangle are aligned with page-relative coordinates.
Update the drawTargetRect Function:
/**
* Draws or updates the target rectangle based on page coordinates.
*
* @param {Object} targetRect - An object containing x, y, width, and height.
*/
function drawTargetRect(targetRect) {
const targetDiv = document.getElementById('target-rect');
// Set the position and size based on targetRect
targetDiv.style.left = `${targetRect.x}px`;
targetDiv.style.top = `${targetRect.y}px`;
targetDiv.style.width = `${targetRect.width}px`;
targetDiv.style.height = `${targetRect.height}px`;
// Display the rectangle
targetDiv.style.display = 'block';
}
Update the createRandomTargetRect Function:
/**
* Creates a random target rectangle within the page boundaries.
*
* @returns {Object} The generated target rectangle with x, y, width, and height.
*/
function createRandomTargetRect() {
const targetDiv = document.getElementById('target-rect');
// Define target rectangle dimensions
const width = 100; // px
const height = 100; // px
// Get page dimensions to ensure the rectangle stays within the page
const pageWidth = document.documentElement.scrollWidth;
const pageHeight = document.documentElement.scrollHeight;
const maxX = pageWidth - width; // Ensure within horizontal bounds
const maxY = pageHeight - height; // Ensure within vertical bounds
const x = Math.random() * maxX;
const y = Math.random() * maxY;
const targetRect = { x: Math.round(x), y: Math.round(y), width, height };
drawTargetRect(targetRect);
updateRectInputFields(targetRect); // Update input fields with new values
return targetRect;
}
Notes:
-
Body Margin Removed: With the body margin set to 0, we no longer need to adjust coordinates for body margins.
-
Page Dimensions: Using document.documentElement.scrollWidth and document.documentElement.scrollHeight ensures that the random target rectangle is placed within the full document dimensions.
5. Ensure Container Positioning is Consistent
The #elements-container is currently set to a fixed width and height with position: relative. To ensure that its child elements' absolute positioning aligns correctly with the page, verify the following:
Check or Update CSS for #elements-container:
#elements-container {
width: 1200px; /* Define width */
height: 2400px; /* Define height */
position: relative; /* To position nested elements absolutely within */
overflow: auto;
border: 1px solid #ccc;
padding: 10px;
/* Remove fixed positioning if not necessary */
}
Ensure that the container's positioning and dimensions correctly represent where elements are placed on the page.
6. Update Event Listener for the Search Button
Ensure that when initiating the search, the current target rectangle is accurately retrieved and used.
Update the Search Button Event Listener:
document.getElementById('search-btn').addEventListener('click', () => {
const searchBtn = document.getElementById('search-btn');
const resultsList = document.getElementById('results-list');
// Clear previous results
resultsList.innerHTML = '';
// Reset progress bar
const progressBar = document.getElementById('progress-bar');
const progressText = document.getElementById('progress-text');
progressBar.style.width = '0%';
progressText.textContent = '0%';
// Disable buttons during search
const generateBtn = document.getElementById('generate-btn');
const createTargetBtn = document.getElementById('create-target-btn');
const updateRectBtn = document.getElementById('update-rect-btn');
generateBtn.disabled = true;
createTargetBtn.disabled = true;
updateRectBtn.disabled = true;
searchBtn.disabled = true;
let targetRect = getCurrentTargetRect();
// If no target rect exists, create one
if (!targetRect) {
targetRect = createRandomTargetRect();
}
// Log the target rectangle
console.log('Using Target Rectangle:', targetRect);
// Start search
findMostAncestralElements(
targetRect.x,
targetRect.y,
targetRect.width,
targetRect.height,
0.5, // errorRatio
{
asyncIterations: 100, // Process 100 elements per batch
onProgress: (progress) => {
updateProgressBar(progress);
}
}
).then((matchingElements) => {
displayResults(matchingElements);
alert(`Search complete! Found ${matchingElements.length} matching elements.`);
generateBtn.disabled = false;
createTargetBtn.disabled = false;
updateRectBtn.disabled = false;
searchBtn.disabled = false;
}).catch((error) => {
console.error('Error during search:', error);
alert('An error occurred during the search.');
generateBtn.disabled = false;
createTargetBtn.disabled = false;
updateRectBtn.disabled = false;
searchBtn.disabled = false;
});
});
Ensure the getCurrentTargetRect Function is Accurate:
/**
* Retrieves the current target rectangle's page-relative coordinates.
*
* @returns {Object|null} The target rectangle or null if not defined.
*/
function getCurrentTargetRect() {
const targetDiv = document.getElementById('target-rect');
if (targetDiv.style.display === 'none') return null;
const rect = getPageCoordinates(targetDiv);
if (isNaN(rect.x) || isNaN(rect.y) || isNaN(rect.width) || isNaN(rect.height)) {
return null;
}
return { x: rect.x, y: rect.y, width: rect.width, height: rect.height };
}
7. Final Testing
After implementing the above changes, perform thorough testing to ensure:
-
Element Positioning: Generated elements are correctly positioned within the #elements-container and their page-relative coordinates are accurate.
-
Target Rectangle: The target rectangle is correctly positioned relative to the entire page, and updates correctly when modified.
-
Search Functionality: The search accurately identifies elements within the target rectangle based on the specified error ratio.
-
Progress Feedback: The progress bar accurately reflects the search progress.
Complete Revised Code
For convenience, here's the complete revised HTML document incorporating all the changes discussed:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Find Most Ancestral Elements with Progress Feedback</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0; /* Removed margins for simpler calculations */
position: relative;
}
/* Button Styles */
.buttons {
margin: 20px;
}
button {
padding: 10px 20px;
margin-right: 10px;
font-size: 16px;
cursor: pointer;
}
/* Progress Bar Styles */
#progress-container {
width: 80%;
background-color: #f3f3f3;
border: 1px solid #ccc;
border-radius: 5px;
overflow: hidden;
margin: 20px;
height: 25px;
}
#progress-bar {
width: 0%;
height: 100%;
background-color: #4caf50;
transition: width 0.2s ease;
}
#progress-text {
display: block;
text-align: right;
margin-top: -25px;
margin-bottom: 20px;
font-weight: bold;
margin-right: 10px;
}
/* Elements Container */
#elements-container {
width: 1200px; /* Define width */
height: 2400px; /* Define height */
display: block;
overflow: auto;
border: 1px solid #ccc;
padding: 10px;
position: relative; /* To position nested elements if needed */
margin: 20px; /* Added margin for better layout */
}
/* Generated Elements */
.generated-element {
width: 50px;
height: 50px;
background-color: #add8e6;
border: 1px solid #000;
box-sizing: border-box;
}
/* Results */
#results {
margin: 20px;
}
#results ul {
list-style-type: none;
padding-left: 0;
}
#results li {
padding: 5px 0;
}
/* Target Rectangle Styles */
#target-rect {
position: absolute;
border: 2px solid red;
pointer-events: none; /* Allows clicks to pass through the rectangle */
z-index: 1000; /* Ensure it's on top of other elements */
display: none; /* Hidden by default */
}
/* Input Sections */
.input-section {
margin: 20px;
}
.input-section label {
display: inline-block;
width: 120px;
font-weight: bold;
}
.input-section input {
padding: 5px;
width: 100px;
margin-right: 10px;
box-sizing: border-box;
}
.error-message {
color: red;
margin-top: 5px;
display: block;
}
</style>
</head>
<body>
<h1 style="margin: 20px;">Find Most Ancestral Elements with Progress Feedback</h1>
<!-- Generation Parameters Section -->
<div class="input-section">
<h2>Generation Parameters</h2>
<label for="generate-count">Number of Elements:</label>
<input type="number" id="generate-count" value="200" min="1">
<label for="generate-max-depth">Maximum Depth:</label>
<input type="number" id="generate-max-depth" value="2" min="0">
<button id="generate-btn">Generate Elements</button>
<span id="generate-errors" class="error-message"></span>
</div>
<!-- Target Rectangle Definition Section -->
<div class="input-section">
<h2>Define Target Rectangle</h2>
<label for="rect-x">X-coordinate:</label>
<input type="number" id="rect-x" value="0" min="0">
<label for="rect-y">Y-coordinate:</label>
<input type="number" id="rect-y" value="0" min="0">
<label for="rect-width">Width:</label>
<input type="number" id="rect-width" value="100" min="1">
<label for="rect-height">Height:</label>
<input type="number" id="rect-height" value="100" min="1">
<button id="update-rect-btn">Update Rect</button>
<button id="create-target-btn">Create Random Rect</button>
<span id="rect-errors" class="error-message"></span>
</div>
<!-- Search Button -->
<div class="buttons">
<button id="search-btn" disabled>Start Search</button>
</div>
<!-- Progress Bar -->
<div id="progress-container">
<div id="progress-bar"></div>
</div>
<span id="progress-text">0%</span>
<h2 style="margin: 20px;">Generated Elements</h2>
<div id="elements-container"></div>
<!-- Target Rectangle -->
<div id="target-rect"></div>
<h2 style="margin: 20px;">Search Results</h2>
<div id="results">
<ul id="results-list"></ul>
</div>
<script>
/**
* Calculates the page-relative coordinates of an element.
*
* @param {Element} element - The DOM element.
* @returns {Object} An object containing the x and y coordinates.
*/
function getPageCoordinates(element) {
const rect = element.getBoundingClientRect();
return {
x: rect.left + window.pageXOffset,
y: rect.top + window.pageYOffset,
width: rect.width,
height: rect.height
};
}
/**
* The optimized findMostAncestralElements function.
*
* @param {number} x - The X-coordinate of the target rectangle (relative to the page).
* @param {number} y - The Y-coordinate of the target rectangle (relative to the page).
* @param {number} width - The width of the target rectangle.
* @param {number} height - The height of the target rectangle.
* @param {number} errorRatio - The allowed error ratio (0 to 1) for matching.
* @param {Object} [options] - Optional settings.
* @param {boolean} [options.exhaustive=true] - If true, performs an exhaustive search.
* @param {number} [options.asyncIterations] - Number of iterations per asynchronous batch.
* If null, undefined, or <= 0, executes synchronously.
* @param {Function} [options.onProgress] - Callback function to receive progress updates.
* @returns {Promise<Element[]>|Element[]} A promise that resolves to an array of the most ancestral DOM elements matching the criteria, or the array directly if not asynchronous.
*/
const findMostAncestralElements = (function () {
return function findMostAncestralElements(
x,
y,
width,
height,
errorRatio,
options = {}
) {
const {
exhaustive = true,
asyncIterations, // Number of elements per async batch
onProgress,
} = options;
// Determine if the function should execute asynchronously
const isAsync = typeof asyncIterations === 'number' && asyncIterations > 0;
const iterationBatch = isAsync ? asyncIterations : null;
// Clamp errorRatio between 0 and 1
const z = Math.max(0, Math.min(errorRatio, 1));
const target = { x, y, width, height };
const results = new Set();
const queue = [document.body];
const head = { value: 0 }; // Using an object to allow mutation inside closure
// Helper function to check if an element matches the target criteria
function isMatch(rect, target, z) {
// Calculate the coordinates of the intersection rectangle
const xLeft = Math.max(rect.x, target.x);
const yTop = Math.max(rect.y, target.y);
const xRight = Math.min(rect.x + rect.width, target.x + target.width);
const yBottom = Math.min(rect.y + rect.height, target.y + target.height);
// Compute the width and height of the overlapping area
const overlapWidth = xRight - xLeft;
const overlapHeight = yBottom - yTop;
// If there is no overlap, the overlapping area is 0
if (overlapWidth <= 0 || overlapHeight <= 0) {
return false; // No part of rect is inside target
}
// Calculate areas
const overlapArea = overlapWidth * overlapHeight;
const rectArea = rect.width * rect.height;
// Determine if the overlapping area meets the required ratio
return overlapArea >= (1 - z) * rectArea;
}
// Helper function to check if an element overlaps with the target rectangle
function isOverlap(rect, target) {
return (
rect.x < target.x + target.width &&
rect.x + rect.width > target.x &&
rect.y < target.y + target.height &&
rect.y + rect.height > target.y
);
}
// Process a batch of elements asynchronously
function processBatchAsync() {
return new Promise((resolve) => {
let processed = 0;
while (head.value < queue.length && processed < iterationBatch) {
const elem = queue[head.value++];
const rect = getPageCoordinates(elem);
if (isMatch(rect, target, z)) {
results.add(elem);
console.log(elem);
} else if (exhaustive || isOverlap(rect, target)) {
const children = elem.children;
for (let i = 0, len = children.length; i < len; i++) {
queue.push(children[i]);
}
}
processed++;
}
if (onProgress) {
onProgress({
processedElements: head.value,
totalQueued: queue.length,
uniqueResults: results.size,
});
}
resolve();
});
}
// Handle asynchronous processing
if (isAsync) {
return new Promise((resolve) => {
function iterate() {
processBatchAsync().then(() => {
if (head.value < queue.length) {
setTimeout(iterate, 0);
} else {
resolve(Array.from(results));
}
});
}
iterate();
});
} else {
// Synchronous Processing
while (head.value < queue.length) {
const elem = queue[head.value++];
const rect = getPageCoordinates(elem);
if (isMatch(rect, target, z)) {
results.add(elem);
continue; // Skip enqueuing children to maintain ancestry
}
if (exhaustive || isOverlap(rect, target)) {
const children = elem.children;
for (let i = 0, len = children.length; i < len; i++) {
queue.push(children[i]);
}
}
if (onProgress) {
onProgress({
processedElements: head.value,
totalQueued: queue.length,
uniqueResults: results.size,
});
}
}
return Array.from(results);
}
};
})();
// Function to update the progress bar and percentage text
function updateProgressBar(progress) {
const { processedElements, totalQueued } = progress;
const progressBar = document.getElementById('progress-bar');
const progressText = document.getElementById('progress-text');
// Estimate percentage based on processed vs total queued
const percentage = totalQueued > 0 ? Math.min((processedElements / (processedElements + totalQueued)) * 100, 100) : 100;
progressBar.style.width = `${percentage}%`;
progressText.textContent = `${percentage.toFixed(2)}%`;
}
// Corrected Function to Generate Elements in a Grid Without Overlapping
function generateElements(count = 200, maxDepth = 2) {
const container = document.getElementById('elements-container');
container.innerHTML = ''; // Clear previous elements if any
const ELEMENT_SIZE = 50; // px
const GAP = 10; // px
// Get container dimensions
const containerRect = container.getBoundingClientRect();
const containerWidth = containerRect.width - GAP; // Adjust for padding and gaps
const containerHeight = containerRect.height - GAP;
const columns = Math.floor((containerWidth) / (ELEMENT_SIZE + GAP));
const rows = Math.floor((containerHeight) / (ELEMENT_SIZE + GAP));
let currentColumn = 0;
let currentRow = 0;
// Function to get position based on row and column
function getPosition(row, column) {
const left = column * (ELEMENT_SIZE + GAP);
const top = row * (ELEMENT_SIZE + GAP);
return { left, top };
}
// Function to create nested elements with controlled branching
function createNestedElements(parent, currentDepth) {
if (currentDepth > maxDepth) return;
// Define a fixed number of child elements per parent to prevent exponential growth
const CHILDREN_PER_PARENT = 2; // Fixed branching factor
for (let i = 0; i < CHILDREN_PER_PARENT; i++) {
const div = document.createElement('div');
div.classList.add('generated-element');
// Position children within the parent with a smaller gap
const childGap = 5; // px
const childLeft = (i % 2) * (ELEMENT_SIZE + childGap);
const childTop = Math.floor(i / 2) * (ELEMENT_SIZE + childGap);
// Apply inline styles for absolute positioning
div.style.position = 'absolute';
div.style.left = `${childLeft}px`;
div.style.top = `${childTop}px`;
parent.appendChild(div);
// Randomly decide to nest further with controlled depth
if (Math.random() > 0.5) { // 50% chance to nest for demonstration
createNestedElements(div, currentDepth + 1);
}
}
}
// Generate top-level elements in grid
for (let i = 0; i < count; i++) {
if (currentRow >= rows) {
console.warn('Reached maximum rows. Some elements may not be visible.');
break;
}
const div = document.createElement('div');
div.classList.add('generated-element');
const row = currentRow;
const column = currentColumn;
const { left, top } = getPosition(row, column);
// Apply inline styles for absolute positioning
div.style.position = 'absolute';
div.style.left = `${left}px`;
div.style.top = `${top}px`;
container.appendChild(div);
// Randomly decide to nest further with controlled branching
if (Math.random() > 0.7) { // 30% chance to nest
createNestedElements(div, 1);
}
// Update row and column
currentColumn++;
if (currentColumn >= columns) {
currentColumn = 0;
currentRow++;
}
}
}
// Function to display search results
function displayResults(elements) {
const resultsList = document.getElementById('results-list');
resultsList.innerHTML = ''; // Clear previous results
if (elements.length === 0) {
resultsList.innerHTML = '<li>No matching elements found.</li>';
return;
}
elements.forEach((elem, index) => {
const li = document.createElement('li');
li.textContent = `Element ${index + 1}: ${elem.tagName}${elem.id ? `#${elem.id}` : ''}${elem.className ? `.${elem.className}` : ''}`;
resultsList.appendChild(li);
});
window.results = Array.from(elements);
}
/**
* Draws or updates the target rectangle based on page coordinates.
*
* @param {Object} targetRect - An object containing x, y, width, and height.
*/
function drawTargetRect(targetRect) {
const targetDiv = document.getElementById('target-rect');
// Set the position and size based on targetRect
targetDiv.style.left = `${targetRect.x}px`;
targetDiv.style.top = `${targetRect.y}px`;
targetDiv.style.width = `${targetRect.width}px`;
targetDiv.style.height = `${targetRect.height}px`;
// Display the rectangle
targetDiv.style.display = 'block';
}
/**
* Creates a random target rectangle within the page boundaries.
*
* @returns {Object} The generated target rectangle with x, y, width, and height.
*/
function createRandomTargetRect() {
const targetDiv = document.getElementById('target-rect');
// Define target rectangle dimensions
const width = 100; // px
const height = 100; // px
// Get page dimensions to ensure the rectangle stays within the page
const pageWidth = document.documentElement.scrollWidth;
const pageHeight = document.documentElement.scrollHeight;
const maxX = pageWidth - width; // Ensure within horizontal bounds
const maxY = pageHeight - height; // Ensure within vertical bounds
const x = Math.random() * maxX;
const y = Math.random() * maxY;
const targetRect = { x: Math.round(x), y: Math.round(y), width, height };
drawTargetRect(targetRect);
updateRectInputFields(targetRect); // Update input fields with new values
return targetRect;
}
/**
* Updates the rectangle input fields with given values.
*
* @param {Object} rect - The target rectangle with x, y, width, and height.
*/
function updateRectInputFields(rect) {
document.getElementById('rect-x').value = rect.x;
document.getElementById('rect-y').value = rect.y;
document.getElementById('rect-width').value = rect.width;
document.getElementById('rect-height').value = rect.height;
}
// Event listeners for buttons
document.getElementById('generate-btn').addEventListener('click', () => {
const generateBtn = document.getElementById('generate-btn');
const createTargetBtn = document.getElementById('create-target-btn');
const searchBtn = document.getElementById('search-btn');
const progressBar = document.getElementById('progress-bar');
const progressText = document.getElementById('progress-text');
const resultsList = document.getElementById('results-list');
const generateErrors = document.getElementById('generate-errors');
// Clear previous error messages
generateErrors.textContent = '';
// Get input values
const countInput = document.getElementById('generate-count').value;
const maxDepthInput = document.getElementById('generate-max-depth').value;
let hasError = false;
let count = parseInt(countInput, 10);
let maxDepth = parseInt(maxDepthInput, 10);
let errorMessage = '';
// Validate count
if (isNaN(count) || count <= 0) {
errorMessage += 'Number of Elements must be a positive integer. ';
hasError = true;
}
// Validate maxDepth
if (isNaN(maxDepth) || maxDepth < 0) {
errorMessage += 'Maximum Depth must be a non-negative integer.';
hasError = true;
}
if (hasError) {
generateErrors.textContent = errorMessage;
return;
}
// Disable buttons during generation
generateBtn.disabled = true;
createTargetBtn.disabled = true;
searchBtn.disabled = true;
resultsList.innerHTML = '';
progressBar.style.width = '0%';
progressText.textContent = '0%';
// Generate elements
setTimeout(() => { // Use setTimeout to allow UI to update
generateElements(count, maxDepth); // Generate elements with user-specified count and maxDepth
generateBtn.disabled = false;
createTargetBtn.disabled = false;
searchBtn.disabled = false;
}, 100); // Slight delay to ensure buttons get disabled first
});
document.getElementById('create-target-btn').addEventListener('click', () => {
const targetRect = createRandomTargetRect();
// Optionally, you can notify the user
// alert('Random Target Rectangle Created!');
});
document.getElementById('update-rect-btn').addEventListener('click', () => {
const rectErrors = document.getElementById('rect-errors');
rectErrors.textContent = ''; // Clear previous error messages
// Get input values
const xInput = document.getElementById('rect-x').value;
const yInput = document.getElementById('rect-y').value;
const widthInput = document.getElementById('rect-width').value;
const heightInput = document.getElementById('rect-height').value;
let hasError = false;
let x = parseFloat(xInput);
let y = parseFloat(yInput);
let width = parseFloat(widthInput);
let height = parseFloat(heightInput);
let errorMessage = '';
// Validate x
if (isNaN(x) || x < 0) {
errorMessage += 'X-coordinate must be a non-negative number. ';
hasError = true;
}
// Validate y
if (isNaN(y) || y < 0) {
errorMessage += 'Y-coordinate must be a non-negative number. ';
hasError = true;
}
// Validate width
if (isNaN(width) || width <= 0) {
errorMessage += 'Width must be a positive number. ';
hasError = true;
}
// Validate height
if (isNaN(height) || height <= 0) {
errorMessage += 'Height must be a positive number.';
hasError = true;
}
if (hasError) {
rectErrors.textContent = errorMessage;
return;
}
const targetRect = { x, y, width, height };
drawTargetRect(targetRect);
console.log('User Defined Target Rectangle:', targetRect);
// Optionally, notify the user
});
document.getElementById('search-btn').addEventListener('click', () => {
const searchBtn = document.getElementById('search-btn');
const resultsList = document.getElementById('results-list');
// Clear previous results
resultsList.innerHTML = '';
// Reset progress bar
const progressBar = document.getElementById('progress-bar');
const progressText = document.getElementById('progress-text');
progressBar.style.width = '0%';
progressText.textContent = '0%';
// Disable buttons during search
const generateBtn = document.getElementById('generate-btn');
const createTargetBtn = document.getElementById('create-target-btn');
const updateRectBtn = document.getElementById('update-rect-btn');
generateBtn.disabled = true;
createTargetBtn.disabled = true;
updateRectBtn.disabled = true;
searchBtn.disabled = true;
let targetRect = getCurrentTargetRect();
// If no target rect exists, create one
if (!targetRect) {
targetRect = createRandomTargetRect();
}
// Log the target rectangle
console.log('Using Target Rectangle:', targetRect);
// Start search
findMostAncestralElements(
targetRect.x,
targetRect.y,
targetRect.width,
targetRect.height,
0.5, // errorRatio
{
asyncIterations: 100, // Process 100 elements per batch
onProgress: (progress) => {
updateProgressBar(progress);
}
}
).then((matchingElements) => {
displayResults(matchingElements);
alert(`Search complete! Found ${matchingElements.length} matching elements.`);
generateBtn.disabled = false;
createTargetBtn.disabled = false;
updateRectBtn.disabled = false;
searchBtn.disabled = false;
}).catch((error) => {
console.error('Error during search:', error);
alert('An error occurred during the search.');
generateBtn.disabled = false;
createTargetBtn.disabled = false;
updateRectBtn.disabled = false;
searchBtn.disabled = false;
});
});
/**
* Retrieves the current target rectangle's page-relative coordinates.
*
* @returns {Object|null} The target rectangle or null if not defined.
*/
function getCurrentTargetRect() {
const targetDiv = document.getElementById('target-rect');
if (targetDiv.style.display === 'none') return null;
const rect = getPageCoordinates(targetDiv);
if (isNaN(rect.x) || isNaN(rect.y) || isNaN(rect.width) || isNaN(rect.height)) {
return null;
}
return { x: rect.x, y: rect.y, width: rect.width, height: rect.height };
}
</script>
</body>
</html>
Explanation of Changes
-
Body Margin Removed: The CSS for the <body> now sets margin: 0; to eliminate any unexpected offsets in coordinate calculations. This simplifies the process of mapping element positions to page coordinates.
-
Helper Function getPageCoordinates: This function consolidates the logic for converting an element's getBoundingClientRect() output to page-relative coordinates by adding the current scroll positions. It ensures consistency across all position calculations.
-
Adjusted findMostAncestralElements: The function now relies on getPageCoordinates for accurate position data. It eliminates the need to account for body margins separately, reducing potential errors in the matching logic.
-
Target Rectangle Handling: Both the creation of a random target rectangle and updating via user inputs now utilize the getPageCoordinates function to maintain consistency. The drawTargetRect function positions the target rectangle based on page coordinates, ensuring it overlays correctly on the elements.
-
Event Listeners and Validation: Input validations remain intact, ensuring that users provide valid data for element generation and target rectangle dimensions.
-
Progress Feedback: The progress bar and percentage text provide real-time feedback during the search operation, enhancing user experience.
Additional Recommendations
-
Responsive Design: Consider making the #elements-container responsive to different screen sizes to enhance usability across devices.
-
Enhanced Visualization: Visual feedback highlighting matched elements on the page can provide a clearer understanding of the search results.
-
Performance Optimization: For very large numbers of elements, further optimizations like requestIdleCallback or Web Workers can be implemented to prevent UI blocking.
-
Accessibility Considerations: Ensure that all interactive elements are accessible via keyboard and screen readers for inclusivity.
By implementing the above changes, your application will accurately match elements based on page-relative coordinates, providing reliable search results within the defined target rectangle.