vue隨機(jī)驗(yàn)證碼組件的封裝實(shí)現(xiàn)
本文實(shí)例為大家分享了vue隨機(jī)驗(yàn)證碼組件的具體代碼,供大家參考,具體內(nèi)容如下
由于工作需要自己做了一個(gè)驗(yàn)證碼組件,靈活性不高,但是可以用,代碼也不太復(fù)雜
<template> <div style="display: flex;justify-content: start;align-items: center;border-radius: 4px"> <canvas style="" :width="contentWidth" :height="contentHeight" id="cav" @click="next()"></canvas> </div> </template> <script> // import num from './components/cc' export default { name: 'verificationCode', props: { fontSize: { type: Number, default: 20 }, contentWidth: { type: Number, default: 100 }, contentHeight: { type: Number, default: 40 }, verification: { type: String }, refreshCode: { type: Boolean } }, watch: { verification: function (newVal) { if (newVal.toLowerCase() === this.identify.toLowerCase()) { this.$emit('handleIdentify', true) } else { this.$emit('handleIdentify', false) } }, refreshCode: function (newVal) { this.draw() } }, mounted () { this.draw() }, data () { return { identify: '', letter: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] } }, methods: { draw () { let self = this let text = this.randomNum() this.identify = text.join('') let ctx = document.getElementById('cav') let cav = ctx.getContext('2d') cav.clearRect(0, 0, self.contentWidth, self.contentHeight) let w = ctx.width let h = ctx.height cav.textBaseline = 'bottom' self.drawText(cav, text[0], 10, 10, self.randomDeg(0, 0.1), 20) self.drawText(cav, text[1], 20, 10, self.randomDeg(0, 0.1)) self.drawText(cav, text[2], 25, 10, self.randomDeg(0, 0.1)) self.drawText(cav, text[3], 30, 10, self.randomDeg(0, 0.1)) let i for (i = 0; i < 3; i++) { self.drawLine(cav, self.randomDeg(0, 0.3 * w, 1), self.randomDeg(0, h, 1), self.randomDeg(0, w, 1), self.randomDeg(0, h, 1), self.randomDeg(0.8 * w, w, 1), self.randomDeg(0, h, 1)) } }, drawText (cav, text, x1, y1, route) { cav.beginPath() cav.fillStyle = '#ffe9db' cav.font = this.fontSize + 'px' + ' ' + '黑體' cav.translate(x1, y1) cav.rotate(Math.PI * route) cav.fillText(text, x1, y1) cav.fill() cav.rotate(-Math.PI * route) cav.translate(-(x1), -(y1)) }, drawLine (cav, x1, y1, x2, y2, x3, y3) { cav.beginPath() cav.strokeStyle = 'rgb(155, 204, 237)' cav.bezierCurveTo(x1, y1, x2, y2, x3, y3) cav.stroke() }, randomNum () { // 生成隨機(jī)字 let i let letter = this.letter let text = [] for (i = 0; i < 4; i++) { text.push(letter[Math.floor(Math.random() * 36)]) } return text }, randomDeg (min, max, f) { // 設(shè)置文字傾斜角度 f = undefined ? Math.random() > 0.5 ? 1 : -1 : 1 return f * (Math.random() * (max - min) + min) }, next () { this.draw() } } } </script> <style scoped> </style>
樣式什么的自己可以調(diào)一調(diào),效果如下:
更多教程點(diǎn)擊《Vue.js前端組件學(xué)習(xí)教程》,歡迎大家學(xué)習(xí)閱讀。
關(guān)于vue.js組件的教程,請(qǐng)大家點(diǎn)擊專題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車小案例
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車小案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10Vue中使用element-ui給按鈕綁定一個(gè)單擊事件實(shí)現(xiàn)點(diǎn)擊按鈕就彈出dialog對(duì)話框
最近遇到了個(gè)需求是使用element-ui插件編寫頁(yè)面,點(diǎn)擊按鈕,彈出對(duì)話框,這篇文章主要給大家介紹了關(guān)于Vue中使用element-ui給按鈕綁定一個(gè)單擊事件實(shí)現(xiàn)點(diǎn)擊按鈕就彈出dialog對(duì)話框的相關(guān)資料,需要的朋友可以參考下2022-11-11vue3.0使用mapState,mapGetters和mapActions的方式
這篇文章主要介紹了vue3.0使用mapState,mapGetters和mapActions的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06vue實(shí)現(xiàn)消息的無(wú)縫滾動(dòng)效果的示例代碼
本篇文章主要介紹了vue實(shí)現(xiàn)消息的無(wú)縫滾動(dòng)效果的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12Vue3 Ref獲取真實(shí)DOM學(xué)習(xí)實(shí)戰(zhàn)
這篇文章主要為大家介紹了Vue3 Ref獲取真實(shí)DOM學(xué)習(xí)實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06