Skip to content

Signature 签名板 ​

1:35

说明 ​

基于 canvas 2D 的手写签名板:手写、撤销、清空、导出图片,每一笔按落笔时的颜色与粗细留存。撤销通道按平台区分——鸿蒙端按笔画记录重绘,其它端还原像素快照。

导出统一为 base64 dataURL:点击确定通过 submit 事件回传,也可以 ref 调用 done() 自取;未落笔时两者都返回空串。

兼容性 ​

平台支持情况
App✅ 支持
H5✅ 支持
微信小程序✅ 支持

示例 ​

基础用法 ​

在签名区手写,点击「确定」通过 submit 事件回传 base64 图片,弹窗预览并支持保存图片。

查看源码
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 端不支持保存到相册。 -->

自定义画笔 ​

line-color 与 line-width 控制笔迹颜色与粗细,已落下的笔画保持原有颜色与粗细。

查看源码
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>

提示语 ​

tips / tips-size / tips-color 自定义提示文案与样式;提示默认半透明水印,自定义建议传 rgba 颜色;show-tips=false 可关闭提示层。

查看源码
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>

按钮配置 ​

show-clear / show-revoke / show-confirm 控制按钮显隐,clear-text / revoke-text / confirm-text 自定义文案。

查看源码
vue
<template>
    <ux-signature :show-revoke="true" clear-text="重写" revoke-text="撤销" confirm-text="保存" @submit="onSubmit" />
</template>

实例方法 ​

关闭内置按钮后,可通过 ref 调用 done / revoke / clear 自行控制,导出的图片同样弹窗预览。

查看源码
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>

背景与导出格式 ​

export-bg-color 会写入导出图,jpeg 的透明区域会变黑,务必设置导出底色;bg-color 只做签名区容器底色、不参与导出;export-format 与 quality 控制导出格式与质量。底色为浅色时请搭配深色 line-color,否则暗黑模式下默认浅色笔迹会看不清。

查看源码
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>

自定义尺寸 ​

width 支持百分比与带单位字符串,height 控制签名区高度。

查看源码
vue
<template>
    <ux-signature width="80%" :height="140" @submit="onSubmit" />
</template>

禁用书写 ​

disabled=true 时不可手写,按钮与导出不受影响。

查看源码
vue
<template>
    <ux-signature :disabled="true" @submit="onSubmit" />
</template>

API ​

Props ​

属性说明类型默认值版本
customStyle自定义根节点样式string | UTSJSONObject--
customClass自定义根节点样式类string''-
width组件宽度number | string'100%'-
height画布区域高度,单位 pxnumber | string200-
exportFormat导出格式string'png'-
qualityjpeg 导出质量number0.8-
disabled是否禁用书写booleanfalse-
showTips是否显示提示层booleantrue-
tips提示文案string''-
tipsSize提示字号number | string24-
tipsColor提示文字颜色string''-
lineColor笔迹颜色string''-
lineWidth笔迹宽度number3-
bgColor签名区容器底色string''-
exportBgColor画布填充底色string''-
exportGrid导出是否保留格线booleantrue-
gridType格线类型string''-
gridSize格子边长,单位 pxnumber56-
gridColor网格线颜色string''-
guideText描红文字string''-
guideColor描红文字颜色string''-
gridAlign格线垂直对齐string'center'-
showClear是否显示清空按钮booleantrue-
showRevoke是否显示撤销按钮booleanfalse-
showConfirm是否显示确定按钮booleantrue-
clearText清空按钮文案string''-
revokeText撤销按钮文案string''-
confirmText确定按钮文案string''-

Events ​

事件名说明回调参数版本
start落笔时触发--
signing书写过程中持续触发--
end抬笔或触摸取消时触发--
submit点击确定时触发,回传 base64 dataURL;未落笔为空串image: string-

Slots ​

插槽名说明版本
tips自定义提示层内容,覆盖在画布上方且不拦截书写事件-

注意事项 ​

  1. 撤销:非鸿蒙端按像素快照回退(保留最近 50 步),鸿蒙端按笔画重绘;鸿蒙端清空会一并清掉笔画记录,此后 revoke() 不再恢复清空前的内容,需要可撤销清空请在业务层留存导出图。
  2. 尺寸变化:容器布局变化或 width / height 改动后,旧快照与笔画坐标失效——非鸿蒙端清空撤销历史、鸿蒙端按新尺寸重绘笔画,内容会保留但缩放后笔迹精度下降。
  3. 导出:done() / submit 在没有任何笔画时返回空串;export-bg-color 会写入导出图,bg-color 只做屏幕容器底色、不参与导出;jpeg 的透明区域会变黑,务必同时设置 export-bg-color;quality 仅对 jpeg 生效,越界值钳制到 0-1。
  4. 保存相册:H5 端 uni.saveImageToPhotosAlbum 不可用,需降级为下载或提示(演示页给出提示);App 与小程序可直接保存。
  5. 暗黑模式:默认笔迹色取主题主文字色、默认底色取主题白底;自定义浅色 export-bg-color 时请搭配深色 line-color,否则笔迹看不清。
  6. 提示层用半透明水印叠在画布上方且不拦截触摸;tips 为空串时回退组件语料,show-tips=false(或传空插槽)可整体关闭。
  7. 组件隐藏(v-if=false / display:none)期间画布尺寸为 0,重新显示且布局已变化时,可调用 resize() 让画布按新尺寸重建并重铺格线。
  8. disabled 只关闭触摸书写,清空 / 撤销 / 确定按钮与导出不受影响。