Signature
Records vector strokes with undo and redo history, and exports PNG or JPG through platform adapters.
Import
Use <hui-signature> after configuring uni-app easycom. Import public types and functions from @nscode/handy-uni. Ordinary Vue applications install the @nscode/handy-uni/web preview plugin.
Examples
Each example is standalone. Controls show the current value or event result on the page.
<template>
<view>
<hui-signature
:height="240"
:before-clear="beforeClear"
@start="lastEvent = 'start'"
@signing="(point) => (lastEvent = 'signing:' + JSON.stringify(point))"
@end="lastEvent = 'end'"
@clear="lastEvent = 'clear'"
@change="(value) => (lastEvent = JSON.stringify(value))"
@confirm="(value) => (lastEvent = value.width + '×' + value.height)"
@error="(value) => (lastEvent = value.message)"
/>
<text>{{ lastEvent }}</text>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const lastEvent = ref('尚无事件 / No event')
import { showConfirmDialog } from '@nscode/handy-uni'
const beforeClear = async () => {
try {
await showConfirmDialog({
title: '清空签名 / Clear signature',
message: '确定删除当前全部笔迹吗? / Delete all current strokes?'
})
return true
} catch {
return false
}
}
</script>
Props
| Prop | Type | Default | Details |
|---|---|---|---|
height | number | string | 200 | Height; numbers use px and strings preserve their unit. |
penColor | string | '#000' | Stroke color captured when each stroke starts. |
lineWidth | number | 3 | Fixed stroke width, in px. |
minLineWidth | number | — | Minimum speed-adjusted stroke width. |
maxLineWidth | number | — | Maximum speed-adjusted stroke width. |
backgroundColor | string | '' | Canvas background; omitted PNG stays transparent and omitted JPG uses white. |
fileType | 'png' | 'jpg' | 'png' | Export format: png or jpg. |
quality | number | 0.92 | JPG export quality from 0 to 1. |
exportScale | number | — | Output pixel scale, capped so the longest output side does not exceed 4096. |
exportRotate | 0 | 90 | 180 | 270 | — | Clockwise output rotation; landscape defaults to 270 degrees. |
landscape | boolean | false | Rotates the prompt and actions for sideways signing without changing touch coordinates. |
placeholder | string | — | Placeholder text or measured fixed-bar placeholder switch, depending on component. |
disabled | boolean | false | Disables user interaction. |
toolbar | boolean | true | Displays signature actions. |
beforeClear | () => boolean | void | Promise<boolean | void> | — | Clear guard. Clears directly when omitted; callers provide confirmation when needed. Cancellation and stale results preserve strokes. |
Root classes and styles use customClass and customStyle. Numbers representing dimensions use px; durations use ms unless explicitly documented.
Events
| Event | Arguments |
|---|---|
start | () |
signing | (point: { x:number;y:number }) |
end | () |
change | (state: { empty:boolean;canUndo:boolean;canRedo:boolean }) |
clear | () |
confirm | (result: SignatureResult) |
error | (error: HuiPlatformError) |
Controlled state only changes when the parent accepts its update event. Events with multiple words use kebab-case. Disabled or stale asynchronous actions do not emit a successful state change.
Slots
| Slot | Scope |
|---|---|
placeholder | — |
toolbar | { clear, undo, redo, confirm, empty, canUndo, canRedo, exporting } |
Methods
| Method | Signature | Description |
|---|---|---|
clear | (): void | Explicitly clear all strokes and history. |
undo | (): void | Undo the last stroke. |
redo | (): void | Redo an undone stroke. |
isEmpty | (): boolean | Whether there are no effective strokes. |
exportImage | (options?: SignatureExportOptions): Promise<SignatureResult | null> | Flush drawing and export. |
resize | (): Promise<void> | Rebuild and replay at container size. |
Types
SignatureExportOptions、SignatureResult、SignatureProps、SignatureEmits、SignatureExpose.
Platform notes
The component does not include a clear confirmation dialog and clears directly when beforeClear is omitted. Callers can provide confirmation through beforeClear; false, rejection, thrown errors and stale results preserve strokes. clear() is an explicit programmatic cleanup. PNG is transparent by default and JPG defaults to white; the longest output side is capped at 4096. WeChat uses a 2d node, App-vue serializes legacy draw callbacks, and H5 avoids double scaling. Sideways signing rotates prompts and buttons; output defaults to 270 degrees. Resizing replays original coordinates, so signing again is recommended. Check canvas overlay stacking on devices. Release exported files after use.
Device smoke checklist.