如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<img id="myimg" src="./yidongbangong20240124103725.png"></img>
<canvas id="mycanvas"></canvas>
<script>
const resData = {
data: {
cont: [{
pos: [10, 20, 33, 40],
cont: "数据标注"
}]
}
}
const img = document.getElementById("myimg");
const canvas = document.getElementById("mycanvas");
// 调整画布大小
const ctx = canvas.getContext("2d");
canvas.width = img.width;
canvas.height = img.height;
console.log(1);
// 根据数据循环生成标注框
ctx.strokeStyle = "red";
// 画布上显示图片
ctx.drawImage(img, 0, 0, img.width, img.height);
// 计算缩小比率
const widthrat = (img.naturalWidth - img.width) / img.width;
const heightrat = (img.naturalHeight - img.height) / img.height;
resData.data.cont.forEach(value => {
// 获取标注框的x、y坐标
const x = value.pos[0] * widthrat;
const y = value.pos[1] * heightrat;
// 获取标注框的宽、高
const wid = (value.pos[2] - value.pos[0]) * widthrat;
const hei = (value.pos[3] - value.pos[1]) * heightrat;
// 根据获取的数据绘制描边矩形
ctx.rect(x, y, wid, hei);
// 绘制描边矩形上的红色填充矩形,并稍微调整样式
ctx.fillStyle = "red";
ctx.fillRect(x, y - 20, ctx.measureText(value.cont).width + 20, 20);
// 绘制填充矩形上的白色文字,并调整坐标
ctx.fillStyle = '#fff';
ctx.font = "16px Arial";
ctx.fillText(value.cont, x + 10, y - 4);
})
ctx.stroke()
</script>
</body>
</html>