Skip to content

Signature - Vant 4

Signature

Intro

Component for signature scene, based on Canvas. Please upgrade vant to >= v4.3.0 before using this component.

Install

Register component globally via app.use, refer to Component Registration for more registration ways.

js
import { createApp } from'vue'; import { Signature } from'vant'; const app = createApp(); app.use(Signature);

Usage

Basic Usage

When the confirm button is clicked, the component will emit the submit event. The first parameter of the event is data, which contains the following fields:

  • image: The image corresponding to the signature, which is in base64 string format. Returns an empty string if the signature is empty.
  • canvas: The Canvas element.
html
js
import { ref } from'vue'; import { showToast } from'vant'; exportdefault { setup() { const image = ref(''); constonSubmit = (data) => { image.value = data.image; }; constonClear = () => showToast('clear'); return { image, onSubmit, onClear, }; }, };

Pen Color

Use pen-color prop to set the color of the brush stroke.

html

Line Width

Use line-width prop to set the width of the line.

html

Background Color

Use background-color prop to set the color of the background.

html

API

Props

ParameterDescriptionTypeDefault
typeExport image typestringpng
pen-colorColor of the brush stroke, default is blackstring#000

| line-width | Width of the line | number | 3 | | background-color | Background color | string | - | | tips | Text that appears when Canvas is not supported | string | - | | clear-button-text | Clear button text | string | Clear | | confirm-button-text | Confirm button text | string | Confirm |

Events

Event NameDescriptionCallback Parameters
startEmitted when signing starts-
endEmitted when signing ends-
signingEmitted when signingevent: TouchEvent
submitEmitted when clicking the confirm buttondata: { image: string; canvas: HTMLCanvasElement }
clearEmitted when clicking the cancel button-

Slots

NameDescriptionSlotProps
tipsCustom tips-

Methods

Use ref to get Signature instance and call instance methods.

NameDescriptionAttributeReturn value
resize v4.7.3Resize Signature when container element resized or visibility changed--
clear v4.8.6Can be called to clear the signature--
submit v4.8.6Trigger the submit event, which is equivalent to clicking the confirm button.--

Types

The component exports the following type definitions:

ts
importtype { SignatureProps, SignatureInstance } from'vant';

SignatureInstance is the type of component instance:

ts
import { ref } from'vue'; importtype { SignatureInstance } from'vant'; const signatureRef = ref<SignatureInstance>(); signatureRef.value?.resize();

Theming

CSS Variables

The component provides the following CSS variables, which can be used to customize styles. Please refer to ConfigProvider component.

NameDefault ValueDescription
--van-signature-paddingvar(--van-padding-xs)-
--van-signature-content-height200pxHeight of the canvas
--van-signature-content-backgroundvar(--van-background-2)Background color of the canvas
--van-signature-content-border1px dotted #dadadaBorder style of the canvas

Enterprise-level mobile solution based on Vant