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,不干扰绘制。

