Vitral 0.2.18
Form

SignaturePad

A surface to sign on with a mouse, a pen or a finger. The ink follows the speed of the hand, and the signature comes back as an SVG a form can store.

Import

main.ts
import { SignaturePad } from '@vitral/vue';

Basic

`v-model` is the signature as an SVG data URL, ready for an `<img>` or a form field; `v-model:strokes` is the points, to draw it again later. Undo takes back the last stroke.

Draw your signature with a mouse, a pen or a finger.
<script setup lang="ts">import { SignaturePad } from '@vitral/vue';import { ref } from 'vue'; const signature = ref<string | null>(null);</script> <template>    <div style="display: grid; gap: 1rem; max-width: 32rem">        <SignaturePad v-model="signature" aria-label="Signature" />        <div v-if="signature" style="display: flex; gap: 1rem; align-items: center">            <span>Stored as:</span>            <img :src="signature" alt="The signature, as stored" style="height: 3rem; border: 1px dashed currentColor; border-radius: 0.375rem; opacity: 0.8" />        </div>    </div></template>

The ink

The line follows the hand the way a nib does. `thinning` is how much the pressure changes the width — a pen's own pressure, or else one worked out from the speed, so a slow stroke is wider than a quick one; 0 draws a line of one width. `smoothing` takes the tremor out, `taper` narrows each end to a point over that length, and `pen-width` is the width at middling pressure. The stored SVG is drawn with the same ink. Sign, then move the sliders: the signature is drawn again.

Draw your signature with a mouse, a pen or a finger.
<script setup lang="ts">import { Label, SignaturePad, Slider, ToggleSwitch } from '@vitral/vue';import { ref } from 'vue'; const width = ref(3);const thinning = ref(0.6);const smoothing = ref(0.5);const taper = ref(0);const pressure = ref(true);</script> <template>    <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr)); gap: 1.5rem; align-items: start">        <SignaturePad :pen-width="width" :thinning="thinning" :smoothing="smoothing" :taper="taper" :pressure="pressure" aria-label="Signature" height="12rem" />        <div style="display: grid; gap: 1rem">            <div style="display: grid; gap: 0.5rem">                <Label id="ink-width">Width: {{ width }}</Label>                <Slider v-model="width" :min="1" :max="8" :step="0.5" aria-labelledby="ink-width" />            </div>            <div style="display: grid; gap: 0.5rem">                <Label id="ink-thinning">Thinning: {{ thinning }}</Label>                <Slider v-model="thinning" :min="0" :max="1" :step="0.05" aria-labelledby="ink-thinning" />            </div>            <div style="display: grid; gap: 0.5rem">                <Label id="ink-smoothing">Smoothing: {{ smoothing }}</Label>                <Slider v-model="smoothing" :min="0" :max="1" :step="0.05" aria-labelledby="ink-smoothing" />            </div>            <div style="display: grid; gap: 0.5rem">                <Label id="ink-taper">Taper: {{ taper }}</Label>                <Slider v-model="taper" :min="0" :max="80" :step="5" aria-labelledby="ink-taper" />            </div>            <label style="display: flex; gap: 0.5rem; align-items: center">                <ToggleSwitch v-model="pressure" />                A pen's own pressure            </label>        </div>    </div></template>

In a form

The pad is a field like any other: it takes a label, and `toPNG()` gives an image for a service that wants one.

Draw your signature with a mouse, a pen or a finger.
<script setup lang="ts">import { Button, InputText, Label, SignaturePad } from '@vitral/vue';import { ref } from 'vue'; const name = ref('');const signature = ref<string | null>(null);const pad = ref<InstanceType<typeof SignaturePad> | null>(null);const sent = ref(''); async function submit() {    const png = await pad.value?.toPNG();    sent.value = png ? `Sent ${name.value || 'the form'} with a ${Math.max(1, Math.round(png.size / 1024))} KB PNG.` : 'Sign first.';}</script> <template>    <form style="display: grid; gap: 0.75rem; max-width: 28rem" @submit.prevent="submit">        <Label for="sig-name">Full name</Label>        <InputText id="sig-name" v-model="name" />        <Label id="sig-label">Signature</Label>        <SignaturePad ref="pad" v-model="signature" aria-labelledby="sig-label" height="8rem" pen-color="#1d4ed8" />        <div style="display: flex; gap: 0.75rem; align-items: center">            <Button type="submit" label="Send" :disabled="!signature" />            <span role="status">{{ sent }}</span>        </div>    </form></template>

Read-only and disabled

Read-only shows a signature and takes no more ink; disabled is also dimmed and left out of the tab order.

Draw your signature with a mouse, a pen or a finger.
Draw your signature with a mouse, a pen or a finger.
<script setup lang="ts">import { SignaturePad } from '@vitral/vue'; const strokes: [number, number][][] = [    [[20, 70], [34, 40], [44, 30], [52, 36], [46, 60], [40, 78], [52, 64], [66, 48], [74, 52], [72, 70], [84, 58], [96, 52], [104, 60], [118, 50], [132, 46], [150, 54]],    [[160, 40], [172, 76]],    [[180, 60], [196, 48], [210, 56], [206, 72], [222, 60], [240, 54], [262, 62]]];</script> <template>    <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr)); gap: 1rem">        <SignaturePad :strokes="strokes" readonly aria-label="Signed (read-only)" height="7rem" />        <SignaturePad disabled aria-label="Signature (disabled)" height="7rem" />    </div></template>

API

Read from packages/vue/src/components/SignaturePad/types.ts, so it says what the component actually accepts.

Props

NameTypeDescription
penColor stringThe ink's colour; the theme's (the text colour) otherwise.
penWidth numberThe ink's width at middling pressure, in the pad's units. Defaults to the theme's, 2.5.
thinning numberHow much the pressure changes the width, 0 to 1: at 0.5 the line runs from half to one and a half times `penWidth`. The pressure is a pen's own, or else worked out from the speed — slower is wider. 0 draws a line of one width. Defaults to 0.5.
smoothing numberHow much of the hand's tremor is smoothed away, 0 to 1. Higher is smoother, and lags a little more. Defaults to 0.5.
taper number | [start: number, end: number]Length over which each end of a stroke narrows to a point, in the pad's units: one for both, or `[start, end]`. Defaults to 0.
pressure booleanUse a pen's own pressure where it reports one; false works it out from the speed for every device. Defaults to true.
minDistance numberPoints closer than this to the last one, in the pad's units, are left out. Defaults to 1.5.
height stringAny CSS length; the theme's (`10rem`) otherwise.
disabled boolean—
readonly booleanShows the signature, and takes no more ink.
placeholder string | falseThe words above the line while the pad is empty; the locale's "Sign here" otherwise. `false` for none.
showControls booleanThe undo and clear buttons in the corner. Defaults to true.

Plus pt, dt and unstyled from BaseProps, see pass-through and unstyled mode.

Emits

EventPayloadDescription
begin —A stroke was started.
end —A stroke was finished; the model has the signature with it.