Signature Pad
1:35Description
A handwritten signature pad supporting undo, clear and image export, with each stroke keeping its own color and width.
Compatibility
| Platform | Support |
|---|---|
| App | ✅ Supported |
| H5 | ✅ Supported |
| WeChat Mini Program | ✅ Supported |
Examples
Basic Usage
Draw in the pad and tap Confirm to receive a base64 image through the submit event; a preview dialog opens and the image can be saved.
View source
<template>
<ux-signature @submit="onSubmit" />
<ux-modal
v-model="showPreview"
title="签名图片"
cancel-text="保存图片"
confirm-text="确认"
:show-cancel-button="true"
>
<image :src="preview" mode="widthFix" />
</ux-modal>
</template>
<script setup lang="uts">
const showPreview = ref(false);
const preview = ref<string>('');
function onSubmit(image: string): void {
if (image == '') return;
preview.value = image;
showPreview.value = true;
}
</script>
<!-- 保存图片:先 uni.base64ToArrayBuffer + getFileSystemManager().writeFile 落临时文件,
再调用 uni.saveImageToPhotosAlbum({ filePath });H5 端不支持保存到相册。 -->Custom Brush
line-color and line-width control the ink color and thickness; existing strokes keep their original appearance.
View source
<template>
<ux-signature line-color="#fa3534" :line-width="6" @submit="onSubmit" />
</template>
<script setup lang="uts">
function onSubmit(image: string): void {
console.log('base64 长度', image.length);
}
</script>Hint Text
Use tips / tips-size / tips-color to customize the hint; the hint is a translucent watermark by default, so pass an rgba color when customizing; set show-tips=false to hide the hint layer.
View source
<template>
<ux-signature tips="请在此签名确认" :tips-size="20" tips-color="rgba(144, 147, 153, 0.45)" @submit="onSubmit" />
<ux-gap :height="8" />
<ux-signature :show-tips="false" @submit="onSubmit" />
</template>Button Configuration
show-clear / show-revoke / show-confirm toggle the buttons, while clear-text / revoke-text / confirm-text customize their labels.
View source
<template>
<ux-signature :show-revoke="true" clear-text="重写" revoke-text="撤销" confirm-text="保存" @submit="onSubmit" />
</template>Instance Methods
With built-in buttons hidden, call done / revoke / clear through a ref; exported images open in the same preview dialog.
View source
<template>
<ux-signature
ref="signatureRef"
:height="160"
:show-clear="false"
:show-confirm="false"
:show-revoke="false"
/>
<view class="signature-actions">
<view class="signature-action">
<ux-button size="small" @click="onClear">清空</ux-button>
</view>
<view class="signature-action">
<ux-button size="small" type="warning" @click="onRevoke">撤销</ux-button>
</view>
<view class="signature-action">
<ux-button size="small" type="primary" @click="onDone">导出</ux-button>
</view>
</view>
</template>
<script setup lang="uts">
const signatureRef = ref<UxSignatureComponentPublicInstance | null>(null);
function onClear(): void {
signatureRef.value?.clear();
}
function onRevoke(): void {
signatureRef.value?.revoke();
}
function onDone(): void {
// 此处接预览弹窗
console.log('base64', signatureRef.value?.done() ?? '');
}
</script>
<style lang="scss">
.signature-actions {
display: flex;
flex-direction: row;
align-items: center;
justify-content: flex-end;
margin-top: 12px;
}
.signature-action {
margin-left: 10px;
}
</style>Background and Format
export-bg-color is painted into the exported image; transparent areas turn black in jpeg, so always set an export background. bg-color only paints the on-screen container and is never exported. export-format and quality control the output. With a light background, pair it with a dark line-color, otherwise the default light ink is hard to see in dark mode.
View source
<template>
<ux-signature export-bg-color="#ffffff" line-color="#303133" tips="背景色 #ffffff · jpeg" export-format="jpeg" :quality="0.6" @submit="onSubmit" />
<ux-gap :height="8" />
<ux-signature export-bg-color="#f8f8f8" line-color="#303133" tips="背景色 #f8f8f8" @submit="onSubmit" />
</template>Custom Size
width accepts percentages and unit strings, while height sets the pad height.
View source
<template>
<ux-signature width="80%" :height="140" @submit="onSubmit" />
</template>Disabled
When disabled is true drawing is blocked, while buttons and export still work.
View source
<template>
<ux-signature :disabled="true" @submit="onSubmit" />
</template>API
Props
| Property | Description | Type | Default | Version |
|---|---|---|---|---|
customStyle | Custom root element style | string | UTSJSONObject | - | - |
customClass | Custom root element style class | string | '' | - |
width | Component width; number (px), percentage or any CSS length | number | string | '100%' | - |
height | Canvas area height in px | number | string | 200 | - |
exportFormat | Export format: png | jpeg (default png; transparent areas turn black in jpeg, so set exportBgColor as well) | string | 'png' | - |
quality | JPEG export quality, 0-1 | number | 0.8 | - |
disabled | Disable touch writing | boolean | false | - |
showTips | Show the tips layer | boolean | true | - |
tips | Tip text, falls back to the built-in locale text | string | '' | - |
tipsSize | Tip font size in px | number | string | 24 | - |
tipsColor | Tip text color, falls back to a translucent watermark color | string | '' | - |
lineColor | Stroke color, falls back to the theme primary text color | string | '' | - |
lineWidth | Stroke width in px | number | 3 | - |
bgColor | Signature area container background, painted on screen only and never exported | string | '' | - |
exportBgColor | Canvas fill color written into the export; transparent when empty | string | '' | - |
exportGrid | Keep the writing grid in the export | boolean | true | - |
gridType | Grid line type: empty disables it; tian | mi | nine | string | '' | - |
gridSize | Cell edge length in px (clamped to 24-120) | number | 56 | - |
gridColor | Grid line color, falls back to reference red | string | '' | - |
guideText | Tracing text, one character per cell | string | '' | - |
guideColor | Tracing text color, falls back to translucent red | string | '' | - |
gridAlign | Vertical grid alignment: center | top | string | 'center' | - |
showClear | Show the clear button | boolean | true | - |
showRevoke | Show the undo button | boolean | false | - |
showConfirm | Show the confirm button | boolean | true | - |
clearText | Clear button label, falls back to the built-in locale text | string | '' | - |
revokeText | Undo button label, falls back to the built-in locale text | string | '' | - |
confirmText | Confirm button label, falls back to the built-in locale text | string | '' | - |
Events
| Event | Description | Params | Version |
|---|---|---|---|
start | Emitted when a stroke starts | - | - |
signing | Emitted continuously while writing | - | - |
end | Emitted on stroke end or touch cancel | - | - |
submit | Emitted on confirm with a base64 data URL; empty string when nothing was drawn | image: string | - |
Slots
| Slot | Description | Version |
|---|---|---|
tips | Custom tips layer above the canvas; it does not block writing | - |
Notes
- Undo: platforms other than HarmonyOS restore a pixel snapshot (last 50 steps), HarmonyOS repaints the recorded strokes. On HarmonyOS clearing also drops the stroke records, so
revoke()cannot restore pre-clear content; keep the exported image yourself if clearing must stay reversible. - Resizing: after a container layout change or a
width/heightupdate, snapshots and stroke coordinates are invalidated — other platforms drop the undo history while HarmonyOS repaints at the new size. Content survives but stroke precision drops after scaling. - Export:
done()/submitreturn an empty string when nothing was drawn.export-bg-coloris written into the export whilebg-coloronly paints the on-screen container;jpegturns transparent areas black, so always setexport-bg-colortoo;qualityapplies to jpeg only and is clamped to 0-1. - Saving to the album:
uni.saveImageToPhotosAlbumis unavailable on H5, fall back to a download or a hint (the demo shows one); App and Mini Program can save directly. - Dark mode: the default stroke color is the theme primary text color and the default background is the theme white; when using a light
export-bg-color, pair it with a darkline-color. - The tips layer is a translucent watermark above the canvas that does not block touches; an empty
tipsfalls back to the built-in locale text andshow-tips=falsehides the layer (including the slot). - While hidden (
v-if=false/display:none) the canvas size is 0; after showing it again with a changed layout, callresize()to rebuild the canvas and repaint the grid. disabledonly blocks touch writing; the clear / undo / confirm buttons and export keep working.