canvas橡皮筋式线段绘制

这里的要注意
getImageData //getImageData() 方法返回 ImageData 对象,该对象拷贝了画布指定矩形的像素数据。

putImageData
//putImageData() 方法将图像数据(从指定的 ImageData 对象)放回画布上。

context.save() 设置绘图环境存档
context.restore() 读取绘图环境存档

var canvas=document.getElementById("canvas"),
   context=canvas.getContext("2d"),
   eraseAllButton=document.getElementById("eraseAllButton"),
   strokeStyleSelect=document.getElementById("strokeStyleSelect"),
   guidewireCheckbox=document.getElementById("guidewireCheckbox"),
   drawingSurfaceImageData,
   mousedown={},
   rubberbandRect={},
   dragging=false,
   guidewires=guidewireCheckbox.checked;


    function windowToCanvas(x,y){
        var bbox=canvas.getBoundingClientRect()//getBoundingClientRect 返回元素的6个值 left top right bottom width height
     
        return {
            x:x-bbox.left*(canvas.width/bbox.width),
            y:y-bbox.top*(canvas.height/bbox.height)
        }
    }

    function saveDrawingSurface(){
        drawingSurfaceImageData=context.getImageData(0,0,canvas.width,canvas.height) 
     //getImageData() 方法返回 ImageData 对象,该对象拷贝了画布指定矩形的像素数据。
    }
    
    function restoreDrawingSurface(){
        context.putImageData(drawingSurfaceImageData,0,0)
        //putImageData() 方法将图像数据(从指定的 ImageData 对象)放回画布上。
    }

   function updateRubberbandRectangle(loc){
       rubberbandRect.width=Math.abs(loc.x-mousedown.x)  //用绝对值求线段的长度
       rubberbandRect.height=Math.abs(loc.y-mousedown.y)  //用绝对值求线段的高度

       if(loc.x>mousedown.x){rubberbandRect.left=mousedown.x}else{
           rubberbandRect.left=loc.x
       }
       if(loc.y>mousedown.y){rubberbandRect.top=mousedown.y}else{
           rubberbandRect.top=loc.y
       }
   }
   function drawRubberbandShape(loc){

       context.beginPath()
       context.moveTo(mousedown.x,mousedown.y)
       context.lineTo(loc.x,loc.y)
       context.stroke()
   }
   function updataRubberband(loc){  //loc是鼠标点在canvas上的坐标集合对象
      // updateRubberbandRectangle(loc)
       drawRubberbandShape(loc)
   }
//这三个函数式辅助线函数
   function drawHorizontalLine(y){
       context.beginPath()
       context.moveTo(0,y+0.5)
       context.lineTo(context.canvas.width,y+0.5)
       context.stroke()
   }
   function drawVerticalLine(x){
       context.beginPath()
       context.moveTo(x+0.5,0)
       context.lineTo(x+0.5,context.canvas.height)
       context.stroke()

   }
   function drawGuidewires(x,y){
       context.save()
       context.strokeStyle="rgba(0,0,230,0.4)"
       context.lineWidth=0.5;
       drawVerticalLine(x)
       drawHorizontalLine(y)
       context.restore()
   }
//这三个函数式辅助线函数
   canvas.onmousedown=function(e){  //只执行一次
     var loc=windowToCanvas(e.clientX,e.clientY); //获取鼠标点在canvas的点坐标
     e.preventDefault();
     saveDrawingSurface()                         //复制canvas画布的像素
     mousedown.x=loc.x;                          //鼠标点击的x轴坐标  这里mousedown记录的是初始位置
     mousedown.y=loc.y;                          //鼠标点击的y轴坐标   这里mousedown记录的是初始位置
     dragging=true;
 }
canvas.onmousemove=function(e){
       var loc;
       if(dragging){
           e.preventDefault()
           loc=windowToCanvas(e.clientX,e.clientY);
           restoreDrawingSurface()
           updataRubberband(loc)  //loc是鼠标点在canvas上的坐标集合对象
           if(guidewires){        //辅助线
               drawGuidewires(loc.x,loc.y)
           }
       }
   }
   canvas.onmouseup=function(e){
       loc=windowToCanvas(e.clientX,e.clientY)
       restoreDrawingSurface()
       updataRubberband(loc)
       dragging=false
   }
   strokeStyleSelect.onchange=function(){

       context.strokeStyle=strokeStyleSelect.value
   }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、简介 HTML5 中的定义:“它是依赖分辨率的位图画布,你可以在 canvas 上面绘制任何图形,甚至加载照片...
    destiny0904阅读 13,626评论 1 4
  • 本章内容 理解 元素 绘制简单的 2D 图形 使用 WebGL 绘制 3D 图形 这个元素负责在页面中设定一个区域...
    闷油瓶小张阅读 4,379评论 0 0
  • Canvas绘图 HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像。 画布是一个矩形...
    shanruopeng阅读 18,573评论 2 14
  • 一、图形的组合方式 globalAlpha是一个介于0和1之间的值(包括0和1),用于指定所有绘制的透明度。默认值...
    空谷悠阅读 5,214评论 0 0
  • 线条样式 绘制直线,第五章知识简单回顾 lineWidth 设置或返回当前的线条宽度,单位为像素 lineCap ...
    Zd_silent阅读 3,418评论 0 0

友情链接更多精彩内容