我正在使用HTML5(画布)的绘图应用程序工作正常,除了你绘制的东西有点“锯齿”而不像我想要的那样平滑。以下是代码;这可能是什么问题?
$(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;
});
});
答案 0 :(得分:2)
使用shadowBlur
可以让它看起来更好。见:http://jsfiddle.net/diode/EsYqm/6/。您必须使用lineWidth
调整模糊设置。
但是对于一个完美的解决方案,你必须开发一种平滑线的算法,比如http://courses.engr.illinois.edu/ece390/archive/archive-f2000/mp/mp4/anti.html