Skip to content

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 缩放由适配层处理。

基础用法 ​

vue
<template>
  <lh-signature :height="200" />
</template>

自定义笔色与线宽 ​

penColor 为笔色,lineWidth 为线宽(默认 3)。未传 penColor 时取令牌 字面量兜底色(canvas 不能消费 var(--lh-*) 字符串):

vue
<lh-signature pen-color="#B87800" :line-width="5" />

画布背景色与辅助线 ​

backgroundColor 为画布背景色(未传为透明背景);guideline 开启基准 虚线(位于画布偏下 2/3 处,提示落笔基准区):

vue
<lh-signature background-color="#F5F3EE" guideline />

橡皮擦 ​

eraser 开启橡皮擦模式:笔迹以 destination-out 合成擦除已有路径, 撤销栈里按笔记录擦除标记,重绘时第二轮擦除:

vue
<lh-signature eraser />

命令式方法(ref 调用) ​

vue
<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线宽number3
backgroundColor画布背景色string—(透明)
disabled禁用绘制booleanfalse
guideline辅助线(基准虚线)booleanfalse
height画布高度 pxnumber200
eraser橡皮擦模式booleanfalse
customClass自定义类名(透传到根节点)string—
sxsx 快速布局属性LhSxProps—

方法(defineExpose) ​

方法说明参数
toDataURL导出画布为 dataURL(type?: string, quality?: number)
clear清空画布—
undo撤销上一笔—
isEmpty画布是否为空— → boolean

无障碍 ​

根节点携带 role="region" 语义与 aria-label(默认「签名画布」, 禁用时为「签名画布(已禁用)」),并落 aria-disabled。