vue3使用canvas的詳細(xì)指南
canvas是什么?
一個(gè)html5支持的新標(biāo)簽,見名知意,canvas就是畫板的意思,可以在canvas上畫畫。css畫三角形很簡(jiǎn)單,但是要畫五角星呢,不妨試試canvas。
在html中使用canvas
1、canvas是html5中的一個(gè)標(biāo)簽。
新建一個(gè)html。并在body中加入canvas標(biāo)簽。
<body>
<canvas height="600" width="600"></canvas>
</body>
此時(shí)canvas已經(jīng)顯示在畫板中,只不過(guò)因?yàn)楹蚥ody的顏色一樣,所以看不出來(lái)。
在head中加入css樣式。
<style>
canvas {
border:1px solid;
}
</style>
這時(shí)我們就可以看到canvas了。

2、獲取CanvasRenderingContext2D對(duì)象用于繪圖。
先給canvas一個(gè)id屬性,
<canvas id='canvas' height="600" width="600"></canvas>
獲取。
<script>
const ctx=document.querySelector('#canvas').getContext('2d');
</script>
注意,script標(biāo)簽應(yīng)該在body標(biāo)簽后(至少在canvas標(biāo)簽后),只有在canvas渲染后才能通過(guò)JavaScript代碼獲取到canvas中的CanvasRenderingContext2D對(duì)象。
<body>
<canvas height="600" width="600"></canvas>
</body>
<script>
const ctx=document.querySelector('.canvas').getContext('2d');
</script>
3、使用JavaScript代碼進(jìn)行繪畫。
<script>
const ctx=document.querySelector('#canvas').getContext('2d');
ctx.moveTo(100,100);
ctx.lineTo(100,400);
ctx.stroke();
</script>
該代碼繪制了一條直線。

關(guān)于CanvasRenderingContext2D對(duì)象更多的繪制方法請(qǐng)參考官方文檔。至少現(xiàn)在我們知道canvas是如何使用的了。(一定要注意要在渲染完成后才能通過(guò)JavaScript代碼獲取CanvasRenderingContext2D對(duì)象)
在vue3中使用canvas
1、創(chuàng)建vite+vue3項(xiàng)目并運(yùn)行。
npm init vue@latest

2、創(chuàng)建我們的canvas。
這是我們的App.vue文件
<script setup> </script> <template> </template> <style scoped> </style>
創(chuàng)建我們的canvas
<script setup>
</script>
<template>
<canvas height="600" width="600"></canvas>
</template>
<style scoped>
canvas {
border: 1px solid;
}
</style>

3、獲取CanvasRenderingContext2D對(duì)象并繪制直線。
給canvas標(biāo)簽添加一個(gè)ref屬性
<canvas ref='canvas' height="600" width="600"></canvas>
獲取canvas對(duì)象
<script setup>
import {ref} from 'vue'
const canvas = ref();
</script>
渲染完成后獲取CanvasRenderingContext2D對(duì)象
<script setup>
import { onMounted, ref } from 'vue'
const canvas = ref();
onMounted(() => {
const ctx = canvas.value.getContext('2d');
})
</script>
畫一條直線
<script setup>
import { onMounted, ref } from 'vue'
const canvas = ref();
onMounted(() => {
const ctx = canvas.value.getContext('2d');
ctx.moveTo(100, 100);
ctx.lineTo(100, 400);
ctx.stroke();
})
</script>

