react-signature-pad-wrapper

repository·master·Indexed 42 days ago

https://github.com/michaeldzjap/react-signature-pad-wrapper

A React component wrapper for the signature_pad library (version 4.3.3) that simplifies responsive canvas behavior. It provides a SignaturePad component with support for fixed or responsive sizing, a redrawOnResize prop to preserve drawings during window resize, and direct access to the underlying signature_pad API via refs for managing state, exporting data as URL or SVG, and controlling drawing parameters.

Tokens
2.1K
Snippets
14
Records
14
Agent score
51%

What's inside react-signature-pad-wrapper

  1. Configure canvas responsiveness

    master

    The SignaturePad component handles canvas sizing in two ways:

    1. Fixed Size: Pass explicit width and height props (in pixels).
    2. Responsive: Omit the width and height props. The component will automatically update the canvas size on window resize using a debounced handler.

    Note: Changing canvas dimensions normally erases existing content. To prevent this, use the redrawOnResize prop.

    // Fixed size
    <SignaturePad width={500} height={500} />
    
    // Responsive (omitting width/height)
    <SignaturePad />
    
    // Responsive with content preservation
    <SignaturePad redrawOnResize />
  2. Use the SignaturePad component

    master

    Import the SignaturePad component and use it within your React render method. You can pass configuration options via the options prop or interact with the underlying signature pad instance using a ref.

    import SignaturePad from 'react-signature-pad-wrapper';
    
    // Using options prop
    <SignaturePad options={{minWidth: 5, maxWidth: 10, penColor: 'rgb(66, 133, 244)'}} />
    
    // Using ref for runtime control
    <SignaturePad ref={this.signaturePadRef} />
  3. Control SignaturePad via Ref

    master

    By using a ref, you can access the underlying signature_pad instance methods and properties at runtime. This allows you to clear the pad, check if it is empty, or update drawing parameters dynamically.

    // Call an instance method
    this.signaturePad.clear();
    this.signaturePad.isEmpty();
    
    // Get/set an object property
    this.signaturePad.minWidth = 5;
    this.signaturePad.maxWidth = 10;
    this.signaturePad.penColor = 'rgb(66, 133, 244)';
  4. Use redrawOnResize to preserve drawings during resize

    master

    When using a responsive layout (omitting width and height), setting the redrawOnResize prop to true will attempt to preserve the current drawing. The component saves the canvas content to a base64 string before resizing and reloads it afterward.

    Warning: Repeatedly saving and loading image data during resize events can cause rapid degradation of drawing quality.

    <SignaturePad redrawOnResize />
  5. Export signature data as URL or SVG

    master

    Use the following methods on the SignaturePad instance to retrieve the signature data:

    • toDataURL(type?: string, encoderOptions?: number): Returns the signature as a data URL (e.g., 'image/png').
    • toSvgDataUrl(encoderOptions?: ToSVGOptions): Returns the signature as an SVG data URL.
    • toSVG(svgOptions?: ToSVGOptions): Returns the signature as an SVG string (not base64 encoded).
    const pngDataUrl = signaturePadRef.current.toDataURL('image/png');
    const svgString = signaturePadRef.current.toSVG();
  6. Manipulate signature pad properties via component instance

    master

    The SignaturePad component exposes several properties as getters and setters on its instance, allowing you to modify the drawing behavior dynamically:

    • dotSize: Set the radius of a single dot.
    • minWidth: Set the minimum width of a line.
    • maxWidth: Set the maximum width of a line.
    • throttle: Set the throttle for drawing the next point (in ms).
    • backgroundColor: Set the color used to clear the background.
    • penColor: Set the color used to draw the lines.
    • velocityFilterWeight: Set the weight used to modify new velocity based on previous velocity.
    // Example of changing pen color via ref
    signaturePadRef.current.penColor = 'blue';
    signaturePadRef.current.dotSize = 2;
  7. Access the underlying signature_pad instance

    master

    You can access the original signature_pad instance via the instance getter on the SignaturePad component. This allows you to call any method available in the base signature_pad library that might not be explicitly wrapped by this component.

    // Assuming you have a ref to the SignaturePad component
    const signaturePadInstance = signaturePadRef.current.instance;
  8. Configure the SignaturePad component props

    master

    The SignaturePad component accepts several props to control its dimensions, appearance, and behavior.

    Props

    • width (number, optional): The width of the canvas. If not provided, the canvas will default to 100% width and respond to window resize events.
    • height (number, optional): The height of the canvas. If not provided, the canvas will default to 100% height and respond to window resize events.
    • options (Options, optional): Configuration options passed directly to the underlying signature_pad instance.
    • canvasProps (object, optional): An object containing attributes to be applied directly to the <canvas> element.
    • redrawOnResize (boolean, default: false): If true, the signature will be redrawn when the window is resized.
    • debounceInterval (number, default: 150): The interval in milliseconds used to debounce the resize handler.
    <SignaturePad 
      width={500} 
      height={200} 
      options={{ penColor: 'rgb(0, 0, 0)' }} 
      redrawOnResize={true} 
    />