我正在使用HTML5 canvas创建一个绘图应用程序。
https://github.com/homanchou/sketchyPad
我可以使用rgba来控制我的线条笔触的不透明度,但是我如何实现一个柔和的羽毛笔刷边缘与一个坚硬的圆形边缘呢?
发布于 2012-01-31 17:51:02
三种可能的解决方案:
对每个线段使用线性渐变的示例:
http://jsfiddle.net/chdh/MmYAt/
function drawSoftLine(x1, y1, x2, y2, lineWidth, r, g, b, a) {
var lx = x2 - x1;
var ly = y2 - y1;
var lineLength = Math.sqrt(lx*lx + ly*ly);
var wy = lx / lineLength * lineWidth;
var wx = ly / lineLength * lineWidth;
var gradient = ctx.createLinearGradient(x1-wx/2, y1+wy/2, x1+wx/2, y1-wy/2);
// The gradient must be defined accross the line, 90° turned compared
// to the line direction.
gradient.addColorStop(0, "rgba("+r+","+g+","+b+",0)");
gradient.addColorStop(0.43, "rgba("+r+","+g+","+b+","+a+")");
gradient.addColorStop(0.57, "rgba("+r+","+g+","+b+","+a+")");
gradient.addColorStop(1, "rgba("+r+","+g+","+b+",0)");
ctx.save();
ctx.beginPath();
ctx.lineWidth = lineWidth;
ctx.strokeStyle = gradient;
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
ctx.restore(); }
通过减小宽度和增加alpha多次绘制一条线的示例:
http://jsfiddle.net/chdh/RmtxL/
function drawSoftLine(x1, y1, x2, y2, lineWidth, r, g, b, a) {
ctx.save();
var widths = [1 , 0.8 , 0.6 , 0.4 , 0.2 ];
var alphas = [0.2 , 0.4 , 0.6 , 0.8 , 1 ];
var previousAlpha = 0;
for (var pass = 0; pass < widths.length; pass++) {
ctx.beginPath();
ctx.lineWidth = lineWidth * widths[pass];
var alpha = a * alphas[pass];
// Formula: (1 - alpha) = (1 - deltaAlpha) * (1 - previousAlpha)
var deltaAlpha = 1 - (1 - alpha) / (1 - previousAlpha)
ctx.strokeStyle = "rgba(" + r + "," + g + "," + b + "," + deltaAlpha + ")";
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
previousAlpha = alpha; }
ctx.restore(); }
发布于 2012-01-25 21:57:12
我敢肯定这取决于你使用的浏览器。上次我检查过了(不久前--可能已经改变了)火狐和Chrome不能消除边缘锯齿,而IE9可以。
发布于 2016-03-09 09:12:34
您可以使用css过滤器来模糊画布。使用SVG rasterization trick可以做到这一点。下面是你如何做到这一点:
css-filter: blur(px)
应用到Buffer画布上,这样用户可以立即看到模糊的预览。<svg><foreignObject></foreignObject></svg>
,应用相同的CSS过滤器,光栅化SVG,并将光栅化的SVG放在目标画布上。这是gist with code example.https://stackoverflow.com/questions/9010835
复制