# javascript – Draw boxes around shapes on canvas

You can do this naively with a simple flood fill-style algorithm that keeps track of the max/min points of each black area in all 4 directions.

The algorithm may visit every pixel twice and uses linear space. It also uses recursion, so it’ll blow the stack if your black blobs are too large. Refactoring to use an explicit stack would avoid that. In other words, it’s not optimized at all, just a proof of concept.

Here’s the basic algorithm without a canvas. Note that it gives the points only within grid bounds, which would technically put the box on the edges of the shape, but you can add/subtract 1 (or another padding amount) to each corner to move the bounding box outside of the shape as desired.

``````const findBoundingBoxes = grid => {
const flood = (i, j, best) => {
if (i < 0 || j < 0 ||
i >= grid.length || j >= grid[i].length ||
!grid[i][j] || visited[i][j]) {
return;
}

visited[i][j] = true;
best.top = Math.min(best.top, i);
best.bottom = Math.max(best.bottom, i);
best.left = Math.min(best.left, j);
best.right = Math.max(best.right, j);

for (let di = -1; di < 2; di++) {
for (let dj = -1; dj < 2; dj++) {
if (di !== 0 || dj !== 0) {
flood(i + di, j + dj, best);
}
}
}
};

const boxes = [];
const visited = [...Array(grid.length)]
.map((_, i) => [...Array(grid[i].length)].fill(false));

for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
if (!grid[i][j] || visited[i][j]) {
continue;
}

const best = {top: i, bottom: i, left: j, right: j};
flood(i, j, best);
boxes.push({
topLeft: {x: best.left, y: best.top},
topRight: {x: best.right, y: best.top},
bottomLeft: {x: best.left, y: best.bottom},
bottomRight: {x: best.right, y: best.bottom},
});
}
}

return boxes;
};

const grid = [
"0000000000000",
"0001100001000",
"0011110111110",
"0001100111100",
"0000000000000",
].map(e => e.split("").map(Number));
console.log(findBoundingBoxes(grid));``````

Now, on a canvas. I’m converting the image data to a 2d grid for ease of coding, which isn’t performance-conscious.

``````const findBoundingBoxes = grid => {
const flood = (i, j, best) => {
if (i < 0 || j < 0 ||
i >= grid.length || j >= grid[i].length ||
!grid[i][j] || visited[i][j]) {
return;
}

visited[i][j] = true;
best.top = Math.min(best.top, i);
best.bottom = Math.max(best.bottom, i);
best.left = Math.min(best.left, j);
best.right = Math.max(best.right, j);

for (let di = -1; di < 2; di++) {
for (let dj = -1; dj < 2; dj++) {
if (di !== 0 || dj !== 0) {
flood(i + di, j + dj, best);
}
}
}
};

const boxes = [];
const visited = [...Array(grid.length)]
.map((_, i) => [...Array(grid[i].length)].fill(false));

for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
if (!grid[i][j] || visited[i][j]) {
continue;
}

const best = {top: i, bottom: i, left: j, right: j};
flood(i, j, best);
boxes.push({
topLeft: {x: best.left, y: best.top},
topRight: {x: best.right, y: best.top},
bottomRight: {x: best.right, y: best.bottom},
bottomLeft: {x: best.left, y: best.bottom},
});
}
}

return boxes;
};

const canvas = document.createElement("canvas");
canvas.style.border = "1px solid blue";
document.body.appendChild(canvas);
const ctx = canvas.getContext("2d");
const img = new Image();
img.onload = function() {
const {width: w, height: h} = this;
canvas.width = w;
canvas.height = h;
ctx.drawImage(this, 0, 0, w, h);
const {data} = ctx.getImageData(0, 0, w, h);
const grid = [...Array(h)].map((_, i) =>
[...Array(w)].map((_, j) => data[(i*w+j)*4] < 255 ? 1 : 0)
);
const pad = 1;
ctx.strokeStyle = "red";

for (const box of findBoundingBoxes(grid)) {
const w = box.topRight.x - box.topLeft.x;
const h = box.bottomLeft.y - box.topLeft.y;
ctx.strokeRect(
box.topLeft.x - pad + 0.5,
box.topLeft.y - pad + 0.5,
w + pad * 2, h + pad * 2
);
}
};