React Signature Canvas

1.1.0-alpha.2 · active · verified Sun Apr 19

react-signature-canvas is an unopinionated React wrapper component for the `signature_pad` library, enabling users to add drawing and signature capture capabilities to their React applications. The library aims for a lightweight integration, providing a canvas element without imposing additional styling or wrapping elements. The latest stable version is `1.0.7`, which includes support for React 19 in its peer dependencies. Development is ongoing with `1.1.0-alpha.2` as the latest pre-release, which features a native TypeScript rewrite while maintaining backward compatibility for its API surface. The project boasts 100% test coverage, offers a comprehensive API for canvas manipulation, and allows direct passing of props to the underlying HTML canvas element. It distinguishes itself by its commitment to being a thin, highly configurable wrapper, actively maintained with modern web development practices and full TypeScript support.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to render the `SignatureCanvas` component, customize its pen color and canvas properties, and interact with its API methods like `clear()` and `toDataURL()` using React refs.

import React, { useRef, useEffect } from 'react';
import { createRoot } from 'react-dom/client';
import SignatureCanvas from 'react-signature-canvas';

const App = () => {
  const sigCanvas = useRef(null);

  useEffect(() => {
    if (sigCanvas.current) {
      sigCanvas.current.clear(); // Clear the canvas on mount or some condition
    }
  }, []);

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

  const handleSave = () => {
    // Get data URL or other signature data
    if (!sigCanvas.current.isEmpty()) {
      console.log(sigCanvas.current.toDataURL());
    }
  };

  return (
    <div>
      <h1>Draw Your Signature</h1>
      <SignatureCanvas
        ref={sigCanvas}
        penColor='green'
        canvasProps={{ width: 500, height: 200, className: 'sigCanvas', style: { border: '1px solid black' } }}
        onEnd={() => console.log('Stroke ended')}
        onBegin={() => console.log('Stroke began')}
      />
      <div>
        <button onClick={handleClear}>Clear</button>
        <button onClick={handleSave}>Save Signature</button>
      </div>
    </div>
  );
};

createRoot(document.getElementById('my-react-container')).render(<App />);

view raw JSON →