Socket
Socket
Sign inDemoInstall

react-signature-canvas

Package Overview
Dependencies
10
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-signature-canvas

A signature pad implementation in React


Version published
Weekly downloads
222K
increased by4.85%
Maintainers
1
Created
Weekly downloads
 

Package description

What is react-signature-canvas?

The react-signature-canvas package is a React wrapper for the signature_pad library, which allows users to draw smooth signatures on a canvas element. It provides an easy way to integrate signature capture functionality into React applications.

What are react-signature-canvas's main functionalities?

Basic Signature Capture

This feature allows you to capture a signature on a canvas. The pen color is set to black, and the canvas dimensions are specified.

import React from 'react';
import SignatureCanvas from 'react-signature-canvas';

function SignaturePad() {
  return (
    <SignatureCanvas penColor='black' canvasProps={{width: 500, height: 200, className: 'sigCanvas'}} />
  );
}

export default SignaturePad;

Clear Signature

This feature allows you to clear the signature from the canvas. A button is provided to trigger the clear action.

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

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

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

  return (
    <div>
      <SignatureCanvas ref={sigCanvas} penColor='black' canvasProps={{width: 500, height: 200, className: 'sigCanvas'}} />
      <button onClick={clear}>Clear</button>
    </div>
  );
}

export default SignaturePad;

Save Signature as Image

This feature allows you to save the signature as an image. The signature is converted to a data URL, which can be used to display or store the image.

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

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

  const save = () => {
    const dataURL = sigCanvas.current.getTrimmedCanvas().toDataURL('image/png');
    console.log(dataURL);
  };

  return (
    <div>
      <SignatureCanvas ref={sigCanvas} penColor='black' canvasProps={{width: 500, height: 200, className: 'sigCanvas'}} />
      <button onClick={save}>Save</button>
    </div>
  );
}

export default SignaturePad;

Other packages similar to react-signature-canvas

Readme

Source

React Signature Canvas

package-json releases commits

dt dy dm dw

NPM

A React wrapper component around signature_pad.

Originally, this was just an unopinionated fork of 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 to the underlying canvas element, has new, documented API methods you can use, and has new, documented props you can pass to it.

Installation

npm i -S react-signature-canvas

Usage

import React from 'react'
import ReactDOM from 'react-dom'
import SignatureCanvas from 'react-signature-canvas'

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

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 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. 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 to the SignatureCanvas in order to use and are instance methods of the ref.

<SignatureCanvas ref={(ref) => { this.sigCanvas = ref }} />
  • 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 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. on() and off() will, in addition, bind/unbind the window resize event handler. getCanvas(), getTrimmedCanvas(), and getSignaturePad() are new.

Example

$ npm start

Navigate to http://localhost:8080/ in your browser and you should be able to see the signature canvas in action.

The source code for this example is found in the example/ directory.

Keywords

FAQs

Last updated on 10 Feb 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc