起因:在cavnas绘制矩形时 鼠标移动一直在监测中,所以鼠标移动的轨迹会留下一个个的矩形框,

要想清除矩形框官方给出了ctx.clearRect() 但是这样是把整个画布给清空了,因此需要不断

向画布展示新的图片,这样就出现了不断闪屏的问题。

那么怎么解决呢?

microsoft提供了双缓冲图形技术,可以点击看看这边文章。

具体就是画图的时候做两个 cavnas层,一个临时层 一个显示层,鼠标的监听事件放在显示层处理,

每次清空的时候只清空临时层,这样就可以解决闪屏问题了。

  部分代码如下:

<!--临时层-->
<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带来的闪屏问题的更多相关文章

  1. 双缓冲解决控制台应用程序输出“闪屏”(C/C++,Windows)

    使用 C 语言编写游戏的小伙伴们想必起初都要遇到这样的问题,在不断清屏输出数据的过程中,控制台中的输出内容会不断地闪屏.出现这个问题的原因是程序对数据处理花掉的时间影响到了数据显示,或许你可以使用局部 ...

  2. JQuery Mobile - 解决切换页面时,闪屏,白屏等问题

    在点击链接,切换页面时候,总是闪屏,感觉很别扭,看起来不舒服,怎么解决这个问题?方法很简单,就是在每个页面的meta标签内定义user-scalable的属性为 no! <meta name=& ...

  3. vue下canvas绘制矩形

    起因:根据项目需求本人写了一个绘制矩形的组件.功能:在图片中绘制矩形,根据图片大小进行自适应展示,获取图片矩形坐标.思路:首先定义一个固定大小的DIV,DIV标签中有监测鼠标变化的四个事件moused ...

  4. opencv2 使用鼠标绘制矩形并截取和保存矩形区域图像

    前言 好长时间没写博文了,今天偷偷懒写篇关于opencv2中鼠标响应操作的文章. 鼠标操作属于用户接口设计,以前一直使用Qt来做,但是如果只需要简单的鼠标,键盘操作,直接调用opencv库的函数也未尝 ...

  5. canvas 绘制 矩形 圆形

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <tit ...

  6. canvas 绘制矩形和圆形

    canvas绘制有两神方法:1).填充(fill)填充是将图形内部填满. 2).绘制边框 (stroke)绘制边框是不把图形内部填满,只是绘制图形的外框. 当我们在绘制图形的时候,首先要设定好绘制的样 ...

  7. 详解使用CSS3绘制矩形、圆角矩形、圆形、椭圆形、三角形、弧

    1.矩形 绘制矩形应该是最简单的了,直接设置div的宽和高,填充颜色,效果就出来了. 2.圆角矩形 绘制圆角矩形也很简单,在1的基础上,在使用css3的border-radius,即可. 3.圆 根据 ...

  8. Canvas入门(1):绘制矩形、圆、直线、曲线等基本图形

    来源:http://www.ido321.com/968.html 一.Canvas的基础知识 Canvas是HTML 5中新增的元素,专门用于绘制图形.canvas元素就相当于一块“画布”,一块无色 ...

  9. [HTML5 Canvas学习]绘制矩形

    1.使用strokeRect和fillRect方法绘制矩形 a.strokeRect是绘制一个不填充的矩形 b.fillRect是绘制一个填充的矩形 代码: <script> var ca ...

随机推荐

  1. Unity相机鼠标基本控制

    一.滚轮控制视角缩放 /// <summary> /// 滚轮控制相机视角缩放 /// </summary> public void CameraFOV() { //获取鼠标滚 ...

  2. 【转载】Hadoop面试(1)

    转自:http://www.cnblogs.com/xiaolong1032/p/4504992.html 列举出hadoop常用的一些InputFormat InputFormat是用来对我们的输入 ...

  3. pyppeteer硬钢掉淘宝登入的滑块验证

    完整代码我也不好公布,我可以给你们思路,以及部分代码动动脑子看看文档应该也能搞定 一.初始化Chromium浏览器相关属性 browser = await pyppeteer.launch({'hea ...

  4. Iframe 高度自适应,js控制Iframe 高度自适应

     Iframe 高度自适应, js控制Iframe 高度自适应, iframe自适应高度 ================================ ©Copyright 蕃薯耀 2019年12 ...

  5. varchar(n)

    MySQL5.0.3之前varchar(n)这里的n表示字节数MySQL5.0.3之后varchar(n)这里的n表示字符数,比如varchar(200),不管是英文还是中文都可以存放200个根据字符 ...

  6. kvm增加磁盘容量

    一.qcow2格式 查看镜像文件实际占用空间 ls -alh t.qcow2 加容量(只能加不能减) qemu-img resize t.qcow2 +1G 查看qcow2信息 qemu-img in ...

  7. 511,display:inline-block什么时候不会显示间隙?

    (百科: 在CSS布局中,如果我们想要将一些元素在同一行显示,其中的一种方法就是把要同行显示的元素设置display属性为inline-block,但是你会发现这些同行显示的inline-block元 ...

  8. Linux - Shell - 在多个文件中查找关键字

    1. 概述 在多个文件中 查找内容 2. 想干啥 目的 在 多个文件 中, 查找内容 准备 之前在 单个文件里 查找过内容 工具 awk 前提 文件有固定格式 查找时有字段的要求 例子 # print ...

  9. centos 6.10 安装mysql 5.7.27 出现缺少libnuma.so.1的问题

    centos 6.10安装mysql 5.7.27出现以下报错: [root@localhost /]# /usr/local/mysql/app/mysql/bin/mysqld --default ...

  10. iso15693芯片读写工具套件 icode-slix2读写 nfc type 5 tag读写

    iso15693芯片读写工具套件 icode-slix2读写 nfc type 5 tag读写校验套件 iso15693工具套件支持icode-slix,icode-slix2芯片的读写,支持iso1 ...