From e76fdffeebb227526a1bd61749ed7fee2b9883fc Mon Sep 17 00:00:00 2001 From: Jared Pereira Date: Tue, 29 Sep 2026 01:02:55 -0400 Subject: [PATCH] Add a fill tool for closed strokes in drawings Tapping inside a stroke that closes on itself fills it with the current ink color. A stroke counts as closed when it crosses itself or when one of its ends comes back within reach of the rest; shapes built from several strokes don't fill. The fill polygon is stored on the stroke, so erasing the stroke removes it, and is published as a separate fills list painted beneath the strokes. Co-Authored-By: Claude Fable 5.1 --- .../[rkey]/Blocks/StaticDrawingBlock.tsx | 1 + .../Blocks/DrawingBlock/CanvasInkLayer.tsx | 16 +- components/Blocks/DrawingBlock/InkSvg.tsx | 18 +- components/Blocks/DrawingBlock/InkToolbar.tsx | 10 + components/Blocks/DrawingBlock/index.tsx | 4 +- components/Blocks/DrawingBlock/ink.test.ts | 102 ++++++++ components/Blocks/DrawingBlock/ink.ts | 229 +++++++++++++++++- .../Blocks/DrawingBlock/inkMutations.ts | 57 ++++- .../Blocks/DrawingBlock/useInkSession.ts | 14 +- components/Icons/PaintBucketSmall.tsx | 22 ++ lexicons/api/lexicons.ts | 29 +++ .../api/types/pub/leaflet/blocks/drawing.ts | 21 ++ lexicons/pub/leaflet/blocks/drawing.json | 29 +++ lexicons/src/blocks.ts | 24 ++ src/replicache/attributes.ts | 2 + src/utils/factsToPagesRecord.ts | 9 + 16 files changed, 575 insertions(+), 12 deletions(-) create mode 100644 components/Icons/PaintBucketSmall.tsx diff --git a/app/(app)/(published)/lish/[did]/[publication]/[rkey]/Blocks/StaticDrawingBlock.tsx b/app/(app)/(published)/lish/[did]/[publication]/[rkey]/Blocks/StaticDrawingBlock.tsx index 9a80f312..e642b45d 100644 --- a/app/(app)/(published)/lish/[did]/[publication]/[rkey]/Blocks/StaticDrawingBlock.tsx +++ b/app/(app)/(published)/lish/[did]/[publication]/[rkey]/Blocks/StaticDrawingBlock.tsx @@ -11,6 +11,7 @@ export const StaticDrawingBlock = ({ ({ id: String(i), stroke }))} + fills={block.fills?.map((fill, i) => ({ id: String(i), fill }))} /> ); diff --git a/components/Blocks/DrawingBlock/CanvasInkLayer.tsx b/components/Blocks/DrawingBlock/CanvasInkLayer.tsx index 7e38cd56..e8234728 100644 --- a/components/Blocks/DrawingBlock/CanvasInkLayer.tsx +++ b/components/Blocks/DrawingBlock/CanvasInkLayer.tsx @@ -23,6 +23,7 @@ import { DrawingState, commitStroke, eraseStrokes, + fillAt, readDrawing, stopInk, } from "./inkMutations"; @@ -39,6 +40,9 @@ const ERASER_CURSOR = `url("data:image/svg+xml,${encodeURIComponent( type Gesture = { pointerId: number; erase: boolean; + // Fills where the pointer went down once it lifts, so a pinch's first + // finger can still cancel it. + fill: boolean; // Canvas px, flattened x, y, pressure (0-1) triples. points: number[]; simulatePressure: boolean; @@ -124,6 +128,7 @@ export function CanvasInkLayer(props: { pageID: string }) { if (events.length === 0) events = [e.nativeEvent]; for (let ev of events) { let p = toCanvas(ev); + if (g.fill && g.points.length > 0) break; if (g.erase) { eraseAt(g, p); continue; @@ -138,7 +143,7 @@ export function CanvasInkLayer(props: { pageID: string }) { : g.points[g.points.length - 1] ?? 0.5; g.points.push(p.x, p.y, pressure); } - if (!g.erase) setLive(liveStroke(g)); + if (!g.erase && !g.fill) setLive(liveStroke(g)); }; let liveStroke = (g: Gesture): InkStroke => ({ @@ -168,6 +173,14 @@ export function CanvasInkLayer(props: { pageID: string }) { }).then(() => useInkSession.getState().setErasing([])); return; } + if (g.fill) { + fillAt(rep, undoManager, { + page: props.pageID, + point: { x: g.points[0], y: g.points[1] }, + color, + }); + return; + } let key = pendingKey.current++; setLive(null); setPending((p) => [...p, { key, stroke: liveStroke(g) }]); @@ -217,6 +230,7 @@ export function CanvasInkLayer(props: { pageID: string }) { gesture.current = { pointerId: e.pointerId, erase, + fill: tool === "fill" && !erase, points: [], simulatePressure: e.pointerType !== "pen", drawing: diff --git a/components/Blocks/DrawingBlock/InkSvg.tsx b/components/Blocks/DrawingBlock/InkSvg.tsx index 8dad1617..1068d70b 100644 --- a/components/Blocks/DrawingBlock/InkSvg.tsx +++ b/components/Blocks/DrawingBlock/InkSvg.tsx @@ -1,5 +1,12 @@ import { memo } from "react"; -import { InkStroke, ViewBox, inkColor, inkStrokePath } from "./ink"; +import { + InkFill, + InkStroke, + ViewBox, + inkColor, + inkFillPath, + inkStrokePath, +} from "./ink"; // Fills its container's width at the view box's aspect ratio. Strokes past // the view box still show: a collaborator's stroke can land before the view @@ -7,6 +14,8 @@ import { InkStroke, ViewBox, inkColor, inkStrokePath } from "./ink"; export function InkSvg(props: { viewBox: ViewBox; strokes: { id: string; stroke: InkStroke }[]; + // In paint order, beneath the strokes. + fills?: { id: string; fill: InkFill }[]; className?: string; }) { let { x, y, width, height } = props.viewBox; @@ -17,6 +26,13 @@ export function InkSvg(props: { className={`block w-full h-auto ${props.className || ""}`} style={{ aspectRatio: `${width} / ${height}` }} > + {props.fills?.map((f) => ( + + ))} {props.strokes.map((s) => ( ))} diff --git a/components/Blocks/DrawingBlock/InkToolbar.tsx b/components/Blocks/DrawingBlock/InkToolbar.tsx index 5c969825..716c858f 100644 --- a/components/Blocks/DrawingBlock/InkToolbar.tsx +++ b/components/Blocks/DrawingBlock/InkToolbar.tsx @@ -13,6 +13,7 @@ import { thumbStyle } from "components/ThemeManager/Pickers/ColorPicker"; import { Popover } from "components/Popover"; import { Separator } from "components/Layout"; import { EraserSmall } from "components/Icons/EraserSmall"; +import { PaintBucketSmall } from "components/Icons/PaintBucketSmall"; import { PaintSmall } from "components/Icons/PaintSmall"; import { CloseTiny } from "components/Icons/CloseTiny"; import { INK_COLORS, INK_SIZES, inkColor } from "./ink"; @@ -121,6 +122,15 @@ export function InkToolbar(props: { pageID: string }) { ); })} + +