I’m working on a drawing application with HTML5 (canvas) which works fine besides that what you draw gets kind of “jagged” and not as smooth as I want it to be. Below is the code; what can be the problem here?
$(function() {
var letsdraw = false;
var theCanvas = document.getElementById('paint');
var ctx = theCanvas.getContext('2d');
theCanvas.width = 420;
theCanvas.height = 300;
var canvasOffset = $('#paint').offset();
$('#paint').mousemove(function(e) {
if (letsdraw === true) {
ctx.lineTo(e.pageX - canvasOffset.left, e.pageY - canvasOffset.top);
ctx.stroke();
}
});
$('#paint').mousedown(function() {
letsdraw = true;
ctx.strokeStyle = 'blue';
ctx.lineWidth = 10;
ctx.lineCap = 'round';
ctx.beginPath();
ctx.moveTo(e.pageX - canvasOffset.left, e.pageY - canvasOffset.top);
});
$(window).mouseup(function() {
letsdraw = false;
});
});
Using
shadowBluryou can make it look better. See this : http://jsfiddle.net/diode/EsYqm/6/ . You have to adjust the blur settings withlineWidth.But for a perfect solution, you will have to develop an algorithm for smoothing line, like this http://courses.engr.illinois.edu/ece390/archive/archive-f2000/mp/mp4/anti.html