2014-03-27 125 views
1

我正在構建一個使用畫布進行繪製的HTML5應用程序,但我無法繪製出平滑的曲線。如何在繪製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

您可以使用二次或貝塞爾曲線http://www.html5canvastutorials.com/tutorials/html5-canvas-paths/ – jOshT

+0

有沒有可能幫我編輯我的代碼? –

+0

[Mimick photoshop/painter在HTML5畫布上平滑繪製?]的可能的副本(http://stackoverflow.com/questions/17877276/mimick-photoshop-painter-smooth-draw-on-html5-canvas) – K3N

回答

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

然而!

我看到你在用戶移動鼠標時正在繪製你的點。

通過點繪製樣條最好在已存點集上完成。

您可能需要重構代碼以將點移動點保存到點[]數組中,然後從這些已保存的點創建樣條線。

+0

謝謝巴德!真棒你可以投我的問題嗎?謝謝! –