# 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