← Back to Home

Signature Pad

A canvas-based signature component with smooth drawing, velocity-based stroke width, and multiple export formats. Uses perfect-freehand for natural pen simulation.

Installation

npm install @thomarket/signature-pad

Usage Example

import { useRef } from 'react';
import { SignaturePad, SignaturePadRef } from '@thomarket/signature-pad';

function MyComponent() {
  const signaturePadRef = useRef<SignaturePadRef>(null);

  const handleClear = () => {
    signaturePadRef.current?.clear();
  };

  const handleExport = async () => {
    const dataUrl = signaturePadRef.current?.toDataURL('png');
    console.log(dataUrl);

    // Or export as SVG
    const svg = signaturePadRef.current?.toSVG();

    // Or as Blob for file upload
    const blob = await signaturePadRef.current?.toBlob('png');
  };

  return (
    <div>
      <SignaturePad
        ref={signaturePadRef}
        width={400}
        height={200}
        penColor="#000000"
        minStrokeWidth={1}
        maxStrokeWidth={4}
        velocitySensitivity={0.5}
        backgroundColor="#ffffff"
        showGuideLines={true}
        onBegin={() => console.log('Started signing')}
        onChange={(isEmpty) => console.log('Empty:', isEmpty)}
        onEnd={() => console.log('Finished stroke')}
      />

      <div className="flex gap-2 mt-4">
        <button onClick={() => signaturePadRef.current?.undo()}>
          Undo
        </button>
        <button onClick={() => signaturePadRef.current?.redo()}>
          Redo
        </button>
        <button onClick={handleClear}>Clear</button>
        <button onClick={handleExport}>Export</button>
      </div>
    </div>
  );
}

// Keyboard shortcuts:
// - Cmd/Ctrl + Z: Undo
// - Cmd/Ctrl + Shift + Z: Redo

Live Demo

Configuration:
Pen Color
Guide Lines
Background
Min Width: 1
Max Width: 4
Velocity Sensitivity: 0.5
Keyboard: Cmd/Ctrl+Z to undo, Cmd/Ctrl+Shift+Z to redo

Supports mouse, touch, and stylus input. Stroke width varies based on drawing velocity for a natural handwriting feel.