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
200px; the canvas backing store scales withdevicePixelRatio, so strokes and exports stay crisp on retina screens. guidelinedraws a dashed baseline at 2/3 height;backgroundColorfills the canvas before painting (also used by export).eraserswitches strokes todestination-outcompositing 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__placeholderuntil the first stroke is committed.
Basic usage
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
| Prop | Description | Type | Default |
|---|---|---|---|
| penColor | Stroke color (falls back to the canvas default black) | string | canvas default |
| lineWidth | Stroke width in px | number | 3 |
| backgroundColor | Canvas background fill | string | transparent |
| disabled | Disable drawing (adds --disabled, blocks pointers) | boolean | false |
| guideline | Dashed baseline at 2/3 height | boolean | false |
| height | Canvas height in px | number | 200 |
| eraser | Eraser mode (destination-out compositing) | boolean | false |
| onStart | Fired on stroke start (normalized point) | (point: LhSignaturePoint) => void | — |
| onSigning | Fired for every move point | (point: LhSignaturePoint) => void | — |
| onEnd | Fired when a stroke is committed | () => void | — |
| label | Accessible name (aria-label) | string | '签名画布' |
| sx | Quick layout props | LhSxProps | — |
LhSignatureRef
| Method | Description | Signature |
|---|---|---|
| toDataURL | Export the canvas as a data URL (null when no canvas context is available) | (type?: string, quality?: number) => string | null |
| clear | Clear all strokes and repaint the base layer | () => void |
| undo | Undo the last stroke (snapshot pop + repaint) | () => void |
| isEmpty | Blank state (snapshot based) | () => boolean |
Accessibility
- The root carries
role="img"witharia-label(thelabelprop or the签名画布fallback), so screen readers announce the pad. - The disabled state is conveyed by the
lh-signature--disabledmodifier (pointer blocking plus reduced opacity); the eraser state is conveyed by thelh-signature--erasercursor hint. - Drawing is pointer-only; if you need keyboard equivalents, wrap the pad and
offer
undo/clearbuttons (as in the demo above).