使用vue實(shí)現(xiàn)手寫(xiě)簽名功能
個(gè)人實(shí)現(xiàn)截圖:

安裝:
npm install vue-esign --save
使用:
1.在main.js中引入
import vueEsign from 'vue-esign' Vue.use(vueEsign)
2.在頁(yè)面中引用
<vue-esign ref="esign" :width="800" :height="300" :isCrop="isCrop" :lineWidth="lineWidth" :lineColor="lineColor" :bgColor.sync="bgColor" /> <button @click="handleReset">清空畫(huà)板</button> <button @click="handleGenerate">生成圖片</button>
3.說(shuō)明
| 屬性 | 類(lèi)型 | 默認(rèn)值 | 說(shuō)明 |
|---|---|---|---|
| width | Number | 800 | 畫(huà)布寬度,即導(dǎo)出圖片的寬度 |
| height | Number | 300 | 畫(huà)布高度,即導(dǎo)出圖片的高度 |
| lineWidth | 4 | Number | 畫(huà)筆粗細(xì) |
| lineColor | String | #000000 | 畫(huà)筆顏色 |
| bgColor | String | 空 | 畫(huà)布背景色,為空時(shí)畫(huà)布背景透明, 支持多種格式 '#ccc','#E5A1A1','rgb(229, 161, 161)','rgba(0,0,0,.6)','red' |
| isCrop | Boolean | false | 是否裁剪,在畫(huà)布設(shè)定尺寸基礎(chǔ)上裁掉四周空白部分 |
期待已久,上原碼:
data () {
return {
lineWidth: 6,
lineColor: '#000000',
bgColor: '',
resultImg: '',
isCrop: false
}
},
methods: {
handleReset () {
this.$refs['esign'].reset() //清空畫(huà)布
},
handleGenerate () {
this.$refs['esign'].generate().then(res => {
this.resultImg = res // 得到了簽字生成的base64圖片
}).catch(err => { // 沒(méi)有簽名,點(diǎn)擊生成圖片時(shí)調(diào)用
this.$message({
message: err + ' 未簽名!',
type: 'warning'
})
alert(err) // 畫(huà)布沒(méi)有簽字時(shí)會(huì)執(zhí)行這里 'Not Signned'
})
}
}
附:將base64轉(zhuǎn)化成圖片方法:
// 將base64,轉(zhuǎn)換成圖片
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
})
},
到此這篇關(guān)于使用vue實(shí)現(xiàn)手寫(xiě)簽名功能的文章就介紹到這了,更多相關(guān)vue實(shí)現(xiàn)手寫(xiě)簽名內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue使用$store.commit() undefined報(bào)錯(cuò)的解決
vuex新手進(jìn)階篇之改變state?mutations的使用
Vue3進(jìn)行樣式Scoped和Global的設(shè)置方法
vue獲取當(dāng)前點(diǎn)擊的元素并傳值的實(shí)例
vue.js樣式布局Flutter業(yè)務(wù)開(kāi)發(fā)常用技巧

