当前位置:网站首页>Canvas draws graphics according to coordinate points
Canvas draws graphics according to coordinate points
2022-07-27 18:42:00 【V_ AYA_ V】
There is a need , You need to set the coordinates in according to the coordinate set returned in the background canvas Draw a picture in . because canvas Not much , Simply record and learn . The whole requirement realization is mainly divided into three parts
One . Front end proportional display a4 Paper size canvas canvas
1. A4 Paper size :210×297mm Generally in ps Can be converted into different according to different resolutions px Company :
- When the resolution is 72 Pixels / inches ,A4 The length and width of paper pixels are 842×595
- When the resolution is 120 Pixels / inches ,A4 The length and width of paper pixels are 2105×1487
- When the resolution is 150 Pixels / inches ,A4 The length and width of paper pixels are 1754×1240
- When the resolution is 300 Pixels / inches ,A4 The length and width of paper pixels are 3508×2479
2. Calculate scale
//calcRate Method , Based on width
calcRate(){
let containerDom = this.$refs['canvas-container'] // obtain DOM
const containerWith = containerDom.clientWidth || containerDom.offsetWidth // obtain DOM Width
// const rate = (containerWith / this.originWidth).toFixed() // Will round off , Accuracy has an impact
const rate = Math.floor(containerWith / this.originWidth *100) / 100 // Calculate the scale to two decimal places
this.rate = rate
}
3. Calculate canvas size
computed:{
rateWidth(){
return this.originWidth * this.rate //originWidth Is the original width
},
rateHeight(){
return this.originHeight * this.rate //originWidth Is the original height
}
}
4.canvas Elements
<canvas ref="canvas" :width="rateWidth" :height="rateHeight"></canvas>
Two . Processing point set data
Background return data processing
//[
// [{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 // You need to scale the size proportionally to ensure the correct position
it.y = it.y * this.rate // You need to scale the size proportionally to ensure the correct position
})
})
this.dots = dots
this.$nextTick(()=>{
this.initCanvas()
})
}else{
console.log(res.msg)
}
})
.catch(err=>{
console.log(err)
})
}
3、 ... and . Drawing graphics
// initialization canvas object , Began to draw . There are two drawing forms , One is that many points form a line , The other is to connect dots into lines . If there are enough points, you can draw a small circle directly , The connection method is adopted here
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 // Notice the end of loop condition
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();
}
})
}
effect :
Complete code :
<template>
<div class="canvas" ref="canvas-container">
<canvas ref="canvas" :width="rateWidth" :height="rateHeight">
</canvas>
</div>
</template>
<script>
import * as Api from './indexApi'
//rate Conversion ratio ( Take width as the conversion standard )
//originWidth Original width
//originHeight Original high
//rateWidth Scale conversion width
//rateHeight Scale conversion high
export default {
data(){
return{
rate: 1,
originWidth: 2480, //300 The resolution of the
originHeight: 3508, //300 The resolution of the
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>
边栏推荐
- [mit 6.s081] LEC 5: calling conventions and stack frames risc-v notes
- 阿里p8总结的10条 SQL 优化方案(非常实用)
- 机器学习——SVM训练集只有一类标签数据而引发的错误
- [MIT 6.S081] Lab 7: Multithreading
- MySQL four locks
- 怎么会不喜欢呢,CI/CD中轻松发送邮件
- [MIT 6.S081] Lec 6: Isolation & system call entry/exit 笔记
- 微信小程序获取手机号
- [MIT 6.S081] Lab 11: networking
- Deep learning: stgcn learning notes
猜你喜欢

The combination of text and words perfectly explains the implementation process of MySQL logical backup

Basic operations of MySQL view

搭建一个简单的知识问答系统

Generate PDM file from Navicat export table

2021.7.12 internal and external connection of notes

知识图谱 — pyhanlp实现命名体识别(附命名体识别代码)

Wechat applet obtains openid, sessionkey, unionid

MySQL learning day3 multi table query / transaction / DCL
![[MIT 6.S081] Lab 7: Multithreading](/img/f4/26e513fb8678a88cfba29c1a636b37.png)
[MIT 6.S081] Lab 7: Multithreading

MySQL basic statement
随机推荐
Mybtis-Plus常用的内置方法
Idea packaging war package and war package location
MySQL查询列必须和group by字段一致吗?
[mit 6.s081] LEC 9: interrupts notes
虚拟偶像的歌声原来是这样生成的!
JDBC学习 Day1:JDBC
2021.7.30笔记 索引
JDBC learning day1:jdbc
C杂讲 链表初讲
2021.8.6笔记 jsoup
2021.7.12 internal and external connection of notes
2021.7.31笔记 视图
2021.7.28笔记 事务
[mit 6.s081] LEC 3: OS organization and system calls notes
机器学习——SVM训练集只有一类标签数据而引发的错误
How to realize the full-text content retrieval of word, PDF and txt files?
Installation and deployment of zabbix6.0
2 万字 + 30 张图 | 细聊 MySQL undo log、redo log、binlog 有什么用?
Linked list storage structure of dynamic linked list 2 stack (linkedstack Implementation)
2021.8.1笔记 数据库设计