I'm trying to allow a user to draw on Canvas with a tablet, which provides me with a 'pressure' parameter. I use this parameter to pick a width for the line. This works fine for opaque lines, but if the line is translucent, the 'ends' of each segment overlap, creating a circle of darker tone than the line is intended to be. How can I get this to be consistent throughout? Changing the end type leaves jagged edges. I tried using a global alpha instead of applying the transparency on the color, but it yields the same result. My next thing to try is have the drawing occur into a hidden canvas with full opacity, and use that as a mask to be applied to another canvas with the appropriate translucent color, but that seems like a lot of work to do something that I'd assumed the API supports natively.
Any thoughts?
// 1.
// Varying line width, stroking each piece of line separately
var ctx = document.getElementById('canvas1').getContext('2d');
const c = '#ff000055'
ctx.color = c
ctx.strokeStyle = c
ctx.fillColor = c
ctx.lineCap = 'round'
var points = [null, null, null, null];
for (var i = -1; i < 25; i = i + 1) {
var width = 0.5 + i * 2;
var m = 200;
var x = Math.cos(i / 4) * 180;
var y = Math.sin(i / 4) * 140;
points[0] = points[1];
points[1] = points[2];
points[2] = {
X: x,
Y: y
};
if (points[0] == null)
continue;
var p0 = points[0];
var p1 = points[1];
var p2 = points[2];
var x0 = (p0.X + p1.X) / 2;
var y0 = (p0.Y + p1.Y) / 2;
var x1 = (p1.X + p2.X) / 2;
var y1 = (p1.Y + p2.Y) / 2;
ctx.beginPath();
ctx.lineWidth = width;
ctx.moveTo(m + x0, m + y0);
ctx.quadraticCurveTo(m + p1.X, m + p1.Y, m + x1, m + y1);
ctx.stroke();
}
<canvas id="canvas1" width="400" height="400" style="border: 1px solid black;"></canvas>