当前位置:网站首页>canvas根据坐标点绘制图形
canvas根据坐标点绘制图形
2022-07-27 15:55:00 【V_AYA_V】
有一需求,需要根据后台返回的坐标集在canvas中绘制图形。由于canvas使用不多,简单记录学习一下。整个需求实现主要分为三个部分
一.前端等比展示a4纸大小canvas画布
1. A4纸大小:210×297mm一般在ps中可以根据分辨率的不同转换成不同的px单位:
- 当分辨率是72像素/英寸时,A4纸像素长宽分别是842×595
- 当分辨率是120像素/英寸时,A4纸像素长宽分别是2105×1487
- 当分辨率是150像素/英寸时,A4纸像素长宽分别是1754×1240
- 当分辨率是300像素/英寸时,A4纸像素长宽分别是3508×2479
2.计算缩放比例
//calcRate方法,以宽为基准
calcRate(){
let containerDom = this.$refs['canvas-container'] //获取DOM
const containerWith = containerDom.clientWidth || containerDom.offsetWidth //获取DOM宽度
// const rate = (containerWith / this.originWidth).toFixed() //会四舍五入,精度有影响
const rate = Math.floor(containerWith / this.originWidth *100) / 100 //计算比例精确到两位小数
this.rate = rate
}
3.计算画布大小
computed:{
rateWidth(){
return this.originWidth * this.rate //originWidth为原始宽度
},
rateHeight(){
return this.originHeight * this.rate //originWidth为原始高度
}
}
4.canvas元素
<canvas ref="canvas" :width="rateWidth" :height="rateHeight"></canvas>
二.处理点集数据
后台返回数据处理
//[
// [{x:xxx,y:xxx},{x:xxx,y:xxx},{x:xxx,y:xxx},{x:xxx,y:xxx}...],
// [{x:xxx,y:xxx},{x:xxx,y:xxx},{x:xxx,y:xxx},{x:xxx,y:xxx}...],
// [{x:xxx,y:xxx},{x:xxx,y:xxx},{x:xxx,y:xxx},{x:xxx,y:xxx}...]
//]
getDots(){
Api.get_dots()
.then(res=>{
if(res.errCode ==0){
let dots = res.data
dots.forEach(item=>{
item.forEach(it=>{
it.x = it.x * this.rate //需要根据比例等比缩放大小保证位置正确
it.y = it.y * this.rate //需要根据比例等比缩放大小保证位置正确
})
})
this.dots = dots
this.$nextTick(()=>{
this.initCanvas()
})
}else{
console.log(res.msg)
}
})
.catch(err=>{
console.log(err)
})
}
三.绘制图形
// 初始化canvas对象,开始绘制。有两种绘图形式,一种是点多成线,另一种是连点成线。点足够多的话可以直接绘制小圆,这里采用的是连线法
initCanvas(){
let canvasDom = this.$refs['canvas']
let ctx = canvasDom.getContext("2d")
this.dots.forEach(item=>{
for(let i=0; i < item.length; i++){
if(i == item.length-1) return //注意循环结束条件
ctx.beginPath();
ctx.moveTo(item[i].x,item[i].y);
ctx.lineWidth = 2
ctx.lineTo(item[i+1].x,item[i+1].y)
ctx.stroke();
}
})
}
效果:
完整代码:
<template>
<div class="canvas" ref="canvas-container">
<canvas ref="canvas" :width="rateWidth" :height="rateHeight">
</canvas>
</div>
</template>
<script>
import * as Api from './indexApi'
//rate 换算比例(以宽为换算标准)
//originWidth 原始宽
//originHeight 原始高
//rateWidth 比例换算宽
//rateHeight 比例换算高
export default {
data(){
return{
rate: 1,
originWidth: 2480, //300分辨率
originHeight: 3508, //300分辨率
dots:[],
}
},
computed:{
rateWidth(){
return this.originWidth * this.rate
},
rateHeight(){
return this.originHeight * this.rate
}
},
mounted(){
this.getDots()
this.calcRate()
},
methods:{
getDots(){
Api.get_dots()
.then(res=>{
if(res.errCode ==0){
let dots = res.data
dots.forEach(item=>{
item.forEach(it=>{
it.x = it.x * this.rate
it.y = it.y * this.rate
})
})
this.dots = dots
this.$nextTick(()=>{
this.initCanvas()
})
}else{
console.log(res.msg)
}
})
.catch(err=>{
console.log(err)
})
},
calcRate(){
let containerDom = this.$refs['canvas-container']
const containerWith = containerDom.clientWidth || containerDom.offsetWidth
const rate = Math.floor(containerWith / this.originWidth *100) / 100
this.rate = rate
},
initCanvas(){
let canvasDom = this.$refs['canvas']
let ctx = canvasDom.getContext("2d")
this.dots.forEach(item=>{
for(let i=0; i < item.length; i++){
if(i == item.length-1) return
ctx.beginPath();
ctx.moveTo(item[i].x,item[i].y);
ctx.lineWidth = 2
ctx.lineTo(item[i+1].x,item[i+1].y)
ctx.stroke();
}
})
}
}
}
</script>
<style lang="scss" scoped>
.canvas{
box-sizing: border-box;
padding: 10px;
width: 100%;
display: flex;
justify-content: center;
canvas{
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}
}
</style>
边栏推荐
- About the problem of abnormal C printing
- Application of knowing things and learning | correlation graph analysis in anti cheating business
- Wechat applet realizes location map display and introduces map map without navigation
- 细数国产接口协作平台的六把武器!
- 【Codeforces】 A. Computer Game
- WPF做登陆界面
- Learn from things | Yidun mobile terminal isomorphism practice, improve the official website interaction experience in a few steps
- 知物由学 | 小游戏的安全风险在哪里?
- XStream reports an error abstractreflectionconverter$unknownfield exception when parsing XML
- Kubernetes 1.24 high availability cluster binary deployment
猜你喜欢

