vuecanvas繪制矩形并解決由clearRec帶來(lái)的閃屏問(wèn)題

起因:在cavnas繪制矩形時(shí) 鼠標(biāo)移動(dòng)一直在監(jiān)測(cè)中,所以鼠標(biāo)移動(dòng)的軌跡會(huì)留下一個(gè)個(gè)的矩形框,

創(chuàng)新互聯(lián)建站長(zhǎng)期為千余家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為江海企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作,江海網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。

要想清除矩形框官方給出了ctx.clearRect() 但是這樣是把整個(gè)畫布給清空了,因此需要不斷

向畫布展示新的圖片,這樣就出現(xiàn)了不斷閃屏的問(wèn)題。

那么怎么解決呢?

microsoft提供了雙緩沖圖形技術(shù),可以點(diǎn)擊看看這邊文章。

具體就是畫圖的時(shí)候做兩個(gè)canvas層,一個(gè)臨時(shí)層 一個(gè)顯示層,鼠標(biāo)的監(jiān)聽(tīng)事件放在顯示層處理,

每次清空的時(shí)候只清空臨時(shí)層,這樣就可以解決閃屏問(wèn)題了。

部分代碼如下:

<!--臨時(shí)層-->
<canvas id="customPositionImg2" ref="table2"  ></canvas>
<!--顯示層 增加鼠標(biāo)按下,移動(dòng),松開(kāi)事件-->
<canvas id="customPositionImg" ref="table"  @mousedown="mousedown" @mousemove="mousemove" @mouseup="mouseup" ></canvas>

顯示層展示圖片:

//因?yàn)轫?xiàng)目是dialog展示自定義畫板,所以圖片展示就寫在了dialog打開(kāi)的鉤子里,如果需要直接復(fù)制
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); 
     };

   })

},

鼠標(biāo)操作事件

//鼠標(biāo)按下時(shí)執(zhí)行
mousedown(e){
  this.isMouseDownInCanvas = true;
  // 鼠標(biāo)按下時(shí)開(kāi)始位置與結(jié)束位置相同 防止鼠標(biāo)在畫完矩形后 點(diǎn)擊圖畫形成第二個(gè)圖形
  this.endX = e.offsetX;
  this.endY = e.offsetY;
  this.startX = e.offsetX;
  this.startY = e.offsetY;

},
//鼠標(biāo)移動(dòng)式時(shí)執(zhí)行
mousemove(e){
  if (this.isMouseDownInCanvas){ // 當(dāng)鼠標(biāo)有按下操作時(shí)執(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臨時(shí)層
      let tempCtx = tempCanvas.getContext('2d');
      tempCanvas.width = 1460; tempCanvas.height = 740; // 設(shè)置寬高
      // 清除臨時(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{
        //鼠標(biāo)按下靜止時(shí)顯示矩形的大小。
      let wwidth3 = this.endX - this.startX;
      let wheigth3 = this.endY - this.startY;
      this.customcxt.strokeRect(this.startX,this.startY,wwidth3,wheigth3)
    }
  }
},
//鼠標(biāo)松開(kāi)時(shí)執(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帶來(lái)的閃屏問(wèn)題,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

分享名稱:vuecanvas繪制矩形并解決由clearRec帶來(lái)的閃屏問(wèn)題
本文鏈接:http://www.muchs.cn/article14/geeege.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、網(wǎng)站改版網(wǎng)站營(yíng)銷、企業(yè)建站、ChatGPT、微信公眾號(hào)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)