# Signature 서명

canvas 2d 손글씨 서명 패드입니다(그리기 링크는 @lh-ui/share/core/signature: 이차 베지어 평활 보간, 점 디바운스, 스냅샷 실행 취소와 전체 다시 그리기), 루트 클래스는 lh-signature, 스타일은 share styles/signature에서 옵니다.

서명 (펜 색상 / 선 너비 / 보조선 / 지우개 / 실행 취소 내보내기)
<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="결제 서명"
      @start="onStart"
      @signing="onSigning"
      @end="onEnd"
    />
    <span class="demo-note">
      이벤트 카운트: start {{ startCount }} / signing {{ signingCount }} / end {{ endCount }}; 빈 상태: {{ empty }}
    </span>
    <div class="demo-row" style="gap: 8px; flex-wrap: wrap">
      <LhButton size="small" @click="undo">실행 취소</LhButton>
      <LhButton size="small" @click="clear">지우기</LhButton>
      <LhButton size="small" @click="exportData">dataURL 내보내기</LhButton>
      <LhButton size="small" @click="guideline = !guideline">보조선 ({{ guideline }})</LhButton>
      <LhButton size="small" @click="eraser = !eraser">지우개 ({{ eraser }})</LhButton>
      <LhButton size="small" @click="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="펜 색상 (예: #A63D40)" />
      <LhInput v-model="bgColor" style="width: 130px" placeholder="배경색 (예: #F6F1EA)" />
      <LhInputNumber v-model="lineWidth" :min="1" :max="12" />
      <span class="demo-note">선 너비 {{ lineWidth }}</span>
      <LhInputNumber v-model="height" :min="80" :max="400" />
      <span class="demo-note">높이 {{ height }}</span>
    </div>
    <span v-if="dataUrl" class="demo-note">dataURL 길이 {{ 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: {
    // $refs 유니언 타입에는 컴포넌트가 노출한 메서드가 없어, 통일적으로 좁혀 호출
    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>

# 그리기와 이벤트

pointer 이벤트(마우스 / 터치 통일)는 toSignaturePoint로 캔버스 좌표 점으로 정규화되고, 이동 시 drawSmoothSegment가 인접 세그먼트를 증분 그리기합니다; 시작하면 start를, 각 이동 점마다 signing(매개변수 { x, y, t })을, 끝내면 end를 내보냅니다. 손을 뗀 뒤 스냅샷을 기준으로 전체를 다시 그려, 증분 세그먼트와 실행 취소 / 다시 그리기 기준을 일치시킵니다:

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

# 실행 취소와 지우기

필적은 스냅샷으로 저장됩니다(pushStroke), undo()는 마지막 획을 실행 취소한 뒤 스냅샷 기준으로 redraw합니다: 화면 지움 → 배경 채우기 → 보조선 → 획별 다시 그리기; clear()는 전체를 지웁니다; isEmpty()는 빈 여부를 판정합니다(필적이 없을 때 플레이스홀더 텍스트 표시):

<LhSignature ref="sig" />
<LhButton @click="$refs.sig.undo()">실행 취소</LhButton>
<LhButton @click="$refs.sig.clear()">지우기</LhButton>
<LhButton @click="$refs.sig.toDataURL('image/png')">내보내기</LhButton>

# 보조선 / 배경색 / 지우개

guideline은 기준 점선을 그립니다(캔버스 아래쪽 2/3 지점); backgroundColor는 캔버스 밑바닥에 순색을 채웁니다; eraser는 지우기 모드로 전환합니다(필적을 destination-out 합성으로 지우는 경로), 루트에 --eraser 수정자를 추가합니다.

# Props

프로퍼티 설명 타입 기본값
penColor 펜 색상 (미전달 시 --lh-text-primary 토큰 폴백) string —
lineWidth 선 너비 px number 3
backgroundColor 캔버스 배경색 (미전달 시 투명) string —
disabled 그리기 비활성화 (루트에 --disabled 추가) boolean false
guideline 기준 보조선 boolean false
height 캔버스 높이 px (루트 컨테이너 높이) number 200
eraser 지우개 모드 (경로 지움) boolean false
label 접근성 접근 가능한 이름 (미전달 시 기본 '서명' 텍스트 사용) string —

# Events

이벤트 설명 콜백 매개변수
start 시작 (pointerdown) —
signing 그리는 중 (각 이동 점) point: { x, y, t? }
end 끝내기 (pointerup / cancel) —

# 메서드(ref로 호출)

메서드 설명 매개변수
toDataURL 캔버스 이미지 dataURL 내보내기 type?: string, quality?: number
clear 캔버스 지우기 (배경 / 보조선 유지) —
undo 마지막 획 실행 취소 —
isEmpty 필적이 없는지 여부 —

# 접근성

루트 요소 role="img" + aria-label(label이 기본 '서명'을 덮어쓸 수 있음). 필적이 없을 때 lh-signature__placeholder 플레이스홀더 텍스트를 표시하고, 비활성화 시 루트에 lh-signature--disabled 수정자를 추가합니다.

최근 업데이트: 2026. 12. 4. 오전 12:49:57

Copyright © 2024-2026 FengMo WenXue