You can edit the below JavaScript code to customize the image tool.
Apply Changes
function processImage(originalImg, gridSizeStr = "12", edgeSensitivityStr = "25", showLabelsStr = "1") {
const gridSize = parseInt(gridSizeStr) || 12;
const edgeSensitivity = parseInt(edgeSensitivityStr) || 25; // Scale 0-255
const showLabels = parseInt(showLabelsStr) !== 0;
const canvas = document.createElement('canvas');
const width = originalImg.width;
const height = originalImg.height;
// Maintain max bounds to prevent lag on massive images
const MAX_DIM = 2400;
let scale = 1;
if (width > MAX_DIM || height > MAX_DIM) {
scale = Math.min(MAX_DIM / width, MAX_DIM / height);
}
const scaledWidth = Math.floor(width * scale);
const scaledHeight = Math.floor(height * scale);
canvas.width = scaledWidth;
canvas.height = scaledHeight;
const ctx = canvas.getContext('2d');
// Draw original image
ctx.drawImage(originalImg, 0, 0, scaledWidth, scaledHeight);
// Get pixel data for computer vision processing
const imgData = ctx.getImageData(0, 0, scaledWidth, scaledHeight);
const data = imgData.data;
// 1. Convert to Grayscale
const gray = new Uint8Array(scaledWidth * scaledHeight);
for (let i = 0; i < data.length; i += 4) {
gray[i / 4] = 0.299 * data[i] + 0.587 * data[i + 1] + 0.114 * data[i + 2];
}
// 2. Sobel Edge Detection
const edges = new Uint8Array(scaledWidth * scaledHeight);
for (let y = 1; y < scaledHeight - 1; y++) {
for (let x = 1; x < scaledWidth - 1; x++) {
const idx = y * scaledWidth + x;
// Horizontal gradient
const gx =
-1 * gray[idx - scaledWidth - 1] + 1 * gray[idx - scaledWidth + 1] +
-2 * gray[idx - 1] + 2 * gray[idx + 1] +
-1 * gray[idx + scaledWidth - 1] + 1 * gray[idx + scaledWidth + 1];
// Vertical gradient
const gy =
-1 * gray[idx - scaledWidth - 1] - 2 * gray[idx - scaledWidth] - 1 * gray[idx - scaledWidth + 1] +
1 * gray[idx + scaledWidth - 1] + 2 * gray[idx + scaledWidth] + 1 * gray[idx + scaledWidth + 1];
edges[idx] = Math.min(255, Math.sqrt(gx * gx + gy * gy));
}
}
// 3. Grid-Based Density Map
const cols = Math.floor(scaledWidth / gridSize);
const rows = Math.floor(scaledHeight / gridSize);
const cellActive = new Uint8Array(cols * rows);
for (let y = 0; y < rows; y++) {
for (let x = 0; x < cols; x++) {
let edgePixels = 0;
for (let cy = 0; cy < gridSize; cy++) {
for (let cx = 0; cx < gridSize; cx++) {
const pxX = x * gridSize + cx;
const pxY = y * gridSize + cy;
if (edges[pxY * scaledWidth + pxX] > edgeSensitivity) {
edgePixels++;
}
}
}
// Mark cell as active if edges occupy > 4% of the cell
if (edgePixels > (gridSize * gridSize * 0.04)) {
cellActive[y * cols + x] = 1;
}
}
}
// 4. Horizontal Dilation (To merge text blocks and grouped nav items)
const dilatedActive = new Uint8Array(cols * rows);
for (let cy = 0; cy < rows; cy++) {
for (let cx = 0; cx < cols; cx++) {
if (cellActive[cy * cols + cx]) {
dilatedActive[cy * cols + cx] = 1;
if (cx > 0) dilatedActive[cy * cols + cx - 1] = 1;
if (cx < cols - 1) dilatedActive[cy * cols + cx + 1] = 1;
if (cy > 0) dilatedActive[(cy - 1) * cols + cx] = 1; // Slight vertical merge
if (cy < rows - 1) dilatedActive[(cy + 1) * cols + cx] = 1;
}
}
}
// 5. Connected Component Labeling (DFS) to find distinct UI Regions
const visited = new Uint8Array(cols * rows);
const regions = [];
for (let i = 0; i < cols * rows; i++) {
if (dilatedActive[i] && !visited[i]) {
const stack = [i];
visited[i] = 1;
let minX = i % cols, maxX = i % cols;
let minY = Math.floor(i / cols), maxY = Math.floor(i / cols);
let cellCount = 0;
while (stack.length > 0) {
const curr = stack.pop();
cellCount++;
const cx = curr % cols;
const cy = Math.floor(curr / cols);
if (cx < minX) minX = cx;
if (cx > maxX) maxX = cx;
if (cy < minY) minY = cy;
if (cy > maxY) maxY = cy;
// 8-way connectivity
const nList = [
[cx-1, cy-1], [cx, cy-1], [cx+1, cy-1],
[cx-1, cy], [cx+1, cy],
[cx-1, cy+1], [cx, cy+1], [cx+1, cy+1]
];
for (const [nx, ny] of nList) {
if (nx >= 0 && nx < cols && ny >= 0 && ny < rows) {
const nIdx = ny * cols + nx;
if (dilatedActive[nIdx] && !visited[nIdx]) {
visited[nIdx] = 1;
stack.push(nIdx);
}
}
}
}
regions.push({ minX, maxX, minY, maxY, cellCount });
}
}
// 6. Draw Bounding Boxes and Labels for identified interfaces
const overlayAlpha = 0.4;
ctx.fillStyle = `rgba(0, 0, 0, ${overlayAlpha})`;
ctx.fillRect(0, 0, scaledWidth, scaledHeight);
// Heuristics font setup
ctx.font = 'bold 13px system-ui, -apple-system, sans-serif';
ctx.textBaseline = 'top';
const hexToRgba = (hex, alpha) => {
const r = parseInt(hex.slice(1, 3), 16);
const g = parseInt(hex.slice(3, 5), 16);
const b = parseInt(hex.slice(5, 7), 16);
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
};
for (const r of regions) {
// Expand bounding bounds slightly for visual padding
const rx = Math.max(0, (r.minX * gridSize) - 4);
const ry = Math.max(0, (r.minY * gridSize) - 4);
const rw = Math.min(scaledWidth - rx, ((r.maxX - r.minX + 1) * gridSize) + 8);
const rh = Math.min(scaledHeight - ry, ((r.maxY - r.minY + 1) * gridSize) + 8);
// Filter out extreme artifacts
if (rw < 15 && rh < 15) continue;
if (rw >= scaledWidth * 0.95 && rh >= scaledHeight * 0.95) continue;
let label = "UI Component";
let color = "#f43f5e"; // rose
const aspectRatio = rw / rh;
const area = rw * rh;
const screenArea = scaledWidth * scaledHeight;
// Visual Heuristics Classification
if (rw > scaledWidth * 0.7 && rh < scaledHeight * 0.25 && ry < scaledHeight * 0.2) {
label = "Header / Navbar";
color = "#ef4444"; // red
} else if (rw > scaledWidth * 0.7 && rh < 120) {
label = "Row Container";
color = "#eab308"; // yellow
} else if (rh > scaledHeight * 0.5 && rw < scaledWidth * 0.35) {
label = "Sidebar / Drawer";
color = "#10b981"; // green
} else if (area > screenArea * 0.2) {
label = "Main Content / Media";
color = "#8b5cf6"; // purple
} else if (aspectRatio > 3.5 && rh < 60) {
label = "Text Ribbon / Input Field";
color = "#f97316"; // orange
} else if (aspectRatio >= 0.7 && aspectRatio <= 1.4 && rw < 70) {
label = "Icon / Profile Box";
color = "#ec4899"; // pink
} else if (rw >= 60 && rw <= 250 && rh >= 30 && rh <= 70) {
label = "Button / Tab";
color = "#0ea5e9"; // light blue
} else if (area > 8000) {
label = "Card / Image Block";
color = "#14b8a6"; // teal
}
// Draw Box outline
ctx.strokeStyle = color;
ctx.lineWidth = 2.5;
ctx.strokeRect(rx, ry, rw, rh);
// Box semi-transparent fill
ctx.fillStyle = hexToRgba(color, 0.15);
ctx.fillRect(rx, ry, rw, rh);
// Draw Label Tab
if (showLabels) {
const textWidth = ctx.measureText(label).width;
const tabHeight = 22;
const tabY = ry > tabHeight ? ry - tabHeight : ry + rh; // prevent clipping at the top
ctx.fillStyle = color;
ctx.fillRect(rx, tabY, textWidth + 16, tabHeight);
ctx.fillStyle = "#ffffff";
ctx.fillText(label, rx + 8, tabY + 4);
}
}
return canvas;
}
Apply Changes