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 = arr0.match(/:(.*?);/)1
const suffix = mime.split('/')1
const bstr = atob(arr1)
let n = bstr.length
const u8arr = new Uint8Array(n)

while (n--) {

u8arrn = bstr.charCodeAt(n)

}

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

type: mime

})

},

}

相关推荐
mayaairi1 小时前
Vue2 组件通讯(三):全局事件总线、PubSub、插槽与组件实例属性
前端·javascript·vue.js
kyriewen2 小时前
面试官问我:AI 都能写代码了,前端凭什么还值 25K
前端·javascript·人工智能
风骏时光牛马2 小时前
AI源码分析:拆解模型底层实现逻辑
前端
BillKu3 小时前
全局样式变量:CSS 自定义属性(--border-color)和SCSS 变量($border-color)的说明
javascript·css·scss
IT_陈寒3 小时前
React子组件莫名其妙重渲染?你可能漏了这个Hook
前端·人工智能·后端
乘风gg3 小时前
DeepSeek V4.1 Flash 来了,明天中午 Flash 降价 60%
前端·ai编程·claude
默_笙3 小时前
❗ 给金鱼装记忆(上):从白板到笔记本,AI 的短期记忆生存指南
前端·javascript
酷酷的逗逗乐3 小时前
前端转 Agent 开发 · 第五节:Memory 会话记忆
前端·程序员
掘金挖土4 小时前
前端手摸手跑路之 AI 应用开发(二)
前端·后端
兜里只有三分钱~4 小时前
翻卡_3D 的仪式感,和三个让它穿帮的写法
javascript·css·3d