当前位置:网站首页>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>
边栏推荐
- 最新大厂高级面试题 必备
- 每条你收藏的资讯背后,都离不开TA
- Convolutional neural network -- from r-cnn, fast r-cnn to fast r-cnn, mask r-cnn
- 2022 safety officer-c certificate special operation certificate examination question bank and answers
- Likeshop takeout ordering system "100% open source without encryption"
- I got the P8 "top-level" distributed architecture manual crazy spread on Alibaba intranet
- Learn from things | Yidun mobile terminal isomorphism practice, improve the official website interaction experience in a few steps
- Mysql database defines cursor in trigger
- [user article] examples of P4 consolidation practice guide disassemble resolve
- JS中的冒泡排序
猜你喜欢

How difficult the interview is! I was forced to survive the six rounds of interview of ant financial! Almost out (interview resumption)

最新大厂高级面试题 必备

Cow! His secret is to reproduce the paper in 2 hours——

Oracle 11g数据库安装教程
知物由学 | 小游戏的安全风险在哪里?

2022 safety officer-a certificate examination questions and online simulation examination

工信部再治数据安全,网易易盾“隐私合规”守住企业经营底线

Know things by learning | build a real-time anti plug-in mechanism from 0 to 1 to supplement the offensive and defensive power of mobile games in multiple dimensions

灵魂一问:为什么ES比MySQL更适合复杂条件搜索?

Learn from what you know | Yidun self-developed text real-time clustering technology, and wipe out the same kind of harmful content in social networks
随机推荐
[user article] examples of P4 consolidation practice guide disassemble resolve
7月第4周易盾业务风控关注 | 最高法对APP强索个人信息进行规制
Configuration and basic use of vim
美团到餐“祖传数仓”标准化治理笔记
Code compliance: five reasons why developers use helix QAC
Compilation and testing of raspberry pie driver code
机器学习——概念理解之IoU
【Codeforces】 A. Computer Game
面试好难啊!蚂蚁金服的六轮面试我是强撑过来!差点OUT(面试复盘)
Run loam_ Velodyne real-time mapping
微信小程序 实现位置地图显示,引入map地图,不含导航
ACL 2022 | prompt based automatic depolarization: effectively reducing bias in the pre training language model
I got the P8 "top-level" distributed architecture manual crazy spread on Alibaba intranet
How to improve the security of Android applications?
What are the safety risks of small games?
PostgreSQL 14 支持winserver2022吗?
Could not obtain transaction-synchronized Session for current thread
卷积神经网络——YOLOV2(YOLO9000)论文翻译
Knowing things by learning | app slimming down, the way of safety reinforcement under the new generation AAB framework
Learn from things | Yidun mobile terminal isomorphism practice, improve the official website interaction experience in a few steps