每条你收藏的资讯背后,都离不开TA

Convolutional neural network -- SSD thesis translation

如何开发一款在线Excel表格系统(上)

笔试缺考者入围教师招聘面试?河南祥符:个别考生成绩统计错误

The Ministry of industry and information technology re governs data security, and Netease Yidun "privacy compliance" keeps the bottom line of enterprise operation

JSP自定义标签(下)

JDBC connection database reading foreground cannot display data

2022 safety officer-a certificate examination questions and online simulation examination
知物由学 | SO加固如何提升Android应用的安全性?
![[MCU] 2.3 CPU of AT89S52](/img/4c/7b9d235bf8a919339d75a7ec95789f.png)
[MCU] 2.3 CPU of AT89S52
随机推荐
Telecommuting can be easily realized in only three steps
机器学习之评价指标(一)——回归评价指标
Wechat applet to make calls
The global cloud market is growing rapidly, and data security has entered a strong regulatory era of rule of law
Wechat applet realizes location map display and introduces map map without navigation
Establishing SSL connection without server‘s identity verification is not recommended
树莓派驱动代码的编译和测试
微信小程序 实现位置地图显示,引入map地图,不含导航
运行loam_velodyne实时建图
How to learn C language? This article gives you the complete answer
Oracle 11g database installation tutorial
[introduction to database system (Wang Shan)] Chapter 4 - Database Security
X-sheet development tutorial: initialization configuration custom layout
Getting started with shell programming base
MySQL 中 You can‘t specify target table ‘表名‘ for update in FROM clause错误解决办法
What's the use of games| Game application value research case collection
查找表中多余重复记录并删除保留最小一个
ACL 2022 | prompt based automatic depolarization: effectively reducing bias in the pre training language model
防止sql注入
mysql解决唯一索引重复导致的插入失败问题