# Signature

A canvas 2d handwriting signature pad (the drawing pipeline lives in @lh-ui/share/core/signature: quadratic Bezier smoothing interpolation, point debouncing, snapshot undo, and full redraw). Root class lh-signature, styles come from share styles/signature.

Signature (pen color / line width / guideline / eraser / undo and export)
<template>
  <div class="demo-col" style="gap: 18px; max-width: 480px">
    <LhSignature
      ref="sig"
      :line-width="lineWidth"
      :pen-color="penColor || undefined"
      :background-color="bgColor || undefined"
      :guideline="guideline"
      :disabled="disabled"
      :eraser="eraser"
      :height="height"
      label="Payment signature"
      @start="onStart"
      @signing="onSigning"
      @end="onEnd"
    />
    <span class="demo-note">
      Event counts: start {{ startCount }} / signing {{ signingCount }} / end {{ endCount }}; empty: {{ empty }}
    </span>
    <div class="demo-row" style="gap: 8px; flex-wrap: wrap">
      <LhButton size="small" @click="undo">Undo</LhButton>
      <LhButton size="small" @click="clear">Clear</LhButton>
      <LhButton size="small" @click="exportData">Export dataURL</LhButton>
      <LhButton size="small" @click="guideline = !guideline">Guideline ({{ guideline }})</LhButton>
      <LhButton size="small" @click="eraser = !eraser">Eraser ({{ eraser }})</LhButton>
      <LhButton size="small" @click="disabled = !disabled">Disabled ({{ disabled }})</LhButton>
    </div>
    <div class="demo-row" style="gap: 8px; flex-wrap: wrap; align-items: center">
      <LhInput v-model="penColor" style="width: 130px" placeholder="Pen color (e.g. #A63D40)" />
      <LhInput v-model="bgColor" style="width: 130px" placeholder="Background color (e.g. #F6F1EA)" />
      <LhInputNumber v-model="lineWidth" :min="1" :max="12" />
      <span class="demo-note">Line width {{ lineWidth }}</span>
      <LhInputNumber v-model="height" :min="80" :max="400" />
      <span class="demo-note">Height {{ height }}</span>
    </div>
    <span v-if="dataUrl" class="demo-note">dataURL length {{ dataUrl.length }} (image/png)</span>
  </div>
</template>

<script>
export default {
  data: () => ({
    lineWidth: 3,
    penColor: '',
    bgColor: '',
    guideline: false,
    eraser: false,
    disabled: false,
    height: 200,
    startCount: 0,
    signingCount: 0,
    endCount: 0,
    empty: true,
    dataUrl: '',
  }),
  methods: {
    // The $refs union type does not include the component's exposed methods; narrow it before calling
    sigRef() {
      return this.$refs.sig
    },
    onStart() {
      this.startCount++
    },
    onSigning() {
      this.signingCount++
    },
    onEnd() {
      this.endCount++
      this.empty = this.sigRef().isEmpty()
    },
    undo() {
      this.sigRef().undo()
      this.empty = this.sigRef().isEmpty()
    },
    clear() {
      this.sigRef().clear()
      this.empty = this.sigRef().isEmpty()
    },
    exportData() {
      this.dataUrl = this.sigRef().toDataURL('image/png')
    },
  },
}
</script>

# Drawing and events

Pointer events (unifying mouse / touch) are normalized to canvas coordinates by toSignaturePoint; on move, drawSmoothSegment draws the adjacent segments incrementally. A stroke start emits start, every moving point emits signing (payload { x, y, t }), and the stroke end emits end. After release the whole canvas is redrawn from snapshots so the incremental segments stay consistent with the undo / redraw path:

<LhSignature :line-width="3" pen-color="#A63D40" @start="onStart" @end="onEnd" />

# Undo and clear

Strokes are stored as snapshots (pushStroke); undo() removes the last stroke and redraws from the snapshot via redraw: clear → background fill → guideline → stroke-by-stroke redraw. clear() clears everything, and isEmpty() reports emptiness (a placeholder text shows when there is no stroke):

<LhSignature ref="sig" />
<LhButton @click="$refs.sig.undo()">Undo</LhButton>
<LhButton @click="$refs.sig.clear()">Clear</LhButton>
<LhButton @click="$refs.sig.toDataURL('image/png')">Export</LhButton>

# Guideline / background color / eraser

guideline draws a dashed baseline (at the lower 2/3 of the canvas); backgroundColor fills a solid color at the canvas base; eraser switches to erase mode (strokes composite with destination-out, erasing the path), and the root gets the --eraser modifier.

# Props

Prop Description Type Default
penColor Pen color (falls back to the --lh-text-primary token when not passed) string —
lineWidth Line width in px number 3
backgroundColor Canvas background color (transparent when not passed) string —
disabled Disable drawing (adds --disabled to the root) boolean false
guideline Baseline guideline boolean false
height Canvas height in px (the root container's height) number 200
eraser Eraser mode (erases the path) boolean false
label Accessibility name (falls back to the default "Signature" message) string —

# Events

Event Description Callback args
start Stroke start (pointerdown) —
signing Drawing (every moving point) point: { x, y, t? }
end Stroke end (pointerup / cancel) —

# Methods (called via ref)

Method Description Params
toDataURL Export the canvas image as a dataURL type?: string, quality?: number
clear Clear the canvas (keeps the background / guideline) —
undo Undo the last stroke —
isEmpty Whether there is no stroke —

# Accessibility

The root element has role="img" + aria-label (label can override the default "Signature"). When there is no stroke, the lh-signature__placeholder placeholder text shows; when disabled, the root gets the lh-signature--disabled modifier.

Last Updated: 12/4/2026, 12:49:57 AM

Copyright © 2024-2026 FengMo WenXue