Skip to content

Signature Pad ​

1:35

Description ​

A handwritten signature pad supporting undo, clear and image export, with each stroke keeping its own color and width.

Compatibility ​

PlatformSupport
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
vue
<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
vue
<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
vue
<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
vue
<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
vue
<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
vue
<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
vue
<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
vue
<template>
    <ux-signature :disabled="true" @submit="onSubmit" />
</template>

API ​

Props ​

PropertyDescriptionTypeDefaultVersion
customStyleCustom root element stylestring | UTSJSONObject--
customClassCustom root element style classstring''-
widthComponent width; number (px), percentage or any CSS lengthnumber | string'100%'-
heightCanvas area height in pxnumber | string200-
exportFormatExport format: png | jpeg (default png; transparent areas turn black in jpeg, so set exportBgColor as well)string'png'-
qualityJPEG export quality, 0-1number0.8-
disabledDisable touch writingbooleanfalse-
showTipsShow the tips layerbooleantrue-
tipsTip text, falls back to the built-in locale textstring''-
tipsSizeTip font size in pxnumber | string24-
tipsColorTip text color, falls back to a translucent watermark colorstring''-
lineColorStroke color, falls back to the theme primary text colorstring''-
lineWidthStroke width in pxnumber3-
bgColorSignature area container background, painted on screen only and never exportedstring''-
exportBgColorCanvas fill color written into the export; transparent when emptystring''-
exportGridKeep the writing grid in the exportbooleantrue-
gridTypeGrid line type: empty disables it; tian | mi | ninestring''-
gridSizeCell edge length in px (clamped to 24-120)number56-
gridColorGrid line color, falls back to reference redstring''-
guideTextTracing text, one character per cellstring''-
guideColorTracing text color, falls back to translucent redstring''-
gridAlignVertical grid alignment: center | topstring'center'-
showClearShow the clear buttonbooleantrue-
showRevokeShow the undo buttonbooleanfalse-
showConfirmShow the confirm buttonbooleantrue-
clearTextClear button label, falls back to the built-in locale textstring''-
revokeTextUndo button label, falls back to the built-in locale textstring''-
confirmTextConfirm button label, falls back to the built-in locale textstring''-

Events ​

EventDescriptionParamsVersion
startEmitted when a stroke starts--
signingEmitted continuously while writing--
endEmitted on stroke end or touch cancel--
submitEmitted on confirm with a base64 data URL; empty string when nothing was drawnimage: string-

Slots ​

SlotDescriptionVersion
tipsCustom tips layer above the canvas; it does not block writing-

Notes ​

  1. 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.
  2. Resizing: after a container layout change or a width / height update, 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.
  3. Export: done() / submit return an empty string when nothing was drawn. export-bg-color is written into the export while bg-color only paints the on-screen container; jpeg turns transparent areas black, so always set export-bg-color too; quality applies to jpeg only and is clamped to 0-1.
  4. Saving to the album: uni.saveImageToPhotosAlbum is unavailable on H5, fall back to a download or a hint (the demo shows one); App and Mini Program can save directly.
  5. 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 dark line-color.
  6. The tips layer is a translucent watermark above the canvas that does not block touches; an empty tips falls back to the built-in locale text and show-tips=false hides the layer (including the slot).
  7. While hidden (v-if=false / display:none) the canvas size is 0; after showing it again with a changed layout, call resize() to rebuild the canvas and repaint the grid.
  8. disabled only blocks touch writing; the clear / undo / confirm buttons and export keep working.