Signature 簽名
手寫簽名畫布元件:canvas 2d 全端統一形態,pointer/touch 事件經 @lh-ui/share/core/signature 的 toSignaturePoint 歸一為畫布座標後,由 drawSmoothSegment(相鄰三點二次貝塞爾中點插值)增量繪製,視覺無折線。 undo 以筆跡快照(LhSignatureStroke[])為堆疊,出棧後 redrawSnapshot 重繪。 支援橡皮擦、基準輔助線、背景色、停用態與空態提示。
基礎用法
基礎簽名
vue
<script setup lang="ts">
import { ref } from 'vue'
import { LhSignature } from '@lh-ui/vue3'
const signatureRef = ref<InstanceType<typeof LhSignature>>()
function handleExport() {
const dataUrl = signatureRef.value?.toDataURL('image/png')
console.log(dataUrl)
}
</script>
<template>
<LhSignature ref="signatureRef" @end="handleExport" />
</template>筆色 / 線寬 / 背景色 / 輔助線
penColor 未傳時走令牌兜底深色(canvas 無法解析 CSS 變數,取 --lh-text-primary 令牌回退色 #1D2129);backgroundColor 設定後畫布 填滿該色,匯出點陣圖帶背景;guideline 繪製位於畫布 2/3 處的基準虛線:
外觀客製
vue
<LhSignature pen-color="#D9393B" :line-width="4" />
<LhSignature background-color="#F5F5F5" guideline :height="160" />橡皮擦與復原
eraser 開啟橡皮擦模式:未設 backgroundColor 時以 destination-out 合成態即時擦透;設有 backgroundColor 時按背景色覆蓋重繪(兩種實作均 在快照中登記,undo 出棧同步復原)。disabled 停用繪製(加 --disabled 修飾,樣式表切斷指標事件):
橡皮擦 / 復原 / 清空
vue
<LhSignature eraser />方法呼叫(ref 上 defineExpose):toDataURL(type, quality) 匯出點陣圖、 clear() 清空、undo() 復原最後一筆、isEmpty() 判空(快照空或像素 全為背景色)。
API
Signature Props
| 屬性 | 說明 | 類型 | 預設值 |
|---|---|---|---|
| penColor | 筆色(未傳走令牌兜底深色) | string | — |
| lineWidth | 線寬 px | number | 3 |
| backgroundColor | 背景色(設後畫布填滿該色,橡皮擦按背景色覆蓋重繪) | string | — |
| disabled | 停用繪製 | boolean | false |
| guideline | 輔助線(基準虛線,位於畫布 2/3 處) | boolean | false |
| height | 畫布高度 px | number | 200 |
| eraser | 橡皮擦模式(destination-out 擦除,設有 backgroundColor 時按背景色覆蓋重繪) | boolean | false |
| sx | sx 快速版面配置屬性 | LhSxProps | — |
Signature Events
| 事件名 | 說明 | 回呼參數 |
|---|---|---|
| start | 一筆開始(pointerdown) | — |
| signing | 繪製中(pointermove),攜帶歸一後的筆跡點 | (point: LhSignaturePoint) |
| end | 一筆結束(pointerup / pointercancel) | — |
Signature 方法(defineExpose)
| 方法名 | 說明 | 參數 |
|---|---|---|
| toDataURL | 匯出畫布點陣圖(canvas.toDataURL 包裝) | (type?: string, quality?: number) => string |
| clear | 清空快照與畫布(背景 / 輔助線一併重繪) | — |
| undo | 復原最後一筆(快照出棧 + 重繪) | — |
| isEmpty | 畫布是否為空(快照空或像素全為背景色) | () => boolean |
無障礙
根節點為 role="img",可達名稱固定為「簽名畫布」;空態提示 (__placeholder)pointer-events: none 且 aria-hidden,不干擾繪製。

