Skip to content

Signature 签名

介绍

签名组件用于签名场景,基于 Canvas 实现,支持自定义线条颜色、宽度和背景色等。

导入

js
import Signature from '@/components/form/Signature.vue';

使用方法

基础用法

签名组件的基础用法,可通过 ref 调用组件方法。

vue
<template>
  <Signature ref="signatureRef" />
  <FlexRow :padding="10" :gap="10">
    <Button round size="small" text="清空" @click="signatureRef?.clear()" />
    <Button round size="small" type="primary" text="确定" @click="previewResult(signatureRef)" />
  </FlexRow>
</template>

<script setup lang="ts">
import Button from '@/components/basic/Button.vue';
import FlexRow from '@/components/layout/FlexRow.vue';
import type { SignatureInstance } from '@/components/form/Signature.vue';
import Signature from '@/components/form/Signature.vue';
import { ref } from 'vue';

const signatureRef = ref<SignatureInstance>();

function previewResult(ref: SignatureInstance|undefined) {
  if (ref) {
    uni.showLoading();
    ref.export().then((res) => {
      uni.previewImage({
        urls:[res],
      })
    }).catch((e) => {
      uni.showToast({
        title: e.message,
        icon: 'none',
      })
    }).finally(() => {
      uni.hideLoading();
    });
  }
}
</script>

自定义线条颜色

通过 lineColor 属性自定义签名线条颜色。

vue
<template>
  <Signature ref="signatureRef" lineColor="#f00" />
  <FlexRow :padding="10" :gap="10">
    <Button round size="small" text="清空" @click="signatureRef?.clear()" />
    <Button round size="small" type="primary" text="确定" @click="previewResult(signatureRef)" />
  </FlexRow>
</template>

自定义线条宽度

通过 lineWidth 属性自定义签名线条宽度。

vue
<template>
  <Signature ref="signatureRef" :lineWidth="5" />
  <FlexRow :padding="10" :gap="10">
    <Button round size="small" text="清空" @click="signatureRef?.clear()" />
    <Button round size="small" type="primary" text="确定" @click="previewResult(signatureRef)" />
  </FlexRow>
</template>

自定义背景颜色

通过 backgroundColor 属性自定义签名背景颜色。

vue
<template>
  <Signature ref="signatureRef" backgroundColor="#000" lineColor="#fff" />
  <FlexRow :padding="10" :gap="10">
    <Button round size="small" text="清空" @click="signatureRef?.clear()" />
    <Button round size="small" type="primary" text="确定" @click="previewResult(signatureRef)" />
  </FlexRow>
</template>

API 参考

Signature

签名组件,用于用户手写签名。

Props

名称说明类型必填默认值
backgroundColor签名背景颜色stringwhite
lineColor签名线条颜色stringblack
lineWidth签名线条宽度number3
round是否显示圆角booleantrue
border是否显示边框booleantrue
borderWidth边框宽度number2
borderColor边框颜色stringborder.cell

Methods

名称说明参数返回值描述
clear清空签名内容-void清空当前画布上的所有签名内容
export导出签名为图片-Promise<string>导出签名为临时图片路径

主题变量

名称类型默认值说明
SignatureBackgroundColorstringwhite签名背景颜色
SignatureLineColorstringblack签名线条颜色
SignatureLineWidthnumber3签名线条宽度
SignatureRoundbooleantrue是否显示圆角
SignatureBorderbooleantrue是否显示边框
SignatureBorderWidthnumber2边框宽度
SignatureBorderColorstringborder.cell边框颜色
SignatureBorderRadiusnumber12边框圆角大小