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

})

},

}

相关推荐
AI_paid_community2 分钟前
25k Star 登顶 GitHub:这个专门吃 K 线图长大的 AI,让我意识到之前三年都在裸奔
javascript·claude
golang学习记7 分钟前
Cursor官方团队的AI指南:Cursor Team Kit
前端·cursor
Lee川12 分钟前
RAG 知识库问答:从概念到代码的完整实现
前端·人工智能·后端
计算机安禾1 小时前
【c++面向对象编程】第22篇:输入输出运算符重载:<< 与 >> 的友元实现
java·前端·c++
redreamSo1 小时前
14 小时烧光 200 美金:Codex 和 Claude 的 /goal 命令打开了"放手跑"模式
前端
TingTing1 小时前
Webpack5 前端工程化建设
前端
i220818 Faiz Ul1 小时前
宠物猫之猫咖管理系统|基于java + vue宠物猫之猫咖管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·宠物猫之猫咖管理系统
gjwjuejin1 小时前
前端埋点第二弹:那些年我们踩过的坑,和填坑的正确姿势
javascript
A不落雨滴AI1 小时前
DKERP客户端重构纪实:4天自研控件库的“短命”教训,以及为什么我坚定选择原生Qt
前端
我叫黑大帅1 小时前
通过白名单解决 pnpm i 报错 Ignored build scripts
前端·javascript·面试