Rewrite boundingBox
// js/helpers/polygon.js
{x: number, y: number, width: number, height: number} boundingBox(zone, margin = 0)
From an element's zone (with an image and a polygon), return a bounding rectangle of the zone, clamped to the image's dimensions.
This should crash if the zone is missing an image, to ensure we are always sending an image (and prevent going out of bounds).
> const image = {
url: 'http://lol/1234',
width: 500,
height: 700
}
> const zone = {
image,
polygon: [[100, 200], [100, 600], [400, 600], [400, 200], [100, 200]]
}
> boundingBox({ polygon })
Error: An image is required.
> boundingBox(zone)
{ x: 100, y: 200, width: 300, height: 400 }
> boundingBox(zone, 10)
{ x: 90, y: 190, width: 320, height: 420 }
> boundingBox(zone, 1000)
{ x: 0, y: 0, width: 500, height: 700 }