Draw brush masks with a soft dab inside the brush circle.

Feathering blurred a copy of the stroke and unioned it with the hard
stroke, so coverage stayed at full strength across the whole stroke and
then continued as a halo past the brush outline. The adjustment visibly
spilled outside the brush circle and its rim read as a step rather than a
falloff.

Each stroke is now stamped with a dab that is solid out to 1 - feather of
its radius and fades to nothing at the rim, so the mask follows the brush
circle and softens smoothly at any size. The render gate measures the
dab's core, its rim and the area just outside the circle.
This commit is contained in:
2026-11-28 16:47:49 +00:00
parent 5c2903eeef
commit 69a327cd45
2 changed files with 101 additions and 26 deletions
+44 -26
View File
@@ -113,28 +113,55 @@ function drawLinear(ctx: CanvasRenderingContext2D, component: LocalComponent, w:
ctx.fillRect(0, 0, w, h);
}
/**
* One brush dab: white, solid out to `1 - feather` of the radius and fading to
* nothing at the rim. Feather is the width of that band, so a stroke softens
* inside the brush outline instead of spreading a halo past it.
*/
function brushDab(feather: number) {
const size = 64;
const canvas = makeCanvas(size, size);
const ctx = canvas.getContext('2d');
if (!ctx) return canvas;
const r = size / 2;
// A hair of softness even at 0 keeps the rim from aliasing on screen.
const soft = Math.max(0.02, Math.min(1, feather / 100));
const gradient = ctx.createRadialGradient(r, r, r * (1 - soft), r, r, r);
gradient.addColorStop(0, 'rgba(255,255,255,1)');
gradient.addColorStop(0.45, 'rgba(255,255,255,0.88)');
gradient.addColorStop(1, 'rgba(255,255,255,0)');
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, size, size);
return canvas;
}
/**
* Stamps the dab along the path. A stroked line can only have a hard edge, so
* the stroke is drawn as overlapping dabs: the rim is the dab's falloff, which
* keeps the mask inside the brush circle and smooth at every size.
*/
function drawBrush(ctx: CanvasRenderingContext2D, component: LocalComponent, w: number, h: number) {
ctx.lineCap = 'round';
ctx.lineJoin = 'round';
const dab = brushDab(component.feather);
for (const stroke of component.brush.strokes) {
if (!stroke.points.length) continue;
const width = Math.max(1, stroke.size * 2 * w);
const radius = Math.max(1, stroke.size * w);
const stamp = (x: number, y: number) =>
ctx.drawImage(dab, x - radius, y - radius, radius * 2, radius * 2);
ctx.globalCompositeOperation = stroke.erase ? 'destination-out' : 'source-over';
ctx.strokeStyle = 'rgba(255,255,255,1)';
ctx.fillStyle = 'rgba(255,255,255,1)';
ctx.lineWidth = width;
if (stroke.points.length === 1) {
ctx.beginPath();
ctx.arc(stroke.points[0].x * w, stroke.points[0].y * h, width / 2, 0, Math.PI * 2);
ctx.fill();
continue;
}
ctx.beginPath();
ctx.moveTo(stroke.points[0].x * w, stroke.points[0].y * h);
const first = stroke.points[0];
stamp(first.x * w, first.y * h);
// Close enough that consecutive dabs overlap well inside the soft band.
const spacing = Math.max(0.75, radius * 0.35);
for (let i = 1; i < stroke.points.length; i++) {
ctx.lineTo(stroke.points[i].x * w, stroke.points[i].y * h);
const a = stroke.points[i - 1];
const b = stroke.points[i];
const dx = (b.x - a.x) * w;
const dy = (b.y - a.y) * h;
const steps = Math.max(1, Math.ceil(Math.hypot(dx, dy) / spacing));
for (let step = 1; step <= steps; step++) {
stamp(a.x * w + (dx * step) / steps, a.y * h + (dy * step) / steps);
}
}
ctx.stroke();
}
ctx.globalCompositeOperation = 'source-over';
}
@@ -206,16 +233,7 @@ export function buildLocalMasks(layers: LocalLayer[], srcW: number, srcH: number
drawComponent(ctx, component, w, h);
let source = canvas;
if (component.kind === 'brush') {
// Feather follows the brush, not the frame: 100% softens the edge by
// roughly three quarters of the stroke's half width, so a small dab and a
// wide sweep both feel the same under the slider.
let widest = 0;
for (const stroke of component.brush.strokes) {
widest = Math.max(widest, stroke.size * 2 * w);
}
source = featherMask(canvas, (component.feather / 100) * widest * 0.75);
} else if (component.kind === 'raster') {
if (component.kind === 'raster') {
// Generated masks are low-resolution coverage, so their feather is tied
// to the frame width and only ever loosens the outside edge.
source = featherMask(canvas, ((component.feather / 100) * w) / 16);