Canvas JS如何获取多边形的高度和宽度?

Canvas JS How to get height and width of polygon shape?

我创建了一个小的 canvas js 脚本并在形状上绘制轮廓这里是脚本

context.beginPath();
context.moveTo(coordinates[0].x, coordinates[0].y);
for (let index = 1; index < coordinates.length; index++) {
    context.lineTo(coordinates[index].x, coordinates[index].y);
}
context.lineWidth = 2;
context.strokeStyle = "red";
context.fillStyle = "rgba(255, 0, 0, 0.3)";
context.fill()
context.closePath();
context.stroke();

但我无法获取其形状的宽度和高度,实际上我想显示和显示为工具提示以供用户参考。

我在这里附上图片,你可以看到骰子顶部的黑线(我在 photoshop 中做了,供你参考)。我想显示那条线。 我是菜鸟,如果有人可以帮助我提供示例代码那就太好了。

Example image

您可以这样得到 min/max X 和 Y:

var minx = coordinates[0].x, maxx = coordinates[0].x, miny = = coordinates[0].y, maxy = = coordinates[0].y;
for (let index = 1; index < coordinates.length; index++) {
  if (coordinates[index].x < minx) minx = coordinates[index.x]);
  if (coordinates[index].x > maxx) maxx = coordinates[index.x]);
  if (coordinates[index].y < miny) miny = coordinates[index.y]);
  if (coordinates[index].y > maxy) maxy = coordinates[index.y]);
}

您可以使用最小值和最大值计算高度和宽度。