# react-signature-canvas

> A React wrapper component around signature_pad. 100% test coverage, types, examples, & more. Unopinionated and heavily updated fork of react-signature-pad

Latest version **1.1.0-alpha.2** (published 2025-03-29) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install react-signature-canvas
pnpm add react-signature-canvas
yarn add react-signature-canvas
bun add react-signature-canvas
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0-alpha.2 |
| Published | 2025-03-29 |
| First published | 2016-09-01 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 40.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 653 |
| Author | Anton Gilgur |
| Maintainers | agilgur5 |
| Keywords | react, react-component, component, signature, sign, e-sign, e-signature, canvas, trim, whitespace, draw, pad, wrapper, signature-pad, react-signature-pad |

## Links

- npm: https://www.npmjs.com/package/react-signature-canvas
- Repository: https://github.com/agilgur5/react-signature-canvas
- Issues: https://github.com/agilgur5/react-signature-canvas/issues
- Funding: https://github.com/sponsors/agilgur5
- npm.io page: https://npm.io/package/react-signature-canvas

## Dependencies (4)

- [trim-canvas](https://npm.io/package/trim-canvas.md) ^0.1.0
- [signature_pad](https://npm.io/package/signature_pad.md) ^2.3.2
- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.17.9
- [@types/signature_pad](https://npm.io/package/@types/signature_pad.md) ^2.3.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.1.0-alpha.2 (latest) — 2025-03-29
- 0.1.11 (v0.1.x) — 2019-02-10
- 0.2.5 (v0.2.x) — 2019-02-10
- 0.3.1 (v0.3.x) — 2019-02-10
- 1.1.0-alpha.1 — 2025-02-15
- 1.0.7 — 2025-01-03
- 1.0.6 — 2022-06-16
- 1.0.5 — 2022-02-09
- 1.0.4 — 2022-02-09
- 1.0.3 — 2019-08-05
- 1.0.2 — 2019-06-16
- 1.0.1 — 2019-02-10
- 1.0.0 — 2019-02-10
- 1.0.0-alpha.1 — 2018-04-12
- 0.3.0 — 2018-03-26
- … 16 more at https://npm.io/package/react-signature-canvas/versions

## README

<!-- markdownlint-disable first-line-heading -->
<p align='center'>
  <img height='200' src='https://media.giphy.com/media/KH1emXYVIh1hT6UrrU/giphy.gif' />
</p>

# react-signature-canvas

<!-- releases / versioning -->
[![package-json](https://img.shields.io/github/package-json/v/agilgur5/react-signature-canvas.svg)](https://npmjs.org/package/react-signature-canvas)
[![releases](https://img.shields.io/github/tag-pre/agilgur5/react-signature-canvas.svg)](https://github.com/agilgur5/react-signature-canvas/releases)
[![commits](https://img.shields.io/github/commits-since/agilgur5/react-signature-canvas/latest.svg)](https://github.com/agilgur5/react-signature-canvas/commits/main)
<br><!-- downloads -->
[![dt](https://img.shields.io/npm/dt/react-signature-canvas.svg)](https://npmjs.org/package/react-signature-canvas)
[![dy](https://img.shields.io/npm/dy/react-signature-canvas.svg)](https://npmjs.org/package/react-signature-canvas)
[![dm](https://img.shields.io/npm/dm/react-signature-canvas.svg)](https://npmjs.org/package/react-signature-canvas)
[![dw](https://img.shields.io/npm/dw/react-signature-canvas.svg)](https://npmjs.org/package/react-signature-canvas)
<br><!-- status / activity -->
[![typings](https://img.shields.io/npm/types/react-signature-canvas.svg)](src/index.tsx)
[![build status](https://img.shields.io/github/actions/workflow/status/agilgur5/react-signature-canvas/ci.yml?branch=main)](https://github.com/agilgur5/react-signature-canvas/actions/workflows/ci.yml?query=branch%3Amain)
[![code coverage](https://img.shields.io/codecov/c/gh/agilgur5/react-signature-canvas/main.svg)](https://codecov.io/gh/agilgur5/react-signature-canvas)

A React wrapper component around [signature_pad](https://github.com/szimek/signature_pad).

Originally, this was just an _unopinionated_ fork of [react-signature-pad](https://github.com/blackjk3/react-signature-pad) that did not impose any styling or wrap any other unwanted elements around your canvas -- it's just a wrapper around a single canvas element!
Hence the naming difference.
Nowadays, this repo / library has significantly evolved, introducing new features, fixing various bugs, and now wrapping the upstream `signature_pad` to have its updates and bugfixes baked in.

This fork also allows you to directly pass [props](#props) to the underlying canvas element, has new, documented [API methods](#api) you can use, has new, documented [props](#props) you can pass to it, has a [live demo](https://agilgur5.github.io/react-signature-canvas/), has a [CodeSandbox playground](https://codesandbox.io/s/github/agilgur5/react-signature-canvas/tree/codesandbox-example), has [100% test coverage](https://codecov.io/gh/agilgur5/react-signature-canvas), and is [written in TypeScript](src/index.tsx).

## Installation

```sh
npm i -S react-signature-canvas
```

## Usage

```jsx
import React from 'react'
import { createRoot } from 'react-dom/client'
import SignatureCanvas from 'react-signature-canvas'

createRoot(
  document.getElementById('my-react-container')
).render(
  <SignatureCanvas penColor='green'
    canvasProps={{width: 500, height: 200, className: 'sigCanvas'}} />,
)
```

### Props

The props of SignatureCanvas mainly control the properties of the pen stroke used in drawing.
All props are **optional**.

- `velocityFilterWeight` : `number`, default: `0.7`
- `minWidth` : `number`, default: `0.5`
- `maxWidth` : `number`, default: `2.5`
- `minDistance`: `number`, default: `5`
- `dotSize` : `number` or `function`,
  default: `() => (this.minWidth + this.maxWidth) / 2`
- `penColor` : `string`, default: `'black'`
- `throttle`: `number`, default: `16`

There are also two callbacks that will be called when a stroke ends and one begins, respectively.

- `onEnd` : `function`
- `onBegin` : `function`

Additional props are used to control the canvas element.

- `canvasProps`: `object`
  - directly passed to the underlying `<canvas />` element
- `backgroundColor` : `string`, default: `'rgba(0,0,0,0)'`
  - used in the [API's](#api) `clear` convenience method (which itself is called internally during resizes)
- `clearOnResize`: `bool`, default: `true`
  - whether or not the canvas should be cleared when the window resizes

Of these props, all, except for `canvasProps` and `clearOnResize`, are passed through to `signature_pad` as its [options](https://github.com/szimek/signature_pad#options).
`signature_pad`'s internal state is automatically kept in sync with prop updates for you (via a `componentDidUpdate` hook).

### API

All API methods require [a ref](https://react.dev/learn/manipulating-the-dom-with-refs) to the SignatureCanvas in order to use and are instance methods of the ref.

```jsx
import React, { useRef } from 'react'
import SignatureCanvas from 'react-signature-canvas'

function MyApp() {
  const sigCanvas = useRef(null);

  return <SignatureCanvas ref={sigCanvas} />
}
```

- `isEmpty()` : `boolean`, self-explanatory
- `clear()` : `void`, clears the canvas using the `backgroundColor` prop
- `fromDataURL(base64String, options)` : `void`, writes a base64 image to canvas
- `toDataURL(mimetype, encoderOptions)`: `base64string`, returns the signature image as a data URL
- `fromData(pointGroupArray)`: `void`, draws signature image from an array of point groups
- `toData()`: `pointGroupArray`, returns signature image as an array of point groups
- `off()`: `void`, unbinds all event handlers
- `on()`: `void`, rebinds all event handlers
- `getCanvas()`: `canvas`, returns the underlying canvas ref.
  Allows you to modify the canvas however you want or call methods such as `toDataURL()`
- `getTrimmedCanvas()`: `canvas`, creates a copy of the canvas and returns a [trimmed version](https://github.com/agilgur5/trim-canvas) of it, with all whitespace removed.
- `getSignaturePad()`: `SignaturePad`, returns the underlying SignaturePad reference.

The API methods are _mostly_ just wrappers around [`signature_pad`'s API](https://github.com/szimek/signature_pad#api).
`on()` and `off()` will, in addition, bind/unbind the window resize event handler.
`getCanvas()`, `getTrimmedCanvas()`, and `getSignaturePad()` are new.

## Example

You can interact with the example in a few different ways:

1. Run `npm start` and navigate to [http://localhost:1234/](http://localhost:1234/).<br>
   Hosted locally via the [`example/`](example/) directory

1. [View the live demo here](https://agilgur5.github.io/react-signature-canvas/).<br>
   Hosted via the [`gh-pages` branch](https://github.com/agilgur5/react-signature-canvas/tree/gh-pages), a standalone version of the code in [`example/`](example/)

1. [Play with the CodeSandbox here](https://codesandbox.io/s/github/agilgur5/react-signature-canvas/tree/codesandbox-example).<br>
   Hosted via the [`codesandbox-example` branch](https://github.com/agilgur5/react-signature-canvas/tree/codesandbox-example), a slightly modified version of the above.

---
_Source: https://npm.io/package/react-signature-canvas · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
