vue canvas繪制矩形并解決由clearRec帶來的閃屏問題
起因:在cavnas繪制矩形時 鼠標移動一直在監(jiān)測中,所以鼠標移動的軌跡會留下一個個的矩形框,
要想清除矩形框官方給出了ctx.clearRect() 但是這樣是把整個畫布給清空了,因此需要不斷
向畫布展示新的圖片,這樣就出現(xiàn)了不斷閃屏的問題。
那么怎么解決呢?
microsoft提供了雙緩沖圖形技術(shù),可以點擊看看這邊文章。
具體就是畫圖的時候做兩個canvas層,一個臨時層 一個顯示層,鼠標的監(jiān)聽事件放在顯示層處理,
每次清空的時候只清空臨時層,這樣就可以解決閃屏問題了。
部分代碼如下:
<!--臨時層--> <canvas id="customPositionImg2" ref="table2" style=" display:none;"></canvas> <!--顯示層 增加鼠標按下,移動,松開事件--> <canvas id="customPositionImg" ref="table" @mousedown="mousedown" @mousemove="mousemove" @mouseup="mouseup" style=""></canvas>
顯示層展示圖片:
//因為項目是dialog展示自定義畫板,所以圖片展示就寫在了dialog打開的鉤子里,如果需要直接復制
vue.nextTick里面的代碼就行
show () {
vue.nextTick(_ => {
let customCanvas =this.$refs.table;// canvas顯示層
this.customstyle ='';
customCanvas.height = 740;
customCanvas.width = 1460;
this.customcxt = customCanvas.getContext("2d");
let img = new Image();
img.src = this.imgSrc;
let that = this;
img.onload = function () {
that.customRwidth = customCanvas.width / img.width; //原圖與展示圖片的寬高比
that.customRheight = customCanvas.height / img.height;
that.customcxt.drawImage(img, 0, 0, customCanvas.width, customCanvas.height);
};
})
},
鼠標操作事件
//鼠標按下時執(zhí)行
mousedown(e){
this.isMouseDownInCanvas = true;
// 鼠標按下時開始位置與結(jié)束位置相同 防止鼠標在畫完矩形后 點擊圖畫形成第二個圖形
this.endX = e.offsetX;
this.endY = e.offsetY;
this.startX = e.offsetX;
this.startY = e.offsetY;
},
//鼠標移動式時執(zhí)行
mousemove(e){
if (this.isMouseDownInCanvas){ // 當鼠標有按下操作時執(zhí)行
console.log( e.offsetX,e.offsetY);
if((this.endX != e.offsetX)||( this.endY != e.offsetY)){
this.endX = e.offsetX;
this.endY = e.offsetY;
let wwidth = this.endX - this.startX;
let wheigth = this.endY - this.startY;
let tempCanvas = this.$refs.table2; // canvas臨時層
let tempCtx = tempCanvas.getContext('2d');
tempCanvas.width = 1460; tempCanvas.height = 740; // 設(shè)置寬高
// 清除臨時層指定區(qū)域的所有像素
tempCtx.clearRect(0, 0, 1460, 740);
// 重新展示圖片
let img = new Image();
img.src = this.imgSrc;
let that = this;
img.onload = function () {
that.customcxt.drawImage(img, 0, 0,1460, 740);
};
this.customcxt.strokeStyle=" #00ff00"; //矩形框顏色
this.customcxt.lineWidth="2"; //矩形框?qū)挾?
this.customcxt.strokeRect(this.startX,this.startY,wwidth,wheigth); //繪制矩形
}else{
//鼠標按下靜止時顯示矩形的大小。
let wwidth2 = this.endX - this.startX;
let wheigth2 = this.endY - this.startY;
this.customcxt.strokeRect(this.startX,this.startY,wwidth2,wheigth2)
}
}
},
//鼠標松開時執(zhí)行
mouseup(e){
this.isMouseDownInCanvas = false;
// 繪制最終的矩形框
let wwidth = this.endX - this.startX;
let wheigth = this.endY - this.startY;
this.customcxt.strokeRect(this.startX,this.startY,wwidth,wheigth)
},
總結(jié)
以上所述是小編給大家介紹的vue cavnas繪制矩形并解決由clearRec帶來的閃屏問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
vue利用vue meta info設(shè)置每個頁面的title與meta信息
這篇文章主要給大家介紹了關(guān)于vue如何利用vue meta info設(shè)置每個頁面的title與meta信息的相關(guān)資料,文中將實現(xiàn)的方法介紹的非常詳細,對大家學習或者使用vue具有一定的參考學習價值,需要的朋友可以參考下2021-10-10
vue2 中如何實現(xiàn)動態(tài)表單增刪改查實例
本篇文章主要介紹了vue2 中如何實現(xiàn)動態(tài)表單增刪改查實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
Vant?如何修改van-collapse-item右側(cè)圖標
這篇文章主要介紹了Vant?如何修改van-collapse-item右側(cè)圖標,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
vue?button的@click方法無效鉤子函數(shù)沒有執(zhí)行問題
這篇文章主要介紹了vue?button的@click方法無效鉤子函數(shù)沒有執(zhí)行問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03

