From 69a327cd456ee5a49a250c849e7c393b49d9be1d Mon Sep 17 00:00:00 2001 From: Brian Hursey Date: Sat, 28 Nov 2026 16:47:49 +0000 Subject: [PATCH] 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. --- scripts/render-check.mjs | 57 ++++++++++++++++++++++++++++++++ src/gl/masks.ts | 70 +++++++++++++++++++++++++--------------- 2 files changed, 101 insertions(+), 26 deletions(-) diff --git a/scripts/render-check.mjs b/scripts/render-check.mjs index 95d284e..78cecfb 100644 --- a/scripts/render-check.mjs +++ b/scripts/render-check.mjs @@ -271,6 +271,32 @@ try { renderer.render(groupEdits, { width: w, height: h }); const maskGroupIntersectPixel = sampleBand(2); + // Brush masks are source-space and feather softens the rim inside the brush + // circle. The dab below has a radius of 0.08 of the frame width, so it spans + // 0.12 of the frame height; nothing outside that may be touched, even at a + // high feather, and the rim itself must be a gradient rather than a step. + const pixelAt = (fx, fy) => { + const px = new Uint8Array(4); + const py = h - 1 - Math.min(h - 1, Math.floor(fy * h)); + viewGl.readPixels(Math.min(w - 1, Math.floor(fx * w)), py, 1, 1, viewGl.RGBA, viewGl.UNSIGNED_BYTE, px); + return [px[0], px[1], px[2]]; + }; + const dabRadiusY = 0.08 * (w / h); + const brushEdits = (feather) => { + const layer = defaultLocalLayer('brush', 0); + const shape = layer.components[0]; + shape.feather = feather; + shape.brush.strokes = [{ points: [{ x: 0.25, y: 0.2 }], size: 0.08, erase: false }]; + return { ...defaultEdits(), local: [{ ...layer, curve: darkCurve }] }; + }; + renderer.render(brushEdits(0), { width: w, height: h }); + const dabHard = pixelAt(0.25, 0.2); + const dabOutsideHard = pixelAt(0.25, 0.2 + dabRadiusY * 1.15); + renderer.render(brushEdits(80), { width: w, height: h }); + const dabSoft = pixelAt(0.25, 0.2); + const dabRimSoft = pixelAt(0.25, 0.2 + dabRadiusY * 0.75); + const dabOutsideSoft = pixelAt(0.25, 0.2 + dabRadiusY * 1.15); + pre.textContent = 'RESULT ' + JSON.stringify({ clipPixels, clipRed: near(clipPixels[0], RED_MARK), @@ -297,6 +323,17 @@ try { maskGroupSubtractCleared: near(maskGroupSubtractPixel, maskIdentityPixel), maskGroupIntersectPixel, maskGroupIntersectDarkens: maskGroupIntersectPixel[0] < maskIdentityPixel[0] * 0.75, + dabHard, + dabOutsideHard, + dabSoft, + dabRimSoft, + dabOutsideSoft, + // A hard dab covers the core, and even an 80 % feather stops at the rim. + dabCoreDarkened: dabHard[0] < 140, + dabSoftCoreDarkened: dabSoft[0] < 140, + dabRimSmooth: dabRimSoft[0] > 140 && dabRimSoft[0] < 240, + dabBoundedHard: dabOutsideHard[0] > 240, + dabBoundedSoft: dabOutsideSoft[0] > 240, flagHonouredForBitmap: !same(proxyTop, proxyNoFlipTop), proxyTop: +proxyTop.toFixed(4), sidecarTop: +sideTop.toFixed(4), @@ -512,6 +549,26 @@ async function main() { ); return 1; } + console.log( + `render check: brush dab hard=${JSON.stringify(result.dabHard)}` + + ` outsideHard=${JSON.stringify(result.dabOutsideHard)} soft=${JSON.stringify(result.dabSoft)}` + + ` rimSoft=${JSON.stringify(result.dabRimSoft)} outsideSoft=${JSON.stringify(result.dabOutsideSoft)}`, + ); + if ( + !result.dabCoreDarkened || + !result.dabSoftCoreDarkened || + !result.dabRimSmooth || + !result.dabBoundedHard || + !result.dabBoundedSoft + ) { + console.error( + 'render check FAILED: the brush mask does not follow the brush circle ' + + `(hard=${JSON.stringify(result.dabHard)}, rim=${JSON.stringify(result.dabRimSoft)}, ` + + `outsideHard=${JSON.stringify(result.dabOutsideHard)}, ` + + `outsideSoft=${JSON.stringify(result.dabOutsideSoft)}).`, + ); + return 1; + } console.log( 'render check PASSED: proxy and sidecar share a row order and the clipping overlay names the channels.', ); diff --git a/src/gl/masks.ts b/src/gl/masks.ts index 2207030..149d5e1 100644 --- a/src/gl/masks.ts +++ b/src/gl/masks.ts @@ -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);