Html – Transparency of a filled stroke in HTML5


I'm working on a doodling app in HTML5 and I would like to do a sort of bucket feature. The idea is to draw a path and it will be closed and filled with the selected colour (colour of the stroke). It works quite well with solid colours, but if I want to have a transparent stroke and fill, I run into this problem:

What happens is the fill is done until the middle of the stroke (the actual sampling point of the path) so there's a line of half the size of the stroke inside the shape which is darker because it's the intersection of the fill and the stroke.

You should be able to see what I'm talking about live in this sandbox.

Best Solution

Sure, use ctx.globalCompositeOperation = 'destination-atop'; and it ought to look the way you're expecting.

Like so:

(Take out that line in order to see the problem you're having)

Assuming its not the only thing drawn on canvas, you're probably going to have to draw this to a temporary canvas and then draw that canvas onto your normal one, otherwise it may ruin all the previously drawn shapes. So you'd need a system like so:

edit: code pasted in case of jsfiddle failure:


<canvas id="canvas1" width="500" height="500"></canvas>


var can = document.getElementById('canvas1');
var ctx = can.getContext('2d');

var can2 = document.createElement('canvas');
can2.width = can.width;
can2.height = can.height;
ctx2 = can2.getContext('2d');

ctx.strokeStyle = 'rgba(0,0,0,0.7)';
ctx.fillStyle = 'rgba(0,0,0,0.7)';
ctx.lineWidth = 10;

// Stuff drawn normally before
// Here I draw one rect in the old way just to show the old way
// and show something on the canvas before:

// Draw on can2 then draw can2 to can
ctx2.strokeStyle = 'rgba(0,0,0,0.7)';
ctx2.fillStyle = 'rgba(0,0,0,0.7)';
ctx2.lineWidth = 10;
ctx2.globalCompositeOperation = 'destination-atop';

ctx.drawImage(can2, 0, 0);