在HTML5画布上绘图时如何获得平滑的曲线?

时间:2014-03-27 20:13:44

标签: javascript html5 canvas

我正在使用画布构建HTML5应用程序以进行绘制,但我在尝试绘制平滑曲线方面遇到了麻烦。

我在我的应用中使用moveTo()并使用模式绘制和删除。如何更改代码以便获得平滑的曲线?这是我的代码:

<!doctype html>
<html>
<head>

<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<!--[if lt IE 9]><script type="text/javascript" src="../excanvas.js"></script><![endif]-->

<style>

canvas{background:url(images/note.png)}

</style>

<script>
$(function(){

var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var lastX;
var lastY;

var strokeColor="green";
var strokeWidth=2;
var mouseX;
var mouseY;
var canvasOffset=$("#canvas").offset();
var offsetX=canvasOffset.left;
var offsetY=canvasOffset.top;
var isMouseDown=false;
var image = new Image();
image.src = "https://lh5.googleusercontent.com/-Ks_Ti3x-  QdU/UwvFTB_RqaI/AAAAAAAAANk/dOSa3yoTdX8/w140-h140-p/IMG_0478.JPG";



image.onload = function() {
ctx.drawImage(image, 100, 100);
};

function handleMouseDown(e){
  mouseX=parseInt(e.clientX-offsetX);
  mouseY=parseInt(e.clientY-offsetY);

  // Put your mousedown stuff here
  lastX=mouseX;
  lastY=mouseY;
  isMouseDown=true;
}

function handleMouseUp(e){
  mouseX=parseInt(e.clientX-offsetX);
  mouseY=parseInt(e.clientY-offsetY);

  // Put your mouseup stuff here
  isMouseDown=false;
}

function handleMouseOut(e){
  mouseX=parseInt(e.clientX-offsetX);
  mouseY=parseInt(e.clientY-offsetY);

  // Put your mouseOut stuff here
  isMouseDown=false;
}

function handleMouseMove(e){
  mouseX=parseInt(e.clientX-offsetX);
  mouseY=parseInt(e.clientY-offsetY);

  // Put your mousemove stuff here
  if(isMouseDown){
      ctx.beginPath();
      if(mode=="pen_black"){
          ctx.globalCompositeOperation="source-over";
          ctx.moveTo(lastX,lastY);
          ctx.lineTo(mouseX,mouseY);
          ctx.stroke(); 
          ctx.lineCap = 'round';
          ctx.strokeStyle="#000000"; 



      }
         if(mode=="pen_blue"){
          ctx.globalCompositeOperation="source-over";
          ctx.moveTo(lastX,lastY);
          ctx.lineTo(mouseX,mouseY);
          ctx.stroke(); 
            ctx.lineCap = 'round';
          ctx.strokeStyle="#0000ff"; 
          //ctx.lineWidth = 15; 

      } 
       if(mode=="pen_green"){
          ctx.globalCompositeOperation="source-over";
          ctx.moveTo(lastX,lastY);
          ctx.lineTo(mouseX,mouseY);
          ctx.stroke(); 
            ctx.lineCap = 'round';
          ctx.strokeStyle="#00FF00"; 
          //ctx.lineWidth = 15; 

      } if(mode=="pen_orange"){
          ctx.globalCompositeOperation="source-over";
          ctx.moveTo(lastX,lastY);
          ctx.lineTo(mouseX,mouseY);
          ctx.stroke(); 
            ctx.lineCap = 'round';
          ctx.strokeStyle="#FF7F00"; 
          //ctx.lineWidth = 15; 

      } 
      if(mode=="pen_white"){
          ctx.globalCompositeOperation="source-over";
          ctx.moveTo(lastX,lastY);
          ctx.lineTo(mouseX,mouseY);
          ctx.stroke(); 
            ctx.lineCap = 'round';
          ctx.strokeStyle="#FFFFFF"; 
          //ctx.lineWidth = 15; 

      } 
         if(mode=="pen_red"){
          ctx.globalCompositeOperation="source-over";
          ctx.moveTo(lastX,lastY);
          ctx.lineTo(mouseX,mouseY);
          ctx.stroke(); 
            ctx.lineCap = 'round';
          ctx.strokeStyle="#FF0000"; 
          //ctx.lineWidth = 15; 

      }  

       if(mode=="size1"){
          ctx.globalCompositeOperation="source-over";
          ctx.moveTo(lastX,lastY);
          ctx.lineTo(mouseX,mouseY);
          ctx.stroke(); 
            ctx.lineCap = 'round';
          ctx.lineWidth = 15; 
          ctx.lineJoin = 'round';

      }  
       if(mode=="size2"){
          ctx.globalCompositeOperation="source-over";
          ctx.moveTo(lastX,lastY);
          ctx.lineTo(mouseX,mouseY);
          ctx.stroke(); 
            ctx.lineCap = 'round';
          ctx.lineWidth = 10; 
          ctx.lineJoin = 'round';

      } 
       if(mode=="size3"){
          ctx.globalCompositeOperation="source-over";
          ctx.moveTo(lastX,lastY);
          ctx.lineTo(mouseX,mouseY);
          ctx.stroke(); 
            ctx.lineCap = 'round';
          ctx.lineWidth = 7; 
          ctx.lineJoin = 'round';

      } 
       if(mode=="eraser"){
          ctx.globalCompositeOperation="destination-out";
          ctx.arc(lastX,lastY,5,0,Math.PI*2,false);
           ctx.lineWidth = 10; 
          ctx.fill();
      }


      lastX=mouseX;
      lastY=mouseY;
  }
}

$("#canvas").mousedown(function(e){handleMouseDown(e);});
$("#canvas").mousemove(function(e){handleMouseMove(e);});
$("#canvas").mouseup(function(e){handleMouseUp(e);});
$("#canvas").mouseout(function(e){handleMouseOut(e);});

var mode="pen_black";
$("#pen_black").click(function(){ mode="pen_black"; });
   $("#pen_blue").click(function(){ mode="pen_blue"; });
$("#eraser").click(function(){ mode="eraser"; });
$("#size1").click(function(){ mode="size1"; });
$("#size2").click(function(){ mode="size2"; });
$("#size3").click(function(){ mode="size3"; });

$("#pen_green").click(function(){ mode="pen_green"; });
$("#pen_orange").click(function(){ mode="pen_orange"; });
$("#pen_white").click(function(){ mode="pen_white"; });
$("#pen_red").click(function(){ mode="pen_red"; });




}); // end $(function(){});


