国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

vue canvas繪制矩形并解決由clearRec帶來的閃屏問題

2024-05-06 15:35:06
字體:
供稿:網(wǎng)友

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

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

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

那么怎么解決呢?

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

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

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

  部分代碼如下:

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

顯示層展示圖片:

//因?yàn)轫?xiàng)目是dialog展示自定義畫板,所以圖片展示就寫在了dialog打開的鉤子里,如果需要直接復(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í)開始位置與結(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 wwidth2 = this.endX - this.startX;      let wheigth2 = this.endY - this.startY;      this.customcxt.strokeRect(this.startX,this.startY,wwidth2,wheigth2)    }  }},//鼠標(biāo)松開時(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)},            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 泰和县| 醴陵市| 扎赉特旗| 自贡市| 保德县| 鹤山市| 天等县| 淮北市| 凯里市| 天津市| 中卫市| 永嘉县| 宜黄县| 工布江达县| 维西| 长寿区| 南澳县| 淅川县| 即墨市| 横峰县| 浦东新区| 临邑县| 乌海市| 焉耆| 靖宇县| 舒兰市| 黑山县| 高密市| 都江堰市| SHOW| 多伦县| 东平县| 汕头市| 庆云县| 赣榆县| 肇东市| 策勒县| 柳州市| 北票市| 百色市| 中西区|