Signature 签名
签名组件:canvas 2d 形态的手写笔迹采集(H5 与小程序同构),笔迹平滑 (二次贝塞尔插值)、撤销快照、橡皮擦与 dataURL 导出全部消费 @lh-ui/share/core/signature 纯函数,端内只做 canvas 取上下文与触控归一。 服务表单签名、手写确认、涂鸦批注等场景。
平台差异(登记):本端为 canvas 2d 形态——触控事件统一为 touch(H5 与 MP 均为 touchstart/touchmove/touchend,
touches[0]取坐标), 经toSignaturePoint按画布位置归一后喂入 share core 的drawSmoothSegment增量平滑绘制;undo 走pushStroke/undoStroke快照 +redrawSnapshot整体重绘。canvas 取上下文复用本仓@/adapter/canvas(与 lh-qrcode / lh-chart 同一模式),dpr 缩放由适配层处理。
基础用法
<template>
<lh-signature :height="200" />
</template>自定义笔色与线宽
penColor 为笔色,lineWidth 为线宽(默认 3)。未传 penColor 时取令牌 字面量兜底色(canvas 不能消费 var(--lh-*) 字符串):
<lh-signature pen-color="#B87800" :line-width="5" />画布背景色与辅助线
backgroundColor 为画布背景色(未传为透明背景);guideline 开启基准 虚线(位于画布偏下 2/3 处,提示落笔基准区):
<lh-signature background-color="#F5F3EE" guideline />橡皮擦
eraser 开启橡皮擦模式:笔迹以 destination-out 合成擦除已有路径, 撤销栈里按笔记录擦除标记,重绘时第二轮擦除:
<lh-signature eraser />命令式方法(ref 调用)
<template>
<lh-signature ref="sigRef" />
<button @click="sigRef?.clear()">清空</button>
<button @click="sigRef?.undo()">撤销</button>
<button @click="onExport">导出</button>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import type { LhSignatureExposed } from '@lh-ui/uniapp'
const sigRef = ref<LhSignatureExposed | null>(null)
function onExport() {
const dataUrl = sigRef.value?.toDataURL('image/png') ?? ''
// 上传 / 预览 dataUrl
}
</script>事件
| 事件 | 说明 | 回调参数 |
|---|---|---|
| start | 落笔 | (point: LhSignaturePoint) |
| signing | 移笔(每个触控点) | (point: LhSignaturePoint) |
| end | 抬笔(完成一笔) | — |
LhSignaturePoint 为 { x: number, y: number, t?: number },坐标相对 画布左上角(CSS 逻辑像素)。
Props
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| penColor | 笔色 | string | 正文色兜底 |
| lineWidth | 线宽 | number | 3 |
| backgroundColor | 画布背景色 | string | —(透明) |
| disabled | 禁用绘制 | boolean | false |
| guideline | 辅助线(基准虚线) | boolean | false |
| height | 画布高度 px | number | 200 |
| eraser | 橡皮擦模式 | boolean | false |
| customClass | 自定义类名(透传到根节点) | string | — |
| sx | sx 快速布局属性 | LhSxProps | — |
方法(defineExpose)
| 方法 | 说明 | 参数 |
|---|---|---|
| toDataURL | 导出画布为 dataURL | (type?: string, quality?: number) |
| clear | 清空画布 | — |
| undo | 撤销上一笔 | — |
| isEmpty | 画布是否为空 | — → boolean |
无障碍
根节点携带 role="region" 语义与 aria-label(默认「签名画布」, 禁用时为「签名画布(已禁用)」),并落 aria-disabled。

