Since you need to add click events on individual rectangles, it'll be easier if you use div-containers instead of canvas to draw the boxes.
Set the image position as absolute (at top left position) and iterate through all the boxes to show the rectangle.
<div class="image-app-container" style="position: relative">
<img src="..." style="position: absolute; top: 0, left: 0">
// Iterate through the all the boxes, I'm using react JSX code here
{boxes.map(box => {
return <div
style={{"position": "absolute", "top": box.top, "left": box.left, "border": "2px solid darkred"}}
onClick= {this.selectBox} //selectBox function saves the clicked box info
/>
})
}
</div>
Once you've selected the rectangle, you can update/delete its size or position.
To add new boxes, add a click handler on the container-div
var c = document.querySelector('.image-app-container');
var handleClick = (e) => {
// Calculate position coordinates using e.clientX & e.clientY values
// Add the box with positions to your current list
}
c && c.addEventListener('click', handleClick)