# Signature 签名组件 
该组件可用于电子签名、手写签名等场景。支持多种画笔颜色、压感绘制、撤销重做、水印等功能。
# 平台差异说明
App(vue) | App(nvue) | H5 | 小程序 |
---|---|---|---|
√ | X | √ | √ |
# 基本使用
<template> <view class="signature-container"> <u-signature ref="signatureRef" :penSize="penSize" :backgroundColor="backgroundColor" @confirm="onConfirm" ></u-signature> </view> </template> <script> export default { data() { return { penSize: 5, backgroundColor: '#fff' } }, methods: { onConfirm(res) { console.log('签名完成', res); // res 为签名图片的 base64 数据 } } }; </script> <style lang="scss" scoped> .signature-container { width: 100%; height: 300px; border: 1px solid #d6d7d8; border-radius: 8px; } </style>
✅ Copy success!
# 自定义画笔颜色
可以通过 penColor
属性设置画笔颜色,也可以通过 penColorList
提供颜色选择列表:
<template> <view class="signature-container"> <u-signature ref="signatureRef" :penColor="penColor" :penColorList="penColorList" :showColorList="true" ></u-signature> </view> </template> <script> export default { data() { return { penColor: '#FF1E10', penColorList: ['#4D4D4D', '#FF1E10', '#FFBE00', '#1A9BFF', '#1AAD19'] } } }; </script>
✅ Copy success!
# 工具栏操作
组件内置了清空、撤销、完成等操作按钮:
<template> <view class="signature-container"> <u-signature ref="signatureRef" :showToolbar="true" :showClear="true" :showUndo="true" clearText="清空" undoText="撤销" confirmText="完成" ></u-signature> </view> </template>
✅ Copy success!
# 压感绘制
开启压感功能后,可以根据触摸压力调整线条粗细:
<template> <view class="signature-container"> <u-signature ref="signatureRef" :openSmooth="true" :minLineWidth="2" :maxLineWidth="8" ></u-signature> </view> </template>
✅ Copy success!
# 水印功能
可以为签名添加水印,支持自定义水印文本、颜色、角度等:
<template> <view class="signature-container"> <u-signature ref="signatureRef" :showWatermark="true" :watermark="watermark" ></u-signature> </view> </template> <script> export default { data() { return { watermark: { showOnSave: true, text: '电子签名', color: 'rgba(0, 0, 0, 0.1)', fontSize: 16, fontFamily: 'Arial', rotate: -30, spacing: 100, single: false } } } }; </script>
✅ Copy success!
# 水印配置对象详细说明
watermark
对象包含以下属性:
属性名 | 类型 | 默认值 | 说明 |
---|---|---|---|
showOnSave | Boolean | true | 是否在保存时显示水印 |
text | String | '' | 水印文本内容 |
color | String | 'rgba(0, 0, 0, 0.1)' | 水印颜色,支持 rgba 格式 |
fontSize | Number | 16 | 水印字体大小 |
fontFamily | String | 'Arial' | 水印字体族 |
rotate | Number | -30 | 水印旋转角度(度) |
spacing | Number | 100 | 水印间距 |
single | Boolean | false | 是否只显示单个水印(true 为单个,false 为平铺) |
# 水印使用示例
1. 基础水印
watermark: { text: '电子签名', fontSize: 16, rotate: -30 }
✅ Copy success!
2. 单个水印
watermark: { text: '孙悟空', fontSize: 50, spacing: 120, rotate: 0, single: true }
✅ Copy success!
3. 平铺水印
watermark: { text: 'DRAFT', color: 'rgba(0, 0, 255, 0.15)', fontSize: 14, rotate: 0, spacing: 80, single: false }
✅ Copy success!
# 方法调用
可以通过 ref 调用组件的方法:
<template> <view> <u-signature ref="signatureRef"></u-signature> <view class="button-container"> <u-button @click="clear">清空</u-button> <u-button @click="undo">撤销</u-button> <u-button @click="confirm">完成</u-button> </view> </view> </template> <script> export default { methods: { clear() { this.$refs.signatureRef.clear(); }, undo() { this.$refs.signatureRef.undo(); }, confirm() { this.$refs.signatureRef.getImage().then(res => { console.log('签名图片', res); }); } } }; </script>
✅ Copy success!
# 自定义样式
可以通过 customStyle
和 toolbarStyle
自定义组件样式:
<template> <view class="signature-container"> <u-signature ref="signatureRef" :customStyle="customStyle" :toolbarStyle="toolbarStyle" ></u-signature> </view> </template> <script> export default { data() { return { customStyle: { border: '2px solid #e4e7ed', borderRadius: '8px' }, toolbarStyle: { backgroundColor: '#f5f7fa', padding: '10px' } } } }; </script>
✅ Copy success!
# 此页面源代码地址
# API
# Props
参数 | 说明 | 类型 | 默认值 | 可选值 |
---|---|---|---|---|
title | 标题 | String | 请签名 | - |
showTitle | 是否显示标题 | Boolean | true | - |
showToolbar | 是否显示工具栏 | Boolean | true | - |
showColorList | 是否显示颜色列表 | Boolean | true | - |
showClear | 是否显示清空按钮 | Boolean | true | - |
showUndo | 是否显示撤销按钮 | Boolean | true | - |
clearText | 清空按钮的文本 | String | 清空 | - |
undoText | 撤销按钮的文本 | String | 撤销 | - |
confirmText | 完成按钮的文本 | String | 完成 | - |
toolbarStyle | 工具栏对齐方式 | Object | {} | - |
fixed | 是否固定标题栏和工具栏 | Boolean | false | - |
minLineWidth | 线条最小宽度 | String | Number | 2 | - |
maxLineWidth | 线条最大宽度 | String | Number | 6 | - |
penColor | 画笔颜色 | String | #333333 | - |
penColorList | 画笔颜色列表 | Array | ['#333333', '#FF1E10', '#FFBE00', '#1A9BFF', '#1AAD19'] | - |
penSize | 画笔大小 | String | Number | 2 | - |
backgroundColor | 背景颜色 | String | #ffffff | - |
type | canvas类型 | String | 2d | - |
openSmooth | 是否开启压感 | Boolean | false | - |
maxHistoryLength | 最大历史记录数 | String | Number | 20 | - |
landscape | 是否横屏 | Boolean | false | - |
disableScroll | 是否禁用滚动 | Boolean | true | - |
disabled | 是否禁用 | Boolean | false | - |
boundingBox | 只生成内容区域 | Boolean | false | - |
customStyle | 自定义样式 | Object | String | {} | - |
fileType | 输出的图片类型 | String | png | png, jpg |
quality | 图片的质量,取值范围为 (0, 1) | Number | 1 | - |
showWatermark | 是否显示水印 | Boolean | false | - |
watermark | 水印配置对象 | Object | {} | - |
# Events
事件名 | 说明 | 回调参数 |
---|---|---|
clear | 清空签名时触发 | - |
undo | 撤销操作时触发 | - |
confirm | 完成签名时触发 | base64图片数据 |
# Methods
方法名 | 说明 | 参数 | 返回值 |
---|---|---|---|
clear | 清空签名 | - | - |
undo | 撤销上一步操作 | - | - |
getImage | 获取签名图片 | - | Promise<String> |
# Slots
名称 | 说明 |
---|---|
title | 自定义标题内容 |
toolbar | 自定义工具栏内容 |