当前位置:网站首页>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>
边栏推荐
- 5G 短消息解决方案
- Flutter series -dart basic grammar learning
- org. apache. ibatis. binding. BindingException: Invalid bound statement (not found)
- Detailed explanation of shell script (x) -- how to use sed editor
- 维智科技亮相西部数博会,时空AI技术获高度认可
- Pull down refresh and pull up to load more listviews
- 如何更改Apple Watch上的表盘
- PAT甲级1093 Count PAT‘s (25分)
- After reading the hated courage
- 组合学笔记(五)分配格中的链
猜你喜欢

Iplook becomes RedHat (red hat) business partner

缓存3种方式及原理

Shell script explanation (IV) -- while loop and until loop of loop statements (additional examples and analysis)

IPLOOK 5GC与亚信国际CHF(计费功能)对接成功

5GC和卫星融合通信方案

【建议收藏】消息队列常见的使用场景

Active Directory用户登录报告

3GPP 5g R17 standard is frozen, and redcap as an important feature deserves attention!

Digital commerce cloud: analyze the design idea of B2B2C multi-user mall system architecture, and open a new era of intelligent mall

5g short message solution
随机推荐
Iplook 5gc successfully connected with CICA international CHF (billing function)
2022 operation of simulated examination platform for examination question bank of welder (elementary) special operation certificate
线程池:ThreadPoolExcutor源码阅读
IPLOOK 5GC与亚信国际CHF(计费功能)对接成功
Intelligent procurement system solution for processing and manufacturing industry: help enterprises realize integrated and Collaborative Procurement in the whole process
Message Oriented Middleware (I) MQ explanation and comparison of four MQS
结构型模式之适配器模式
How to manage tasks in note taking software such as flowus and notation?
Shell script explanation (VII) -- regular expression, sort, uniq, tr
小波变换db4进行四层分解及其信号重构—matlab分析及C语言实现
codeup最长回文子串
数字赋能机械制造业,供应链协同管理系统解决方案助力企业供应链再升级
集群、分布式、微服务概念和区别
Shell script explanation (II) -- conditional test, if statement and case branch statement
mini-Web框架:模板替换与路由列表功能开发 | 黑马程序员
Shell script (V) -- function
【干货|接口测试必备技能-常见接口协议解析】
STM32控制矩阵按键,HAL库,cubeMX配置
shell脚本详解(四)——循环语句之while循环和until循环(附加例题及解析)
贪心之区间问题(1)