vue-signature-pad

repository·master·Indexed 20 days ago

https://github.com/neighborhood999/vue-signature-pad

A Vue.js component wrapper for the signature_pad library that provides an HTML5 canvas-based signature drawing interface. It supports Vue 2 (v2.0.5) and Vue 3, offering features such as signature saving in image/png, image/jpeg, and image/svg+xml formats, undo/clear functionality, and customizable drawing options like pen color and dot size.

Tokens
2.2K
Snippets
10
Records
12
Agent score
69%

What's inside vue-signature-pad

  1. Setup vue-signature-pad in Vue 3

    master

    To use the component globally in a Vue 3 project, register it as a component on your app instance.

    import { createApp } from 'vue'
    import App from './App.vue'
    import { VueSignaturePad } from 'vue-signature-pad';
    
    const app = createApp(App)
    app.component("VueSignaturePad", VueSignaturePad);
    app.mount('#app')
  2. Setup vue-signature-pad in Vue 2

    master

    To use the component globally in a Vue 2 project, import VueSignaturePad and use the Vue.use() method.

    import Vue from 'vue';
    import VueSignaturePad from 'vue-signature-pad';
    
    Vue.use(VueSignaturePad);
  3. Handle signature pad events via options

    master

    You can pass event callbacks like onBegin and onEnd through the options prop to react to drawing lifecycle events.

    <template>
      <div id="app">
        <VueSignaturePad
          width="500px"
          height="500px"
          ref="signaturePad"
          :options="{ onBegin, onEnd }"
        />
      </div>
    </template>
    
    <script>
    export default {
      methods: {
        onBegin() {
          console.log('=== Begin ===');
        },
        onEnd() {
          console.log('=== End ===');
        }
      }
    };
    </script>
  4. Use VueSignaturePad in a single component

    master

    If you prefer not to register the component globally, you can import and declare it directly within your component's components option.

    <template>
      <div id="app">
        <VueSignaturePad width="500px" height="500px" ref="signaturePad" />
        <div>
          <button @click="save">Save</button>
          <button @click="undo">Undo</button>
        </div>
      </div>
    </template>
    
    <script>
    import { defineComponent } from "vue";
    import { VueSignaturePad } from 'vue-signature-pad';
    
    export default defineComponent({
      name: 'MySignaturePad',
      components: { VueSignaturePad },
      methods: {
        undo() {
          this.$refs.signaturePad.undoSignature();
        },
        save() {
          const { isEmpty, data } = this.$refs.signaturePad.saveSignature();
          console.log(isEmpty);
          console.log(data);
        }
      }
    });
    </script>
  5. Default configuration options for Vue Signature Pad

    master

    The following options define the default behavior and appearance of the signature pad. These can be overridden when configuring the component:

    • dotSize: The size of the drawing dot (default: 1.5)
    • minWidth: Minimum line width (default: 0.5)
    • maxWidth: Maximum line width (default: 2.5)
    • throttle: Throttling interval in ms (default: 16)
    • minDistance: Minimum distance between points to trigger a draw (default: 5)
    • backgroundColor: Background color in CSS format (default: 'rgba(0,0,0,0)')
    • penColor: Color of the pen in CSS format (default: 'black')
    • velocityFilterWeight: Weight for velocity filtering (default: 0.7)
    • onBegin: Callback function triggered when drawing starts (default: empty function)
    • onEnd: Callback function triggered when drawing ends (default: empty function)
    export const DEFAULT_OPTIONS = {
      dotSize: (0.5 + 2.5) / 2,
      minWidth: 0.5,
      maxWidth: 2.5,
      throttle: 16,
      minDistance: 5,
      backgroundColor: 'rgba(0,0,0,0)',
      penColor: 'black',
      velocityFilterWeight: 0.7,
      onBegin: () => {},
      onEnd: () => {}
    };
  6. Use VueSignaturePad methods

    master

    Access the signature pad instance via a template ref to call the following methods:

    MethodArgumentsDescription
    saveSignature(type, encoderOptions)(String, Number)Returns target canvas status and data (e.g., { isEmpty, data }).
    undoSignature()-Undo the last action.
    clearSignature()-Clear the canvas.
    mergeImageAndSignature(signature)Object or StringMerges provided images prop with the signature.
    addImages(images)ArrayProvides images to merge with signature.
    lockSignaturePad()-Lock the signature pad.
    openSignaturePad()-Open the signature pad.
    getPropImagesAndCacheImages()-Get all image information.
    clearCacheImages()-Clear cached images.
    fromDataURL(data, options, callback)(String, Object, Callback)Draw image from a data URL.
    fromData(data)StringReturns signature image as an array of point groups.
    toData()-Draws signature image from an array of point groups.
    isEmpty()-Returns whether the signature canvas has data.
  7. Configure VueSignaturePad props

    master

    The VueSignaturePad component accepts the following props:

    NameTypeDefaultDescription
    widthString100%Set the div width.
    heightString100%Set the div height.
    optionsObjectReferenceSet the signature pad options (uses signature_pad defaults).
    imagesArray[]Merge signature with provided images. Can be an array of strings ['A.png'] or objects [{ src: 'A.png', x: 0, y: 0 }].
    customStyleObject{}Custom div style.
    scaleToDevicePixelRatioBooleantrueScale the canvas up to match the device pixel ratio.
  8. Install and import the Vue Signature Pad plugin

    master

    To use Vue Signature Pad, import the default plugin export from the package. This plugin object contains the Vue plugin installation method and also includes all individual signature pad components as properties on the plugin object itself. This allows you to either install the plugin globally in your Vue application or access specific components directly from the plugin object.

    import VueSignaturePad from 'vue-signature-pad';
    
    // To install globally in Vue 2:
    Vue.use(VueSignaturePad);
    
    // Or to use components directly:
    const { SignaturePad } = VueSignaturePad;
  9. Transparent PNG placeholder object

    master

    The TRANSPARENT_PNG constant provides a base64 encoded 1x1 transparent PNG image. This can be used as a placeholder or a default value for signature image sources.

    export const TRANSPARENT_PNG = {
      src: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=',
      x: 0,
      y: 0
    };