import { For, Match, Show, Switch, createEffect, createSignal, onCleanup, onMount, untrack } from "solid-js"; import { Button } from "@/components/ui/button"; import { IconDownload, IconEdit, IconEraser, IconGrid, IconMove, IconRuled, IconSquareOff, IconTrash, IconUndo, IconZoomIn, IconZoomOut } from "@/components/ui/icons"; import { cn } from "@/lib/cn"; import { PAPER_CELL, PAPER_LINE, canvasPx, paintStroke, paintTip, strokesBounds, type BgKind, type DrawScene, type Point, type Stroke } from "@/lib/draw-stroke"; import { canvasToImageBlob, sceneToPngFile } from "@/lib/drawing-file"; import { useT } from "@/stores/preferences-context"; const COLORS = ["#1f2937", "#dc2626", "#2563eb", "#16a34a", "#ca8a04"]; const WIDTHS = [2, 6, 14]; const ZOOM_MIN = 0.25; const ZOOM_MAX = 4; const ZOOM_STEP = 1.25; // Whitespace kept around the drawn region when exporting the bounding box. const EXPORT_MARGIN = 8; // Floating glass overlays that live inside the canvas, matching the bottom-right // zoom control. Position/size utilities are appended per overlay via cn(). const OVERLAY_CARD = "absolute z-10 flex gap-1.5 rounded-lg border bg-card/90 p-1.5 shadow-xs backdrop-blur-xs"; const OVERLAY_BTN = "flex h-9 w-9 items-center justify-center text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"; // Per-tool canvas cursors. Pen uses the built-in crosshair; the eraser is a // lucide-eraser SVG data-URI (black glyph on a white halo so it stays visible // over any stroke colour), hotspot centred on the block. Pan falls back to // grab/grabbing in the reactive cursor() below. // Halo-under-glyph cursor: draw each lucide path once thick-white for contrast, // then thin-black on top, baked into ONE svg (a cursor value list only picks the // first url, it can't composite two). Hotspot is per glyph — pen at its writing // tip, eraser at its centre. const haloCursor = (glyph: string, hx: number, hy: number) => `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}`; // lucide "pencil" — nib points to bottom-left, so the hotspot rides its tip. const PEN_GLYPH = "%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"; const PEN_CURSOR = `${haloCursor(PEN_GLYPH, 3, 21)}, crosshair`; const ERASER_GLYPH = "%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"; const ERASER_CURSOR = `${haloCursor(ERASER_GLYPH, 12, 12)}, crosshair`; /** * Freehand drawing pad. `props.onSave` receives a PNG `File` that both displays * as a normal image and carries its editable stroke JSON in a tEXt chunk, so the * same File feeds every existing upload seam (`postNoteFile`, * `postExamQuestionImage`) and a saved drawing can be reopened via `initialScene`. * PNG/JPEG buttons additionally download a flat image straight to the device. */ export function DrawCanvas(props: { onSave: (file: File) => void; initialScene?: DrawScene | null; fileName?: string; pending?: boolean; class?: string; }) { const t = useT(); let canvas: HTMLCanvasElement | undefined; let wrap: HTMLDivElement | undefined; let current: Stroke | undefined; // Live pan drag: pointer origin + pan value at grab, so the pad follows the finger. let panStart: { x: number; y: number; px: number; py: number } | undefined; const [strokes, setStrokes] = createSignal([]); const [color, setColor] = createSignal(COLORS[0]); const [width, setWidth] = createSignal(WIDTHS[1]); const [erasing, setErasing] = createSignal(false); const [hand, setHand] = createSignal(false); // Transient tool while a non-left button is held (middle = pan, right = erase), // so the toolbar highlights it as if clicked and reverts on release. const [override, setOverride] = createSignal(null); // The pad is unbounded — strokes live in world coords; pan is the world→screen // offset in CSS px so you can move around a drawing larger than the viewport. const [pan, setPan] = createSignal({ x: 0, y: 0 }); // Reactive flag for an in-progress pan drag, so the pan cursor flips grab→grabbing. const [panning, setPanning] = createSignal(false); // View-only magnification: screenCSS = world * zoom + pan. Strokes stay in world // px, so stroke widths scale with the transform — never multiply them by hand. const [zoom, setZoom] = createSignal(1); // Notebook paper, drawn as CSS on the wrapper (never in redraw(), or the eraser // would cut it). New pads default to squared grid — the look the pad is for. const [bg, setBg] = createSignal("grid"); // Which floating picker is open (pen/eraser line-thickness, or paper style). // Driven by JS, not CSS :focus-within — a mouse click keeps focus on the trigger // and the canvas isn't focusable to blur it, so focus-within stayed stuck open. const [openMenu, setOpenMenu] = createSignal(null); const ratio = () => window.devicePixelRatio || 1; const context = () => canvas?.getContext("2d") ?? null; // Transient override wins over the persistent tool so the toolbar reflects a held button. const activeTool = (): "pen" | "erase" | "pan" => { const o = override(); if (o) return o; if (hand()) return "pan"; if (erasing()) return "erase"; return "pen"; }; // Canvas cursor mirrors the active tool: pen → crosshair, eraser → its glyph, // pan → grab (grabbing mid-drag). Set inline so all three read from one place. const cursor = () => { const tool = activeTool(); if (tool === "pan") return panning() ? "grabbing" : "grab"; if (tool === "erase") return ERASER_CURSOR; return PEN_CURSOR; }; const redraw = () => { const c = context(); if (!c || !canvas) return; const scale = ratio(); // Clear the whole backing store in device pixels, then shift into world space // by the pan offset so off-origin strokes render where the pan puts them. c.setTransform(scale, 0, 0, scale, 0, 0); c.clearRect(0, 0, canvas.width / scale, canvas.height / scale); const p = pan(); const z = zoom(); c.setTransform(z * scale, 0, 0, z * scale, p.x * scale, p.y * scale); for (const stroke of strokes()) paintStroke(c, stroke); if (current) paintStroke(c, current); }; // The backing store is DPR-scaled; without this strokes land blurry and offset. const resize = () => { if (!canvas) return; const rect = canvas.getBoundingClientRect(); if (rect.width === 0 || rect.height === 0) return; const scale = ratio(); canvas.width = Math.round(rect.width * scale); canvas.height = Math.round(rect.height * scale); redraw(); }; onMount(() => { resize(); const observer = new ResizeObserver(() => resize()); if (wrap) observer.observe(wrap); // Close any open flyout on a pointerdown outside every picker group, or Escape. // The canvas isn't focusable, so nothing would otherwise dismiss it. const onDocDown = (e: PointerEvent) => { if (!(e.target as HTMLElement | null)?.closest("[data-draw-menu]")) setOpenMenu(null); }; const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") setOpenMenu(null); }; document.addEventListener("pointerdown", onDocDown); document.addEventListener("keydown", onKey); onCleanup(() => { observer.disconnect(); document.removeEventListener("pointerdown", onDocDown); document.removeEventListener("keydown", onKey); }); }); // Load (or reload) an edited scene whenever the caller supplies one — including // while the pad is already open. No rescale: the pad is unbounded, so rather than // shrinking a big drawing to fit we keep it 1:1 and pan its content into the // corner (you move around it from there). untrack keeps redraw's read of // strokes() from turning this into a self-triggering loop. let lastLoadedScene: DrawScene | null | undefined = Symbol("uninitialized") as any; createEffect(() => { const scene = props.initialScene; if (scene === lastLoadedScene) return; lastLoadedScene = scene; untrack(() => { const next = scene ? scene.strokes : []; setStrokes(next); setBg(scene?.bg ?? "grid"); // default grid background for new/edited drawings setZoom(1); // a reopened drawing starts at 100% const b = strokesBounds(next); setPan(b ? { x: EXPORT_MARGIN - b.x, y: EXPORT_MARGIN - b.y } : { x: 0, y: 0 }); redraw(); }); }); const pointAt = (e: PointerEvent): Point => { const rect = canvas!.getBoundingClientRect(); const p = pan(); const z = zoom(); return { x: (e.clientX - rect.left - p.x) / z, y: (e.clientY - rect.top - p.y) / z }; }; // Zoom around the pad center: keep the world point under the center fixed so // content doesn't fly off. Clamped to [ZOOM_MIN, ZOOM_MAX]. // ponytail: no pinch-zoom; buttons + wheel cover it, add 2-pointer tracking if touch users ask const zoomBy = (factor: number) => { if (!canvas) return; const rect = canvas.getBoundingClientRect(); const cx = rect.width / 2; const cy = rect.height / 2; const p = pan(); const z0 = zoom(); const z1 = Math.min(ZOOM_MAX, Math.max(ZOOM_MIN, z0 * factor)); if (z1 === z0) return; const wx = (cx - p.x) / z0; const wy = (cy - p.y) / z0; setZoom(z1); setPan({ x: cx - wx * z1, y: cy - wy * z1 }); redraw(); }; // Snap back to 100% while keeping the centered content centered (no-op at 1). const resetZoom = () => zoomBy(1 / zoom()); const drawTip = (stroke: Stroke) => { const c = context(); if (c) paintTip(c, stroke); }; const startStroke = (e: PointerEvent) => { if (props.pending) return; e.preventDefault(); canvas?.setPointerCapture(e.pointerId); // Middle button (1) always pans; the hand tool pans on any button EXCEPT right, // because right button always erases (see below). button is the real value only // on pointerdown; -1 during move, which is why panStart/current carry the state. if (e.button === 1 || (hand() && e.button !== 2)) { if (e.button === 1) setOverride("pan"); // transient middle-button pan const p = pan(); panStart = { x: e.clientX, y: e.clientY, px: p.x, py: p.y }; setPanning(true); return; } // Right button (2) forces an erase stroke regardless of the selected tool. const eraseNow = erasing() || e.button === 2; if (e.button === 2) setOverride("erase"); // transient right-button erase current = { color: color(), width: eraseNow ? width() * 2.5 : width(), erase: eraseNow, points: [pointAt(e)], }; drawTip(current); }; const extendStroke = (e: PointerEvent) => { if (panStart) { setPan({ x: panStart.px + (e.clientX - panStart.x), y: panStart.py + (e.clientY - panStart.y) }); redraw(); return; } if (!current) return; current.points.push(pointAt(e)); drawTip(current); }; const endStroke = () => { setOverride(null); if (panStart) { panStart = undefined; setPanning(false); return; } const done = current; current = undefined; if (!done) return; setStrokes((all) => [...all, done]); redraw(); // swap the live polyline for its smoothed replay }; const undo = () => { setStrokes((all) => all.slice(0, -1)); redraw(); }; const clear = () => { setStrokes([]); redraw(); }; // Editing lets you clear a drawing and save the blank (persist the erase); a // brand-new untouched pad has nothing to save. const canSave = () => strokes().length > 0 || props.initialScene != null; // Render only the drawn region (not the viewport) onto an offscreen DPR-scaled // canvas, and produce the matching scene with strokes shifted into that box. // This is what makes the pad "infinite": export follows the content wherever it // was drawn or panned to, instead of capturing what happens to be on screen. const renderBounds = (): { canvas: HTMLCanvasElement; scene: DrawScene } | null => { const list = strokes(); const b = strokesBounds(list); if (!b) return null; const scale = ratio(); const w = Math.ceil(b.w + EXPORT_MARGIN * 2); const h = Math.ceil(b.h + EXPORT_MARGIN * 2); const dx = EXPORT_MARGIN - b.x; const dy = EXPORT_MARGIN - b.y; const off = document.createElement("canvas"); off.width = canvasPx(w, scale); off.height = canvasPx(h, scale); const oc = off.getContext("2d"); if (!oc) return null; oc.setTransform(scale, 0, 0, scale, dx * scale, dy * scale); for (const s of list) paintStroke(oc, s); const shifted = list.map((s) => ({ ...s, points: s.points.map((p) => ({ x: p.x + dx, y: p.y + dy })) })); // dpr rides along: the PNG is baked at this ratio, so a viewer on a different // display can size the replay to the same pixels instead of its own ratio. const scene: DrawScene = { v: 1, w, h, strokes: shifted, bg: bg(), dpr: scale }; return { canvas: off, scene }; }; const save = async () => { if (!canSave()) return; const name = props.fileName ?? "drawing.png"; const rendered = renderBounds(); if (rendered) { props.onSave(await sceneToPngFile(rendered.canvas, rendered.scene, name)); return; } // Cleared while editing an existing drawing: save a 1×1 blank to persist the erase. const blank = document.createElement("canvas"); blank.width = 1; blank.height = 1; props.onSave(await sceneToPngFile(blank, { v: 1, w: 1, h: 1, strokes: [], bg: bg() }, name)); }; const baseName = () => (props.fileName ?? "drawing").replace(/(\.hzdraw)?\.[a-z0-9]+$/i, ""); const download = async (mime: string, ext: string) => { const rendered = renderBounds(); if (!rendered) return; const blob = await canvasToImageBlob(rendered.canvas, mime, bg()); if (!blob) return; const url = URL.createObjectURL(blob); const link = document.createElement("a"); link.href = url; link.download = `${baseName()}.${ext}`; link.click(); URL.revokeObjectURL(url); }; // Live ruling via CSS gradients so lines pan and zoom WITH the content: world // cell PAPER_CELL → PAPER_CELL*zoom on screen, a world-origin line sits at pan. // (Export bakes its own ruling; the two aren't pixel-identical, and needn't be.) const paperStyle = () => { const kind = bg(); if (kind === "none") return {}; const cell = PAPER_CELL * zoom(); const p = pan(); if (kind === "grid") { return { "background-image": `linear-gradient(to right, ${PAPER_LINE} 1px, transparent 1px), linear-gradient(to bottom, ${PAPER_LINE} 1px, transparent 1px)`, "background-size": `${cell}px ${cell}px`, "background-position": `${p.x}px ${p.y}px`, }; } return { "background-image": `linear-gradient(to bottom, ${PAPER_LINE} 1px, transparent 1px)`, "background-size": `100% ${cell}px`, "background-position": `0 ${p.y}px`, }; }; const bgLabel = () => (bg() === "none" ? t("draw.bgNone") : bg() === "lines" ? t("draw.bgLines") : t("draw.bgGrid")); const toolClass = (active: boolean) => cn( "flex h-11 w-11 items-center justify-center rounded-lg border transition-colors", active ? "border-primary bg-primary text-primary-foreground shadow-xs" : "border-border bg-background text-muted-foreground hover:bg-accent hover:text-foreground", ); // Line-thickness flyout shared by the pen and eraser triggers (width is one // shared signal, so both open the same picker). A JSX-returning local, not a // second exported component — one file, one component. Visibility is driven by // openMenu() (see the pointer/click handlers on each trigger); the pt-1.5 wrapper // bridges the gap so the mouse can travel trigger→flyout without pointerleave // firing. z-20 sits it above the sibling islands. const widthPicker = (id: "pen" | "eraser") => (
{(size) => ( )}
); return (
{ // preventDefault in pointerdown does not stop the middle-click autoscroll widget. if (e.button === 1) e.preventDefault(); }} onContextMenu={(e) => e.preventDefault()} onPointerDown={startStroke} onPointerMove={extendStroke} onPointerUp={endStroke} onPointerCancel={endStroke} onWheel={(e) => { e.preventDefault(); zoomBy(e.deltaY < 0 ? ZOOM_STEP : 1 / ZOOM_STEP); }} />

{t("draw.hint")}

{/* Top-left island — tools only: pen, eraser, pan. Pen and eraser each reveal the shared line-thickness flyout (open on hover/tap, toggled via openMenu()); pan has no flyout. */}
{ if (e.pointerType !== "touch") setOpenMenu("pen"); }} onPointerLeave={(e) => { if (e.pointerType !== "touch") setOpenMenu(null); }} > {widthPicker("pen")}
{ if (e.pointerType !== "touch") setOpenMenu("eraser"); }} onPointerLeave={(e) => { if (e.pointerType !== "touch") setOpenMenu(null); }} > {widthPicker("eraser")}
{/* Top-middle island — colour swatches */}
{(swatch) => ( )}
{/* Top-right island — paper selector. The trigger shows the current paper; hover/tap toggles the none/lines/grid flyout via openMenu(). The card is already `absolute` (its own containing block), so the flyout positions off it without a `relative` wrapper. */}
{ if (e.pointerType !== "touch") setOpenMenu("paper"); }} onPointerLeave={(e) => { if (e.pointerType !== "touch") setOpenMenu(null); }} > {/* Paper flyout — opens downward, right-aligned; pt-1.5 bridges the gap so the mouse can travel trigger→flyout without pointerleave firing. */}
{/* Bottom-left history */}
); }