asafefe.dev
Lab

Lab/14

Draw Pad

Strokes are smoothed with curves between midpoints and drawn at device pixel ratio, so lines stay crisp. Pen pressure is supported.

  • Canvas
  • Tool
Draw anywhere
draw here

Installation

Add Draw Pad to your project with the shadcn CLI. The files land in components/lab, ready to edit.

$ pnpm dlx shadcn@latest add @soultel/lab-draw-pad

Or by URL: https://asafefe.dev/r/lab-draw-pad.json

Brings along

  • utils
  • @soultel/lab-canvas

Usage

example.tsx12 lines
import { useRef } from "react"
import { DrawPad, type DrawPadHandle } from "@/components/lab/draw-pad"

export function Example() {
  const pad = useRef<DrawPadHandle>(null)
  return (
    <div>
      <DrawPad ref={pad} className="h-80 rounded-xl border" />
      <button onClick={() => pad.current?.download("sketch.png")}>Save PNG</button>
    </div>
  )
}

Props

PropTypeDefaultDescription
refRef<DrawPadHandle>-clear(), toDataURL(), and download(filename?).
colorstring"var(--foreground)"Brush color.
sizenumber4Brush width in px.
pressurebooleantrueVary the width with pen pressure.
exportBackgroundstring"#ffffff"Fill behind exports. "transparent" keeps alpha.
placeholderReactNode"draw here"Shown while empty.
onEmptyChange(empty: boolean) => void-Called when the pad gets or loses a drawing.