# Signature 签名
canvas 2d 手写签名板(绘制链路在 @lh-ui/share/core/signature:二次贝塞尔
平滑插值、点去抖、快照撤销与整幅重绘),根类 lh-signature,
样式来自 share styles/signature。
签名(笔色 / 线宽 / 辅助线 / 橡皮擦 / 撤销导出)
<template>
<div class="demo-col" style="gap: 18px; max-width: 480px">
<LhSignature
ref="sig"
:line-width="lineWidth"
:pen-color="penColor || undefined"
:background-color="bgColor || undefined"
:guideline="guideline"
:disabled="disabled"
:eraser="eraser"
:height="height"
label="收款签名"
@start="onStart"
@signing="onSigning"
@end="onEnd"
/>
<span class="demo-note">
事件计数:start {{ startCount }} / signing {{ signingCount }} / end {{ endCount }};空态:{{ empty }}
</span>
<div class="demo-row" style="gap: 8px; flex-wrap: wrap">
<LhButton size="small" @click="undo">撤销</LhButton>
<LhButton size="small" @click="clear">清空</LhButton>
<LhButton size="small" @click="exportData">导出 dataURL</LhButton>
<LhButton size="small" @click="guideline = !guideline">辅助线({{ guideline }})</LhButton>
<LhButton size="small" @click="eraser = !eraser">橡皮擦({{ eraser }})</LhButton>
<LhButton size="small" @click="disabled = !disabled">禁用({{ disabled }})</LhButton>
</div>
<div class="demo-row" style="gap: 8px; flex-wrap: wrap; align-items: center">
<LhInput v-model="penColor" style="width: 130px" placeholder="笔色(如 #A63D40)" />
<LhInput v-model="bgColor" style="width: 130px" placeholder="背景色(如 #F6F1EA)" />
<LhInputNumber v-model="lineWidth" :min="1" :max="12" />
<span class="demo-note">线宽 {{ lineWidth }}</span>
<LhInputNumber v-model="height" :min="80" :max="400" />
<span class="demo-note">高度 {{ height }}</span>
</div>
<span v-if="dataUrl" class="demo-note">dataURL 长度 {{ dataUrl.length }}(image/png)</span>
</div>
</template>
<script>
export default {
data: () => ({
lineWidth: 3,
penColor: '',
bgColor: '',
guideline: false,
eraser: false,
disabled: false,
height: 200,
startCount: 0,
signingCount: 0,
endCount: 0,
empty: true,
dataUrl: '',
}),
methods: {
// $refs 联合类型不含组件暴露方法,统一收紧后调用
sigRef() {
return this.$refs.sig
},
onStart() {
this.startCount++
},
onSigning() {
this.signingCount++
},
onEnd() {
this.endCount++
this.empty = this.sigRef().isEmpty()
},
undo() {
this.sigRef().undo()
this.empty = this.sigRef().isEmpty()
},
clear() {
this.sigRef().clear()
this.empty = this.sigRef().isEmpty()
},
exportData() {
this.dataUrl = this.sigRef().toDataURL('image/png')
},
},
}
</script>
# 绘制与事件
pointer 事件(统一鼠标 / 触摸)经 toSignaturePoint 归一为画布坐标点,
移动时 drawSmoothSegment 增量绘制相邻段;起笔抛 start,每个移动点抛
signing(参数为 { x, y, t }),收笔抛 end。松手后按快照整幅重绘,
保证增量段与撤销 / 重绘口径一致:
<LhSignature :line-width="3" pen-color="#A63D40" @start="onStart" @end="onEnd" />
# 撤销与清空
笔迹以快照存储(pushStroke),undo() 撤销最后一笔后按快照
redraw:清屏 → 背景填充 → 辅助线 → 逐笔重绘;clear() 清空全部;
isEmpty() 判空(无笔迹时展示占位文案):
<LhSignature ref="sig" />
<LhButton @click="$refs.sig.undo()">撤销</LhButton>
<LhButton @click="$refs.sig.clear()">清空</LhButton>
<LhButton @click="$refs.sig.toDataURL('image/png')">导出</LhButton>
# 辅助线 / 背景色 / 橡皮擦
guideline 绘制基准虚线(画布偏下 2/3 处);backgroundColor 在画布
底层填充纯色;eraser 切换为擦除模式(笔迹以 destination-out 叠加,
擦除路径),根加 --eraser 修饰。
# Props
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| penColor | 笔色(未传走 --lh-text-primary 令牌兜底) | string | — |
| lineWidth | 线宽 px | number | 3 |
| backgroundColor | 画布背景色(未传透明) | string | — |
| disabled | 禁用绘制(根加 --disabled) | boolean | false |
| guideline | 基准辅助线 | boolean | false |
| height | 画布高度 px(根容器高度) | number | 200 |
| eraser | 橡皮擦模式(擦除路径) | boolean | false |
| label | 无障碍可达名称(未传走默认「签名」文案) | string | — |
# Events
| 事件 | 说明 | 回调参数 |
|---|---|---|
| start | 起笔(pointerdown) | — |
| signing | 绘制中(每个移动点) | point: { x, y, t? } |
| end | 收笔(pointerup / cancel) | — |
# 方法(经 ref 调用)
| 方法 | 说明 | 参数 |
|---|---|---|
| toDataURL | 导出画布图片 dataURL | type?: string、quality?: number |
| clear | 清空画布(保留背景 / 辅助线) | — |
| undo | 撤销最后一笔 | — |
| isEmpty | 是否无笔迹 | — |
# 无障碍
根元素 role="img" + aria-label(label 可覆盖默认「签名」)。
无笔迹时展示 lh-signature__placeholder 占位文案,禁用时根加
lh-signature--disabled 修饰。
Copyright © 2024-2026 风默文学 | 宁ICP备2024006574号 |
苏公网安备32040002010537