<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>框选</title>
</head>
<style>
div {
width: 300px;
height: 300px;
object-fit: cover;
position: relative;
}
img {
position: absolute;
width: 300px;
height: 300px;
}
#canvas {
position: absolute;
height: 300px;
width: 300px;
}
</style>
<body>
<div>
<img src="./1.png" alt="">
<canvas id='canvas'></canvas>
</div>
</body>
<script type="text/javascript">
function draw(id) {
var canvas = document.getElementById(id);
var context = canvas.getContext('2d'); //getContext() 方法可返回一个对象
context.strokeStyle = "red"; //图形边框的填充颜色
context.lineWidth = 2; //用宽度为 5 像素的线条来绘制矩形:
context.strokeRect(40, 10, 120, 60); //绘制矩形(无填充)参数分别代表下x,y,长,宽
}
draw("canvas");
</script>
</html>
在图片上进行框选区域
最新推荐文章于 2024-07-23 09:17:31 发布