当前位置:网站首页>canvas给图片画框框
canvas给图片画框框
2022-06-22 18:05:00 【凹凸曼与程序猿】
<template>
<div class="item-img">
<canvas id="myCanvas" width="1280" height="650">
Your browser does not support the HTML5 canvas tag.
</canvas>
</div>
</template>
<script>
export default {
data() {
return {
};
},
//src为图片路径,gtcoord为gt标注数据:[[x,x,x,x],[x,x,x,x]......],dt同理
props: ["src", "gtcoord", "dtcoord"],
mounted() {
this.imgURL = this.src;
this.queryCanvasInfo();
},
methods: {
//获取canvasdom节点
queryCanvasInfo() {
(this.c = document.getElementById("myCanvas")),
(this.ctx = this.c.getContext("2d"));
this.img = new Image();
this.img.src = this.imgURL;
this.createCanvasImg();
},
//创建图片画布
createCanvasImg() {
// this.imgX = this.c.width / 2; //拖拽的X轴值
// this.imgY = this.c.height / 2; //拖拽的Y轴值
let img = this.img;
img.src = this.imgURL; //更换图片
img.onload = () => {
//图片加载时
this.ctx.clearRect(0, 0, this.c.width, this.c.height); //清空这个范围的画布
let a = img.width / img.height; //计算原始图片的宽高比例
console.log(img.width, img.height, 999);
if (img.width > img.height) {
//当图片宽度大于高度则写死宽度按高度调整比例
this.maxW = this.c.width; //最大宽度要等于画布的宽度
this.maxH = this.maxW / a;
} else {
//相反
(this.maxH = this.c.height), (this.maxW = this.maxH * a);
}
this.times = this.img.width / this.maxW; //计算原图与canvas的比例
this.canvasInfo(); //重新绘制图片
};
},
//绘制图片
canvasInfo() {
let w = this.maxW, //计算缩放的宽度
h = this.maxH; //计算缩放的高度
this.ctx.drawImage(
this.img, //图片实例
0,
0, //x轴y轴的边距
w,
h //图片的宽高
);
this.canvasRect( this.dtcoord,"dt");
this.canvasRect( this.gtcoord,"gt");
},
//标注框绘制
canvasRect(list,txt) {
list.forEach((item) => {
if (!item) return;
let ary = item,
x = Number(ary[0]) / this.times,
y = Number(ary[1]) / this.times,
w = Number(ary[2]) / this.times,
h = Number(ary[3])/ this.times;
this.ctx.lineWidth = "1"; //框的粗细
if(txt=="gt"){
this.ctx.strokeStyle = "yellow"; //gt选中的标注框为红色
}else{
this.ctx.strokeStyle = "red"; //dt选中的标注框为红色
}
//开始绘制
this.ctx.strokeRect(x, y, w, h);
});
},
},
};
</script>
<style lang="less" scoped>
.item-img {
text-align: center;
}
</style>
边栏推荐
- How to choose smart home? Take a look at this shopping guide
- Shell script (V) -- function
- 智能家居不知如何选?不妨看下这篇选购指南
- 消息中间件(一)MQ详解及四大MQ比较
- 运维、监控、AIOps的几个重要观点
- Flutter series -dart basic grammar learning
- 3GPP 5G R17标准冻结,RedCap作为重要特性值得关注!
- 远程访问及控制——SSH远程管理及TCP Wrappers 访问控制
- Dynamically changing the style of label elements in a loop
- Flutter系列-搭建Flutter开发环境
猜你喜欢

远程访问及控制——SSH远程管理及TCP Wrappers 访问控制

session机制详解以及session的相关应用

Shell script (V) -- function

wpa_ CLI parameter description

维智科技亮相西部数博会,时空AI技术获高度认可

IPLOOK作为O-RAN联盟会员,将共同促进5G产业发展

200亿VS 50亿,“脱水”小红书,到底值多钱?

China's games are "harvesting" foreigners

what? Homekit, Micah, aqara and other ecosystems can also be linked with tmall elf ecology through zhiting?

缓存3种方式及原理
随机推荐
Active Directory用户登录报告
SRE必将走向混沌工程时代--华为云混沌工程实践
实验4 NoSQL和关系数据库的操作比较
贪心之区间问题(1)
Digital commerce cloud: analyze the design idea of B2B2C multi-user mall system architecture, and open a new era of intelligent mall
函数的导数与微分的关系
Notes on Combinatorics (V) chains in distributive lattice
贪心之区间问题(3)
Dynamically changing the style of label elements in a loop
Flutter2.0运行在web上不同渲染器的问题
2022 t elevator repair recurrent training question bank and answers
如何提高工作效率?苹果电脑效率工具合集
上半年,这个领域竟出了7家新独角兽,资本争抢入局
5gc and satellite integrated communication scheme
程序员工具大全【持续更新】
贪心之区间问题(4)
Error in created hook: “TypeError: Cannot read property ‘tableId‘ of undefined“
3GPP 5G R17标准冻结,RedCap作为重要特性值得关注!
Is flush easy to use? Is it safe to open a mobile account?
智能家居不知如何选?不妨看下这篇选购指南