Vue使用鼠標(biāo)在Canvas上繪制矩形
本文實(shí)例為大家分享了Vue使用鼠標(biāo)在Canvas上繪制矩形的具體代碼,供大家參考,具體內(nèi)容如下
1.代碼
<template>
<div class="test" style="background-color: burlywood;">
<canvas id="myCanvas" ref="myCanvas"
width="460" height="240" @mousedown="mousedown" @mouseup="mouseup" @mousemove="mousemove">
</canvas>
</div>
</template>
<script>
export default {
name:"hello-world",
data() {
return {
flag: false,
x: 0,
y: 0
};
},
watch: {},
computed: {},
methods: {
mousedown(e){
console.log("鼠標(biāo)落下");
this.flag = true;
this.x = e.offsetX; // 鼠標(biāo)落下時(shí)的X
this.y = e.offsetY; // 鼠標(biāo)落下時(shí)的Y
},
mouseup(e){
console.log("鼠標(biāo)抬起");
this.flag = false;
},
mousemove(e){
console.log("鼠標(biāo)移動(dòng)");
this.drawRect(e);
},
drawRect(e){
if(this.flag){
console.log("繪制圖形");
const canvas = this.$refs.myCanvas;
var ctx = canvas.getContext("2d");
let x = this.x;
let y = this.y;
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.beginPath();
//設(shè)置線條顏色,必須放在繪制之前
ctx.strokeStyle = '#00ff00';
// 線寬設(shè)置,必須放在繪制之前
ctx.lineWidth = 1;
ctx.strokeRect(x,y,e.offsetX-x,e.offsetY-y);
}
}
},
created() {
},
mounted() {
}
};
</script>
<style scoped>
#myCanvas{
background-color: forestgreen;
background-image:url('../bg.jpg');
}
</style>
2.運(yùn)行截圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決vant的Toast組件時(shí)提示not defined的問(wèn)題
這篇文章主要介紹了解決vant的Toast組件時(shí)提示not defined的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
項(xiàng)目中如何使用axios過(guò)濾多次重復(fù)請(qǐng)求詳解
在項(xiàng)目開(kāi)發(fā)中經(jīng)常需要處理重復(fù)點(diǎn)擊導(dǎo)致多次調(diào)用接口的問(wèn)題,這篇文章主要介紹了項(xiàng)目中如何使用axios過(guò)濾多次重復(fù)請(qǐng)求的相關(guān)資料,需要的朋友可以參考下2021-07-07
vue中實(shí)現(xiàn)監(jiān)聽(tīng)數(shù)組內(nèi)部元素
這篇文章主要介紹了vue中實(shí)現(xiàn)監(jiān)聽(tīng)數(shù)組內(nèi)部元素方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
Vue實(shí)現(xiàn)點(diǎn)擊時(shí)間獲取時(shí)間段查詢功能
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)點(diǎn)擊時(shí)間獲取時(shí)間段查詢功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
JS 函數(shù)的 call、apply 及 bind 超詳細(xì)方法
這篇文章主要描述JS 函數(shù)的 call、apply 及 bind 方法的超詳細(xì)解說(shuō),感興趣的朋友可以參考下文,希望能幫助到您2021-08-08

