跳到主要内容

Signature

Canvas-based signature pad. Pointer events (mouse / touch / pen) are normalized to LhSignaturePoint (layout offset from canvas.getBoundingClientRect(), scale = physical / CSS pixels), each segment is drawn incrementally with drawSmoothSegment (quadratic Bezier mid-point smoothing), and undo works on stroke snapshots redrawn via redrawSnapshot. All smoothing, deduplication and blank-state logic is consumed from @lh-ui/share/core/signature pure functions, so the semantics are identical across platforms.

  • Default height is 200 px; the canvas backing store scales with devicePixelRatio, so strokes and exports stay crisp on retina screens.
  • guideline draws a dashed baseline at 2/3 height; backgroundColor fills the canvas before painting (also used by export).
  • eraser switches strokes to destination-out compositing to erase pixels; every stroke is deduplicated (dedupePoints) before entering the undo snapshot, so jitter does not bloat the stack.
  • The empty state renders lh-signature__placeholder until the first stroke is committed.

Basic usage​

Signature (guideline / undo / clear / export preview / eraser)
isEmpty: true
import { LhSignature } from '@lh-ui/react'

export default function Demo() {
return (
<>
<LhSignature guideline height={180} penColor="#1D2129" backgroundColor="#FFFFFF" />
<LhSignature eraser height={120} />
</>
)
}

Drawing callbacks​

onStart / onSigning / onEnd align with the start / signing / end events of the other platforms. onSigning receives every normalized point, which is handy for stroke statistics:

<LhSignature
onStart={(point) => console.log('落笔', point)}
onSigning={(point) => console.log('行笔', point)}
onEnd={() => console.log('收笔')}
/>

Imperative API (ref)​

Commands are exposed via forwardRef + useImperativeHandle and work in both controlled and uncontrolled forms:

import { useRef } from 'react'
import { LhSignature, type LhSignatureRef } from '@lh-ui/react'

const ref = useRef<LhSignatureRef>(null)
ref.current?.toDataURL('image/png') // export the canvas (type / quality passthrough)
ref.current?.undo() // pop the last stroke and repaint
ref.current?.clear() // clear all strokes and repaint the base layer
ref.current?.isEmpty() // snapshot-based blank state

Signature API​

PropDescriptionTypeDefault
penColorStroke color (falls back to the canvas default black)stringcanvas default
lineWidthStroke width in pxnumber3
backgroundColorCanvas background fillstringtransparent
disabledDisable drawing (adds --disabled, blocks pointers)booleanfalse
guidelineDashed baseline at 2/3 heightbooleanfalse
heightCanvas height in pxnumber200
eraserEraser mode (destination-out compositing)booleanfalse
onStartFired on stroke start (normalized point)(point: LhSignaturePoint) => void—
onSigningFired for every move point(point: LhSignaturePoint) => void—
onEndFired when a stroke is committed() => void—
labelAccessible name (aria-label)string'签名画布'
sxQuick layout propsLhSxProps—

LhSignatureRef​

MethodDescriptionSignature
toDataURLExport the canvas as a data URL (null when no canvas context is available)(type?: string, quality?: number) => string | null
clearClear all strokes and repaint the base layer() => void
undoUndo the last stroke (snapshot pop + repaint)() => void
isEmptyBlank state (snapshot based)() => boolean

Accessibility​

  • The root carries role="img" with aria-label (the label prop or the 签名画布 fallback), so screen readers announce the pad.
  • The disabled state is conveyed by the lh-signature--disabled modifier (pointer blocking plus reduced opacity); the eraser state is conveyed by the lh-signature--eraser cursor hint.
  • Drawing is pointer-only; if you need keyboard equivalents, wrap the pad and offer undo / clear buttons (as in the demo above).