Install vue-signature-pad
masterInstall the package using yarn:
$ yarn add vue-signature-padNote on versions:
- For Vue 2, install version 2.0.5.
- For Vue 3, install the latest publish version.
repository·master·Indexed 20 days ago
https://github.com/neighborhood999/vue-signature-padA 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.
Install the package using yarn:
$ yarn add vue-signature-padNote on versions:
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')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);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>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>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: () => {}
};Access the signature pad instance via a template ref to call the following methods:
| Method | Arguments | Description |
|---|---|---|
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 String | Merges provided images prop with the signature. |
addImages(images) | Array | Provides 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) | String | Returns 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. |
The VueSignaturePad component accepts the following props:
| Name | Type | Default | Description |
|---|---|---|---|
width | String | 100% | Set the div width. |
height | String | 100% | Set the div height. |
options | Object | Reference | Set the signature pad options (uses signature_pad defaults). |
images | Array | [] | Merge signature with provided images. Can be an array of strings ['A.png'] or objects [{ src: 'A.png', x: 0, y: 0 }]. |
customStyle | Object | {} | Custom div style. |
scaleToDevicePixelRatio | Boolean | true | Scale the canvas up to match the device pixel ratio. |
You can import the VueSignaturePad component directly from the package entry point. This component is the primary interface for integrating the signature pad into your Vue application.
import { VueSignaturePad } from 'vue-signature-pad';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;When saving a signature, the library supports the following MIME types:
image/pngimage/jpegimage/svg+xmlYou can verify if a specific type is supported using the checkSaveType utility function.
export const IMAGE_TYPES = ['image/png', 'image/jpeg', 'image/svg+xml'];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
};