DDR爱好者之家 Design By 杰米
起因:在cavnas绘制矩形时 鼠标移动一直在监测中,所以鼠标移动的轨迹会留下一个个的矩形框,
要想清除矩形框官方给出了ctx.clearRect() 但是这样是把整个画布给清空了,因此需要不断
向画布展示新的图片,这样就出现了不断闪屏的问题。
那么怎么解决呢?
microsoft提供了双缓冲图形技术,可以点击看看这边文章。
具体就是画图的时候做两个canvas层,一个临时层 一个显示层,鼠标的监听事件放在显示层处理,
每次清空的时候只清空临时层,这样就可以解决闪屏问题了。
部分代码如下:
<!--临时层--> <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); }; }) },
鼠标操作事件
//鼠标按下时执行 mousedown(e){ this.isMouseDownInCanvas = true; // 鼠标按下时开始位置与结束位置相同 防止鼠标在画完矩形后 点击图画形成第二个图形 this.endX = e.offsetX; this.endY = e.offsetY; this.startX = e.offsetX; this.startY = e.offsetY; }, //鼠标移动式时执行 mousemove(e){ if (this.isMouseDownInCanvas){ // 当鼠标有按下操作时执行 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; // 设置宽高 // 清除临时层指定区域的所有像素 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"; //矩形框宽度 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) } } }, //鼠标松开时执行 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) },
总结
以上所述是小编给大家介绍的vue cavnas绘制矩形并解决由clearRec带来的闪屏问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
更新日志
2024年03月29日
2024年03月29日
- 苏有朋.1994-珍惜的背包(第3张)【飞碟】【WAV+CUE】
- 《宝可梦:朱紫》“蓝之圆盘”IGN7分:性能优化仍旧灾难
- 《塞尔达传说:王国之泪》大获成功!多项年度数据表现优秀
- 应景!欧任用《塞尔达传说:王国之泪》堆雪人
- 杨采妮.1996-为所欲为(国专)【EMI百代】【WAV+CUE】
- 黎姿.1993-如果真的恋爱【华纳】【WAV+CUE】
- 朱婧汐.2024-VILLAIN反派角色【StreetVoic】【FLAC分轨】
- 《喷射战士3》最新祭典公布 1月13日举办
- NS日服年度下载榜公开:《王泪》不敌《西瓜游戏》
- 科乐美《心跳回忆GS》系列推出复刻版 2月14日登陆NS
- 林峰.2012-SELF-PORTRAIT【英皇娱乐】【WAV+CUE】
- TENSION.2004-Story新歌+精丫EMI百代】【FLAC分轨】
- 草蜢.2000-环球2000超巨星系列【环球】【WAV+CUE】
- 国行Switch开启元旦折扣!《马里奥奥德赛》7折促销
- 《塞尔达》官方账号感谢玩家支持 发布王泪最新壁纸