I'm creating a drawing application using HTML5 canvas.
https://github.com/homanchou/sketchyPad
I can use rgba to control opacity in my line strokes, but how do I achieve a soft feathered brush edge vs a hard circular edge?
I'm creating a drawing application using HTML5 canvas.
https://github.com/homanchou/sketchyPad
I can use rgba to control opacity in my line strokes, but how do I achieve a soft feathered brush edge vs a hard circular edge?
I'm fairly sure that depends on the browser you're using. Last I checked (a while ago - might have changed) Firefox and Chrome do not antialias edges, whereas IE9 does.
Three possible solutions:
You could write your lines into an off-screen canvas, apply a blur filter, and then draw the result into the visible canvas.
If you only use straight line segments, you could use a linear gradient for each line segment. The direction of the gradient must be in an 90" angle compared to the direction of the line segment.
Draw the same lines multiple times at the same place. First with the full width and a low alpha. Then decrease the width and increase the alpha.
Example for using a linear gradient for each line segment:
http://jsfiddle.net/chdh/MmYAt/
Example for drawing a line multiple times, by decreasing width and increasing alpha:
http://jsfiddle.net/chdh/RmtxL/
You can use css filter to blur the canvas. It is possible with SVG rasterization trick. Here's how you do it:
Make two canvases, one on top of another. One of them let's call «Target» and another «Buffer». Buffer is one you draw on and Target is the resulting canvas.
Apply
css-filter: blur(px)
to Buffer canvas so user can instantly see the blurred preview.This is the interesting part. On each stroke (i. e. on mouseup), rasterize the Buffer canvas, put the image into
<svg><foreignObject></foreignObject></svg>
, apply the same CSS filter to it, rasterize SVG, and put the rasterized SVG on Target canvas. Here's gist with code example.Once you have committed your lines to a canvas, you can soften (blur) using the CanvasPixelArray class. It's just a matter of adding your color to neighboring pixels. Here is a good info on pixel manipulation .