4、模板
<script setup>
import { onMounted, ref } from 'vue'
const canvas = ref();
let ctx = ref();
const drawLine = () => {
ctx.moveTo(100, 100);
ctx.lineTo(100, 400);
ctx.stroke();
}
const initContext = () => {
ctx = canvas.value.getContext('2d');
}
onMounted(() => {
initContext();
drawLine();
})
</script>
<template>
<canvas ref='canvas' height="600" width="600"></canvas>
</template>
<style scoped>
canvas {
border: 1px solid;
}
</style>
canvas快速入門
繪制折線
一個(gè)moveTo配合多個(gè)lineTo。可以通過(guò)lineWidth設(shè)置線寬,還可以設(shè)置兩個(gè)端點(diǎn)和轉(zhuǎn)折處的形狀(使用lineCap和lineJoin)
// 使用moveTo,lineTo,lineWidth,lineCap,lineJoin
const drawCurvedLine = () => {
ctx.moveTo(100, 100);
ctx.lineTo(400, 100);
ctx.lineTo(100, 400);
ctx.lineTo(400, 400);
ctx.lineCap = 'round';
ctx.lineJoin = 'round';
ctx.stroke();
}
繪制矩形
rect方法以及strokeRect和fillRect。效果等效:strokeRect=rect+stroke,fillRect=rect+stroke。
繪制方式:繪制邊框,使用stroke,繪制填充,使用fill。strokeStyle可以設(shè)置邊框顏色,fillStyle可以設(shè)置填充顏色。
// 使用rect,srokeStyle,stroke,fillStyle,fill
const drawStrokeRect = () => {
ctx.rect(100, 100, 100, 100);
ctx.strokeStyle = 'green';
ctx.stroke();
}
const drawFillRect = () => {
ctx.rect(300, 100, 100, 100);
ctx.fillStyle = 'blue';
ctx.fill();
}
將繪制一個(gè)綠色邊框的矩形和藍(lán)色的矩形。然而,當(dāng)一同調(diào)用時(shí),會(huì)發(fā)現(xiàn)變成了兩個(gè)一模一樣的矩形(綠色邊框或者藍(lán)色填充)。
屬性作用域:解決上述問(wèn)題,使用beginPath方法即可。beginPath將后面對(duì)于屬性的設(shè)置隔離開來(lái),以避免覆蓋前面的屬性。
// 這里還嘗試了使用strokeRect和fillRect替代了rect、stroke、fill
const drawStrokeRect = () => {
ctx.beginPath();
ctx.strokeStyle='green';
ctx.strokeRect(100,100,100,100);
}
const drawFillRect = () => {
ctx.beginPath();
ctx.fillStyle = 'blue';
ctx.fillRect(300, 100, 100, 100);
}
繪制弧線
圓圈
ctx.beginPath(); ctx.arc(100,75,50,0,2*Math.PI); ctx.stroke();
圓弧
ctx.beginPath(); ctx.arc(100,75,50,90/180*Math.PI,2*Math.PI); ctx.stroke();
扇形
ctx.beginPath(); ctx.moveTo(100,75); ctx.arc(100,75,50,90/180*Math.PI,2*Math.PI); ctx.closePath(); ctx.fill();
圓環(huán)
const RINGWIDTH = 10; ctx.beginPath(); ctx.arc(100, 75, 90, 0, 2 * Math.PI); ctx.fill(); ctx.beginPath(); ctx.arc(100, 75, 90-2*RINGWIDTH, 0, 2 * Math.PI); ctx.fillStyle = 'white'; ctx.fill();
補(bǔ)充:
- 如你所見,繪制扇形時(shí)使用了closePath,意思是將所有端點(diǎn)連接起來(lái)(一般是將終點(diǎn)和起點(diǎn)連接起來(lái),形成一個(gè)閉合圖形。只有圖形閉合時(shí),fill才能生效)。
- 所有函數(shù)的參數(shù)不需要單位。(設(shè)置字體時(shí),ctx.font=‘40px’,需要帶單位,但確實(shí)不是函數(shù)的參數(shù))
- 需要角度作為參數(shù)時(shí),都是以弧度的形式提供。計(jì)算公式,弧度=角度*Math.PI/180。90度,記為90*Math.PI/180。
- 更多關(guān)于畫布的使用,可以查看HTML Canvas 參考手冊(cè) (w3school.com.cn)
總結(jié)
到此這篇關(guān)于vue3使用canvas的文章就介紹到這了,更多相關(guān)vue3使用canvas內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue?文件切片上傳的項(xiàng)目實(shí)現(xiàn)
本文主要介紹了vue?文件切片上傳的項(xiàng)目實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
vue實(shí)現(xiàn)編輯器鍵盤抬起時(shí)內(nèi)容跟隨光標(biāo)距頂位置向上滾動(dòng)效果
這篇文章主要介紹了vue實(shí)現(xiàn)編輯器鍵盤抬起時(shí)內(nèi)容跟隨光標(biāo)距頂位置向上滾動(dòng)效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Vue事件處理中的上下文問(wèn)題:原因與解決過(guò)程
本文將探討 Vue 事件處理中常見的上下文問(wèn)題及其解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
vue實(shí)現(xiàn)動(dòng)態(tài)綁定行內(nèi)樣式style的backgroundImage
這篇文章主要介紹了vue實(shí)現(xiàn)動(dòng)態(tài)綁定行內(nèi)樣式style的backgroundImage方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
vue如何基于el-table實(shí)現(xiàn)多頁(yè)多選及翻頁(yè)回顯過(guò)程
在最近的一個(gè)項(xiàng)目中我需要實(shí)現(xiàn)表格的翻頁(yè),并且還要實(shí)現(xiàn)全選、多選功能,下面這篇文章主要給大家介紹了關(guān)于vue如何基于el-table實(shí)現(xiàn)多頁(yè)多選及翻頁(yè)回顯過(guò)程的相關(guān)資料,需要的朋友可以參考下2022-12-12
Vue封裝遠(yuǎn)程下拉框組件的實(shí)現(xiàn)示例
本文主要介紹了Vue封裝遠(yuǎn)程下拉框組件的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
VuePress 靜態(tài)網(wǎng)站生成方法步驟
這篇文章主要介紹了VuePress 靜態(tài)網(wǎng)站生成方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
Monaco-editor 的 JSON Schema 配置及使用介紹
這篇文章主要為大家介紹了Monaco-editor 的 JSON Schema 配置及使用介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10

