Skip to content

Signature ​

Signature component: canvas 2d handwriting capture (identical on web and mini-programs). Stroke smoothing (quadratic Bezier interpolation), undo snapshots, eraser mode and dataURL export all reuse the pure functions from @lh-ui/share/core/signature; the port only fetches the canvas context and normalizes touch input. Serves form signatures, handwritten confirmations and sketch annotations.

Platform differences (registered): this port uses the canvas 2d form — touch events are unified as touch (touchstart / touchmove / touchend on both H5 and mini-programs, coordinates taken from touches[0]), normalized with toSignaturePoint against the canvas position and fed into share core's drawSmoothSegment for incremental smooth drawing; undo goes through pushStroke / undoStroke snapshots plus a full redrawSnapshot repaint. The canvas context is fetched via @/adapter/canvas (the same pattern as lh-qrcode / lh-chart), with dpr scaling handled by the adapter layer.

Basic usage ​

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

Pen color and line width ​

penColor sets the pen color and lineWidth the line width (3 by default). When penColor is omitted, the literal token fallback color is used (canvas cannot consume var(--lh-*) strings):

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

Background color and guideline ​

backgroundColor sets the canvas background (transparent when omitted); guideline enables the dashed baseline (at roughly two-thirds down the canvas, hinting the baseline area):

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

Eraser ​

eraser enables the eraser mode: strokes erase existing paths with the destination-out composite operation, and erase flags are recorded per stroke in the undo stack so repaints apply a second erase pass:

vue
<lh-signature eraser />

Imperative methods (via ref) ​

vue
<template>
  <lh-signature ref="sigRef" />
  <button @click="sigRef?.clear()">Clear</button>
  <button @click="sigRef?.undo()">Undo</button>
  <button @click="onExport">Export</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') ?? ''
  // upload / preview dataUrl
}
</script>

Events ​

EventDescriptionCallback args
startPen down(point: LhSignaturePoint)
signingPen moving (each touch point)(point: LhSignaturePoint)
endPen up (stroke finished)—

LhSignaturePoint is { x: number, y: number, t?: number }; coordinates are relative to the canvas top-left corner (CSS logical pixels).

Props ​

PropDescriptionTypeDefault
penColorPen colorstringbody color fallback
lineWidthLine widthnumber3
backgroundColorCanvas background colorstring— (transparent)
disabledDisable drawingbooleanfalse
guidelineGuideline (dashed baseline)booleanfalse
heightCanvas height in pxnumber200
eraserEraser modebooleanfalse
customClassCustom class (forwarded to the root node)string—
sxsx quick layout propsLhSxProps—

Methods (defineExpose) ​

MethodDescriptionArgs
toDataURLExport the canvas as a dataURL(type?: string, quality?: number)
clearClear the canvas—
undoUndo the last stroke—
isEmptyWhether the canvas is blank— → boolean

Accessibility ​

The root node carries the role="region" semantics and an aria-label ("Signature canvas" by default, "Signature canvas (disabled)" when disabled), plus aria-disabled.