function save() {
document.getElementById("canvasimg").style.border = "2px solid";
var dataURL = canvas.toDataURL();
document.getElementById("canvasimg").src = dataURL;
document.getElementById("canvasimg").style.display = "inline";
}


</script>

</head>

<body>
<canvas id="canvas" width=614 height=620></canvas></br>
     <img id="canvasimg"  style="position:absolute;top:1%;left:50%;background:url(images/note.png);"  style="display:none;"></img>
<button id="pen_black">Black</button>
    <button id="pen_blue">Blue</button>
    <button id="pen_orange">Orange</button>
    <button id="pen_green">Green</button>
    <button id="pen_red">Red</button>
    <button id="pen_white">White</button>
    <button id="size1">Size 15</button>
    <button id="size2">Size 10</button>
    <button id="size3">size 7</button>
<button id="eraser">Eraser</button>

 <input type="button" value="save" id="save" size="30" onclick="save()"    style="position:absolute;top:80%;left:5%;">
   <input type="button" value="clear" size="30" onclick="history.go()"    style="position:absolute;top:85%;left:5%;">
</body>
</html>

1 个答案:

答案 0 :(得分:2)

如果你想要一条平滑的线条通过你的所有点,你就是在谈论一个样条线。

以下是有关如何使用样条线在数据点中绘制平滑曲线的几个教程:

http://scaledinnovation.com/analytics/splines/aboutSplines.html

enter image description here

http://www.codeproject.com/Tips/562175/Draw-Smooth-Lines-on-HTML5-Canvas

enter image description here

<强>然而!

我看到你在用户移动鼠标时“实时”绘制你的积分。

通过点绘制样条曲线最好在一组保存的点上完成。

您可能希望重构代码以将mousemove点保存在points []数组中,然后从这些保存的点创建样条线。