🖊️ tool-aware cursors on the drawing pad
Browse files at this commitdc6b2a95074d7e55a634e406bf724202d0aca12dParent: 494ba28- feat
- 🖊️ tool-aware cursors on the drawing pad
1 changed file +31 -1
src/components/ui/draw-canvas.tsxmodified+31 -1
| @@ -17,6 +17,23 @@ const EXPORT_MARGIN = 8; | |||
| 17 | // zoom control. Position/size utilities are appended per overlay via cn(). | 17 | // zoom control. Position/size utilities are appended per overlay via cn(). |
| 18 | const OVERLAY_CARD = "absolute z-10 flex gap-1.5 rounded-lg border bg-card/90 p-1.5 shadow-xs backdrop-blur-xs"; | 18 | const OVERLAY_CARD = "absolute z-10 flex gap-1.5 rounded-lg border bg-card/90 p-1.5 shadow-xs backdrop-blur-xs"; |
| 19 | const OVERLAY_BTN = "flex h-9 w-9 items-center justify-center text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"; | 19 | const OVERLAY_BTN = "flex h-9 w-9 items-center justify-center text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"; |
| 20 | +// Per-tool canvas cursors. Pen uses the built-in crosshair; the eraser is a | ||
| 21 | +// lucide-eraser SVG data-URI (black glyph on a white halo so it stays visible | ||
| 22 | +// over any stroke colour), hotspot centred on the block. Pan falls back to | ||
| 23 | +// grab/grabbing in the reactive cursor() below. | ||
| 24 | +// Halo-under-glyph cursor: draw each lucide path once thick-white for contrast, | ||
| 25 | +// then thin-black on top, baked into ONE svg (a cursor value list only picks the | ||
| 26 | +// first url, it can't composite two). Hotspot is per glyph — pen at its writing | ||
| 27 | +// tip, eraser at its centre. | ||
| 28 | +const haloCursor = (glyph: string, hx: number, hy: number) => | ||
| 29 | + `url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke-linecap='round' stroke-linejoin='round'%3E%3Cg stroke='%23fff' stroke-width='4'%3E${glyph}%3C/g%3E%3Cg stroke='%23000' stroke-width='2'%3E${glyph}%3C/g%3E%3C/svg%3E") ${hx} ${hy}`; | ||
| 30 | +// lucide "pencil" — nib points to bottom-left, so the hotspot rides its tip. | ||
| 31 | +const PEN_GLYPH = | ||
| 32 | + "%3Cpath d='M12 20h9'/%3E%3Cpath d='M16.5 3.5a2.12 2.12 0 0 1 3 3L7 19l-4 1 1-4Z'/%3E"; | ||
| 33 | +const PEN_CURSOR = `${haloCursor(PEN_GLYPH, 3, 21)}, crosshair`; | ||
| 34 | +const ERASER_GLYPH = | ||
| 35 | + "%3Cpath d='m7 21-4.3-4.3c-1-1-1-2.5 0-3.4l9.6-9.6c1-1 2.5-1 3.4 0l5.6 5.6c1 1 1 2.5 0 3.4L13 21'/%3E%3Cpath d='M22 21H7'/%3E%3Cpath d='m5 11 9 9'/%3E"; | ||
| 36 | +const ERASER_CURSOR = `${haloCursor(ERASER_GLYPH, 12, 12)}, crosshair`; | ||
| 20 | 37 | ||
| 21 | /** | 38 | /** |
| 22 | * Freehand drawing pad. `props.onSave` receives a PNG `File` that both displays | 39 | * Freehand drawing pad. `props.onSave` receives a PNG `File` that both displays |
| @@ -50,6 +67,8 @@ export function DrawCanvas(props: { | |||
| 50 | // The pad is unbounded — strokes live in world coords; pan is the world→screen | 67 | // The pad is unbounded — strokes live in world coords; pan is the world→screen |
| 51 | // offset in CSS px so you can move around a drawing larger than the viewport. | 68 | // offset in CSS px so you can move around a drawing larger than the viewport. |
| 52 | const [pan, setPan] = createSignal({ x: 0, y: 0 }); | 69 | const [pan, setPan] = createSignal({ x: 0, y: 0 }); |
| 70 | + // Reactive flag for an in-progress pan drag, so the pan cursor flips grab→grabbing. | ||
| 71 | + const [panning, setPanning] = createSignal(false); | ||
| 53 | // View-only magnification: screenCSS = world * zoom + pan. Strokes stay in world | 72 | // View-only magnification: screenCSS = world * zoom + pan. Strokes stay in world |
| 54 | // px, so stroke widths scale with the transform — never multiply them by hand. | 73 | // px, so stroke widths scale with the transform — never multiply them by hand. |
| 55 | const [zoom, setZoom] = createSignal(1); | 74 | const [zoom, setZoom] = createSignal(1); |
| @@ -73,6 +92,15 @@ export function DrawCanvas(props: { | |||
| 73 | return "pen"; | 92 | return "pen"; |
| 74 | }; | 93 | }; |
| 75 | 94 | ||
| 95 | + // Canvas cursor mirrors the active tool: pen → crosshair, eraser → its glyph, | ||
| 96 | + // pan → grab (grabbing mid-drag). Set inline so all three read from one place. | ||
| 97 | + const cursor = () => { | ||
| 98 | + const tool = activeTool(); | ||
| 99 | + if (tool === "pan") return panning() ? "grabbing" : "grab"; | ||
| 100 | + if (tool === "erase") return ERASER_CURSOR; | ||
| 101 | + return PEN_CURSOR; | ||
| 102 | + }; | ||
| 103 | + | ||
| 76 | const redraw = () => { | 104 | const redraw = () => { |
| 77 | const c = context(); | 105 | const c = context(); |
| 78 | if (!c || !canvas) return; | 106 | if (!c || !canvas) return; |
| @@ -187,6 +215,7 @@ export function DrawCanvas(props: { | |||
| 187 | if (e.button === 1) setOverride("pan"); // transient middle-button pan | 215 | if (e.button === 1) setOverride("pan"); // transient middle-button pan |
| 188 | const p = pan(); | 216 | const p = pan(); |
| 189 | panStart = { x: e.clientX, y: e.clientY, px: p.x, py: p.y }; | 217 | panStart = { x: e.clientX, y: e.clientY, px: p.x, py: p.y }; |
| 218 | + setPanning(true); | ||
| 190 | return; | 219 | return; |
| 191 | } | 220 | } |
| 192 | // Right button (2) forces an erase stroke regardless of the selected tool. | 221 | // Right button (2) forces an erase stroke regardless of the selected tool. |
| @@ -216,6 +245,7 @@ export function DrawCanvas(props: { | |||
| 216 | setOverride(null); | 245 | setOverride(null); |
| 217 | if (panStart) { | 246 | if (panStart) { |
| 218 | panStart = undefined; | 247 | panStart = undefined; |
| 248 | + setPanning(false); | ||
| 219 | return; | 249 | return; |
| 220 | } | 250 | } |
| 221 | const done = current; | 251 | const done = current; |
| @@ -369,7 +399,7 @@ export function DrawCanvas(props: { | |||
| 369 | <canvas | 399 | <canvas |
| 370 | ref={canvas} | 400 | ref={canvas} |
| 371 | class="h-full w-full touch-none" | 401 | class="h-full w-full touch-none" |
| 372 | - classList={{ "cursor-grab": activeTool() === "pan" }} | 402 | + style={{ cursor: cursor() }} |
| 373 | onMouseDown={(e) => { | 403 | onMouseDown={(e) => { |
| 374 | // preventDefault in pointerdown does not stop the middle-click autoscroll widget. | 404 | // preventDefault in pointerdown does not stop the middle-click autoscroll widget. |
| 375 | if (e.button === 1) e.preventDefault(); | 405 | if (e.button === 1) e.preventDefault(); |