vue 实现签字功能

1、安装:npm install vue-esign --save

2、main.js文件中全局引入:

// 签字

import vueEsign from 'vue-esign'

Vue.use(vueEsign)

3、页面内容

<vue-esign ref="esign" :width="800" :height="300" :isCrop="isCrop" :lineWidth="lineWidth" :lineColor="lineColor" :bgColor.sync="bgColor" />

<button @click="handleReset">清空画板</button>

<button @click="handleGenerate">生成图片</button>

4、说明:

5、

data () {

return {

lineWidth: 6,

lineColor: '#000000',

bgColor: '',

resultImg: '',

isCrop: false

}

},

methods: {

handleReset () {

this.$refs['esign'].reset() //清空画布

},

handleGenerate () {

this.$refs['esign'].generate().then(res => {

this.resultImg = res // 得到了签字生成的base64图片

let imgFile = this.base64ImgtoFile(res, 'file') //得到文件格式

}).catch(err => { // 没有签名,点击生成图片时调用

this.$message({

message: err + ' 未签名!',

type: 'warning'

})

alert(err) // 画布没有签字时会执行这里 'Not Signned'

})

}

}

6、将base64转化成图片方法:

// 将base64,转换成图片

base64ImgtoFile(dataurl, filename='file') {
const arr = dataurl.split(',')
const mime = arr[0].match(/:(.*?);/)[1]
const suffix = mime.split('/')[1]
const bstr = atob(arr[1])
let n = bstr.length
const u8arr = new Uint8Array(n)

while (n--) {

u8arr[n] = bstr.charCodeAt(n)

}

return new File([u8arr], `{filename}.{suffix}`, {

type: mime

})

},

}

相关推荐
码事漫谈3 小时前
大模型输出的“隐性结构塌缩”问题及对策
前端·后端
这儿有一堆花3 小时前
前端三件套真的落后了吗?揭开现代 Web 开发的底层逻辑
前端·javascript·css·html5
.Cnn4 小时前
JavaScript 前端基础笔记(网页交互核心)
前端·javascript·笔记·交互
醉酒的李白、4 小时前
Vue3 组件通信本质:Props 下发,Emits 回传
前端·javascript·vue.js
anOnion4 小时前
构建无障碍组件之Window Splitter Pattern
前端·html·交互设计
小眼哥5 小时前
SpringBoot整合Vue代码生成exe运行程序以及windows安装包
vue.js·windows·spring boot
NotFound4865 小时前
实战分享Python爬虫,如何实现高效解析 Web of Science 文献数据并导出 CSV
前端·爬虫·python
徐小夕5 小时前
PDF无限制预览!Jit-Viewer V1.5.0开源文档预览神器正式发布
前端·vue.js·github
WangJunXiang65 小时前
Haproxy搭建Web群集
前端
吴声子夜歌5 小时前
Vue.js——自定义指令
前端·vue.js·flutter