UNPKG

vue3-esign

Version:
125 lines (89 loc) 4.31 kB
# vue3-esign 基于 [vue-esign](https://github.com/JaimeCheng/vue-esign) 的 **vue3** 和 **ts** 改造版本。 > Canvas 手写签字 电子签名 ![npm](https://img.shields.io/npm/dm/vue3-esign) ![GitHub package.json version](https://img.shields.io/github/package-json/v/liwenfengg/vue3-esign) ## 安装 ``` bash # 推荐使用 ni 和 taze 来管理项目依赖 # https://github.com/antfu/ni # https://github.com/antfu/taze npm i -g @antfu/ni # https://github.com/liwenfengg/vue3-esign ni vue3-esign -S ``` ## 功能 1. 兼容 PC 和 Mobile; 2. 画布自适应屏幕大小变化(窗口缩放、屏幕旋转时画布无需重置,自动校正坐标); 3. 自定义画布尺寸(导出图尺寸),画笔粗细、颜色,画布背景色; 4. 支持裁剪 (针对需求:有的签字需要裁剪掉四周空白)。 5. 导出图片格式为 `base64`; 6. [demo 示例](https://liwenfengg.github.io/vue3-esign/index.html) ## 使用 1. 全局使用 、局部 ```ts // 全局 vue3 main.ts import { createApp } from 'vue' import { Vue3Esign } from 'vue3-esign' // 局部 vue3 App.vue import { Vue3Esign } from 'vue3-esign' import App from './App.vue' const app = createApp(App) app.use(Vue3Esign) ``` 2. 页面中使用 **必须设置 `ref` ,用来调用组件的两个内置方法 `reset()` 和 `generate()`** 无需给组件设置 `style` 的宽高,如果画布的 `width`属性值没超出父元素的样式宽度,则该组件的样式宽度就是画布宽度,超出的话,组件样式宽度则是父元素的100%; 所以只需设置好父元素的宽度即可; ```html <!-- vue3 setup lang="ts" --> <Vue3Esign ref="vueEsignRef" v-model:bgColor="vueEsignBgColor" :is-clear-bg-color="false" :width="800" :height="400" :is-crop="isCrop" :line-width="lineWidth" :line-color="lineColor" /> <button @click="esignReset">清空画板</button> <button @click="create">生成图片</button> ``` ```ts import { Vue3Esign } from '~/export' const vueEsignRef = ref<any>(null) const vueEsignBgColor = ref<string>('rgba(210,210,210,1)') const lineWidth = ref(6) const lineColor = ref('#000000') const isCrop = ref(false) const img = ref('') const esignReset = async () => { vueEsignRef.value.reset() } const create = async () => { console.log('vueEsignRef', vueEsignRef.value) try { const res = await vueEsignRef.value.generate() console.log('res', res) img.value = res } catch (error) { console.error('error', error) img.value = '' } } ``` 3. 说明 | 属性 | 类型 | 默认值 | 说明 | | :------------- | :-----: | :-----: | :------------------------------------------------------------------ | | width | Number | 800 | 画布宽度,即导出图片的宽度 | | height | Number | 300 | 画布高度,即导出图片的高度 | | lineWidth | 4 | Number | 画笔粗细 | | lineColor | String | #000000 | 画笔颜色 | | bgColor | String | 空 | 画布背景色,为空时画布背景透明,支持多种格式 | | | | | '#ccc''#E5A1A1''rgb(229, 161, 161)''rgba (0,0,0,.6)''red' | | isCrop | Boolean | false | 是否裁剪,在画布设定尺寸基础上裁掉四周空白部分 | | isClearBgColor | Boolean | true | 清空画布时(reset)是否同时清空设置的背景色(bgColor) | 两个内置方法(**defineExpose({ generate, reset })**),通过给组件设置 `ref` 调用: **清空画布 reset** ```ts vueEsignRef.value.reset() ``` **生成图片 generate** ```ts const res = await vueEsignRef.value.generate() console.log('res', res) img.value = res ``` ![demo.gif](./static/demo.gif)