Skip to content

Signature Component

Used for signature scenarios, a signature component implemented based on Canvas. Provides basic signing, history records, pen pressure effects, and other features.

📌 Platform Differences

APP(vue)H5WeChat Mini ProgramAlipay Mini Program

⚠️ Notes

Notes

  • If the exported image is not clear, you can set exportScale to 2 or higher.

1. Unable to Draw in a Popup

Cause

  • When used in a WeChat Mini Program popup, if the popup has not opened but the component's onMounted has already executed, initialization will fail. You need to call the initialization method again after the popup opens:
html
<template>
    <hy-button text="Open Popup" @click="onOpen"></hy-button>
    <hy-popup v-model:show="show">
        <hy-signature ref="signatureRef"></hy-signature>
    </hy-popup>
</template>
ts
const signatureRef = ref<>()
const show = ref(false)

const onOpen = () => {
  show.value = true;
  setTimeout(() => signatureRef.value?.init(), 500)
}

🏯 Basic Usage Examples

html
<!-- Global usage -->
<hy-signature></hy-signature>

Custom Brush

  • Set the stroke width by configuring lineWidth
  • Set the stroke color by configuring pen-color
html
<template>
    <hy-signature pen-color="red" :lineWidth="5"></hy-signature>
</template>

History Operations

  • Enable or disable history operations by configuring enableHistory
html
<template>
    <hy-signature enableHistory></hy-signature>
</template>

Custom Pen Pressure Parameters

  • Enable pen pressure mode via pressure to simulate real handwriting. In pen pressure mode, stroke thickness varies with writing speed.
  • min-width: the minimum stroke width, i.e., the line thickness when writing fast
  • max-width: the maximum stroke width, i.e., the line thickness when writing slowly
  • min-speed: the speed threshold, used to adjust pressure sensitivity
html
<template>
    <hy-signature pressure :min-width="1" :max-width="6" :min-speed="1.5"></hy-signature>
</template>

Custom Buttons

  • Customize the bottom buttons via the footer slot.
html
<template>
    <hy-signature enableHistory>
        <template #footer="{ clear, confirm, currentStep, restore, revoke, historyList }">
            <hy-button block @click="changeDisabled" v-if="disabled">Start Signing</hy-button>
            <block v-if="!disabled">
                <hy-button size="small" plain @click="revoke" :disabled="currentStep <= 0">
                    Undo
                </hy-button>
                <hy-button
                    size="small"
                    plain
                    @click="restore"
                    :disabled="currentStep >= historyList.length"
                >
                    Redo
                </hy-button>
                <hy-button size="small" plain @click="clear">Clear</hy-button>
                <hy-button size="small" @click="confirm">Confirm</hy-button>
            </block>
        </template>
    </hy-signature>
</template>

Landscape Signature Page

You can implement a landscape signature page by configuring the page's pageOrientation

vue
<template>
    <view class="landscape-signature">
        <hy-signature
            v-if="inited"
            :height="height"
            :width="width"
            enable-history
            pressure
            background-color="#f5f5f5"
            @confirm="handleConfirm"
        >
            <template #footer="{ clear, confirm, restore, revoke, canUndo, canRedo }">
                <view class="custom-actions">
                    <view class="button-group">
                        <hy-button size="small" plain @click="revoke" :disabled="!canUndo">
                            Undo
                        </hy-button>
                        <hy-button size="small" plain @click="restore" :disabled="!canRedo">
                            Redo
                        </hy-button>
                        <hy-button size="small" plain @click="clear">Clear</hy-button>
                        <hy-button size="small" type="primary" @click="confirm">Done</hy-button>
                    </view>
                </view>
            </template>
        </hy-signature>
    </view>
</template>
ts
import { sleep } from '@hy-app/ui';

const height = ref(0);
const width = ref(0);
const inited = ref(false);

onMounted(() => {
    const { windowWidth, windowHeight } = uni.getSystemInfoSync();
    width.value = windowWidth - 48;
    height.value = windowHeight - 48;
    sleep(100).then(() => {
        inited.value = true;
    });
});
scss
.landscape-signature {
    height: 100vh;
    // #ifdef H5
    height: calc(100vh - 44px);
    // #endif
    background: #fff;
    position: relative;
    padding: 24px 0;
    padding-left: 48px;
    box-sizing: border-box;

    .custom-actions {
        position: fixed;
        left: 0;
        top: 50%;
        width: 48px;
        transform: translateY(-50%) rotate(90deg);
        transform-origin: center;
        z-index: 10;

        .button-group {
            display: flex;
            flex-direction: row;
            gap: 12px;
            white-space: nowrap;
            width: max-content;
            transform: translateX(-50%);
        }
    }
}

API

Signature Props

ParameterDescriptionTypeDefault
pen-colorSignature pen colorstring#000000
line-widthSignature pen widthnumber3
heightCanvas height, default unit px for numbersnumber|string-
widthCanvas width, default unit px for numbersnumber|string-
clear-textText of the clear buttonstring-
confirm-textText of the confirm buttonstring-
file-typeExported image typestringpng
qualityExported image quality (0-1)number1
export-scaleScale ratio of the exported imagenumber1
disabledWhether to disable the signature padbooleanfalse
background-colorBackground color of the canvasstring-
disable-scrollWhether to disable canvas scrollingbooleantrue
enable-historyWhether to enable historybooleanfalse
stepHistory step sizenumber1
pressureWhether to enable pen pressure modebooleanfalse
min-widthMinimum width in pen pressure modenumber2
max-widthMaximum width in pen pressure modenumber6
min-speedSpeed threshold in pen pressure modenumber1.5

Events

Event NameDescriptionCallback Parameters
startTriggered when signing startsevent: TouchEvent
endTriggered when signing endsevent: TouchEvent
signingTriggered during signingevent: TouchEvent
confirmTriggered on signature confirmresult: SignatureResult
clearTriggered when signature is cleared-

Methods

NameDescriptionParameters
initInitialize the signature padforceUpdate?: boolean
confirmConfirm the signature-
clearClear the signature-
restoreRedo the previous step-
revokeUndo the previous step-

Slots

Slot NameDescriptionReceived Values
footerCustom bottom buttonsclear, confirm, restore, revoke, currentStep, historyList
01:10