GooseUI

Components

Signature Pad

New

A field to sign with a mouse, a finger or a stylus. Strokes are smoothed with quadratic curves, the canvas is sharp on high-density screens and repaints itself after a resize. By default the ink takes the current text color, so it reads well in light and dark themes. Methods on ref clear the pad, undo the last stroke and export an image, optionally trimmed to the signature.

Preview

Sign in the box, then save it: the export is trimmed and drawn with dark ink.

Installation

npx shadcn@latest add @gooseui/signature-pad
pnpm dlx shadcn@latest add @gooseui/signature-pad
yarn dlx shadcn@latest add @gooseui/signature-pad
bunx --bun shadcn@latest add @gooseui/signature-pad

Usage

import { Button } from "@/components/ui/button"

import { useRef } from "react"
import { SignaturePad, type SignaturePadHandle } from "@/components/ui/signature-pad"

export default function Example() {
  const pad = useRef<SignaturePadHandle>(null)

  const save = async () => {
    if (!pad.current || pad.current.isEmpty()) return
    const blob = await pad.current.toBlob({ trim: true })
    // upload the blob
  }

  return (
    <>
      <SignaturePad ref={pad} />
      <Button onClick={() => pad.current?.undo()}>Undo</Button>
      <Button onClick={() => pad.current?.clear()}>Clear</Button>
      <Button onClick={save}>Save</Button>
    </>
  )
}

Examples

Pen color and width

Without penColor the ink follows the text color, so a text-* class is enough. penColor sets a fixed color, penWidth the line width in CSS pixels.

<SignaturePad penWidth={4} showGuide={false} className="h-36 text-primary" />
<SignaturePad penColor="#2563eb" penWidth={1.5} className="h-36" />

Callbacks

onBegin and onEnd fire when a stroke starts and ends. onChange reports whether the pad is empty and how many strokes it holds, which is handy for enabling a submit button.

Waiting for the pen · Strokes: 0
import { Button } from "@/components/ui/button"

const [count, setCount] = useState(0)

<SignaturePad
  onBegin={() => setDrawing(true)}
  onEnd={() => setDrawing(false)}
  onChange={({ strokeCount }) => setCount(strokeCount)}
/>
<Button disabled={count === 0}>Confirm</Button>

Props

PropTypeDefaultDescription
refRef<SignaturePadHandle>-Gives access to the methods below
penColorstringcurrentColorInk color. Follows the text color when omitted
penWidthnumber2.5Line width in CSS pixels
disabledbooleanfalseTurns off drawing
showGuidebooleantrueShow the guide line and the placeholder
onChange({ isEmpty, strokeCount }) => void-Called after a stroke, undo or clear
onBegin() => void-Called when a stroke starts
onEnd() => void-Called when a stroke ends
labels{ canvas?: string; placeholder?: string }-Texts: canvas name for screen readers and the placeholder
classNamestring-Additional CSS classes. Height is h-48 by default

Ref methods

MethodReturnsDescription
clear()voidRemoves all strokes
undo()voidRemoves the last stroke
isEmpty()booleanWhether there is nothing drawn
toDataURL(options?)stringImage as a data URL
toBlob(options?)Promise<Blob | null>Image as a Blob, null if the browser could not encode it
getStrokes()SignatureStroke[]A copy of the recorded strokes in CSS pixels

Export options

toDataURL and toBlob take the same options: trim crops empty space around the signature (padding keeps a margin), color repaints the ink, background fills the image, type and quality choose the format, scale sets pixel density.

pad.current?.toDataURL({
  trim: true,
  padding: 8,
  color: "#111827",
  background: "#ffffff",
  type: "image/jpeg",
  quality: 0.9,
  scale: 2,
})

Features

  • Pointer Events: one handler for mouse, touch and stylus, page scroll is blocked only over the canvas
  • Sharp on any screen: the canvas buffer follows devicePixelRatio and the element size
  • Vector history: strokes are stored as points, so resize, theme switch and export redraw them cleanly
  • Exact trim: the crop box is computed from the points, not by scanning pixels