Skip to content
API 风格:

Signature ​

A handwritten signature canvas component: a unified canvas 2d form across platforms. Pointer/touch events are normalized to canvas coordinates by toSignaturePoint in @lh-ui/share/core/signature, then incrementally drawn by drawSmoothSegment (quadratic Bézier midpoint interpolation over every three adjacent points), with no polyline artifacts. Undo works on a stack of stroke snapshots (LhSignatureStroke[]); on pop, redrawSnapshot redraws. Supports an eraser, a baseline guide, background color, a disabled state and an empty-state hint.

Basic Usage ​

Basic signature
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>

Pen color / line width / background color / guide line ​

When penColor is not passed, it falls back to the token's dark color (canvas cannot resolve CSS variables, so the --lh-text-primary token fallback #1D2129 is used); once backgroundColor is set, the canvas is filled with that color and the exported bitmap carries the background; guideline draws a baseline dashed line at 2/3 of the canvas height:

Appearance customization
vue
<LhSignature pen-color="#D9393B" :line-width="4" />
<LhSignature background-color="#F5F5F5" guideline :height="160" />

Eraser and undo ​

eraser enables the eraser mode: when backgroundColor is not set, it erases through instantly with the destination-out composite mode; when backgroundColor is set, it redraws with the background color covering the strokes (both implementations register in the snapshot, so an undo pops and reverts them synchronously). disabled disables drawing (adds the --disabled modifier; the stylesheet cuts off pointer events):

Eraser / undo / clear
vue
<LhSignature eraser />

Method calls (defineExpose on the ref): toDataURL(type, quality) exports a bitmap, clear() clears the canvas, undo() reverts the last stroke, and isEmpty() checks for emptiness (the snapshot is empty, or all pixels are the background color).

API ​

Signature Props ​

PropDescriptionTypeDefault
penColorPen color (falls back to the token's dark color when not passed)string—
lineWidthLine width in pxnumber3
backgroundColorBackground color (when set, the canvas is filled with that color and the eraser redraws with the background color)string—
disabledDisable drawingbooleanfalse
guidelineGuide line (a baseline dashed line at 2/3 of the canvas height)booleanfalse
heightCanvas height in pxnumber200
eraserEraser mode (destination-out erasing; when backgroundColor is set, redraws with the background color)booleanfalse
sxsx quick-layout propsLhSxProps—

Signature Events ​

EventDescriptionCallback
startA stroke starts (pointerdown)—
signingDrawing in progress (pointermove), carrying the normalized stroke point(point: LhSignaturePoint)
endA stroke ends (pointerup / pointercancel)—

Signature Methods (defineExpose) ​

MethodDescriptionParameters
toDataURLExport a canvas bitmap (a canvas.toDataURL wrapper)(type?: string, quality?: number) => string
clearClear the snapshot and the canvas (redraws the background / guide line together)—
undoRevert the last stroke (snapshot pop + redraw)—
isEmptyWhether the canvas is empty (the snapshot is empty or all pixels are the background color)() => boolean

Accessibility ​

The root node is role="img" with the fixed accessible name "Signature canvas"; the empty-state hint (__placeholder) has pointer-events: none and aria-hidden, so it does not interfere with drawing.

Beauty of tradition, foundation of code