Reputation: 47
I'm building an HTML5 app using canvas for drawing, but I'm having trouble trying to draw a smooth curve.
I'm using moveTo()
in my app and it draws and erases using a mode. How can I change my code so I can get smooth curves? This is my code:
<!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>
Upvotes: 3
Views: 4815
Reputation: 105035
If you want a smooth line to pass through all your points, you're talking about a spline.
Here are several tutorials on how to use splines to draw smooth curves through your data points:
http://scaledinnovation.com/analytics/splines/aboutSplines.html
http://www.codeproject.com/Tips/562175/Draw-Smooth-Lines-on-HTML5-Canvas
However!
I see that you're drawing your points "live" as the user moves the mouse.
Drawing a spline through points is best done on a saved set of points.
You might want to refactor your code to save the mousemove points in a points[] array and then create a spline from those saved points.
Upvotes: 3