vue隨機驗證碼組件的封裝實現(xiàn)
本文實例為大家分享了vue隨機驗證碼組件的具體代碼,供大家參考,具體內(nèi)容如下
由于工作需要自己做了一個驗證碼組件,靈活性不高,但是可以用,代碼也不太復(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 () { // 生成隨機字 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),效果如下:
更多教程點擊《Vue.js前端組件學(xué)習(xí)教程》,歡迎大家學(xué)習(xí)閱讀。
關(guān)于vue.js組件的教程,請大家點擊專題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue中使用element-ui給按鈕綁定一個單擊事件實現(xiàn)點擊按鈕就彈出dialog對話框
最近遇到了個需求是使用element-ui插件編寫頁面,點擊按鈕,彈出對話框,這篇文章主要給大家介紹了關(guān)于Vue中使用element-ui給按鈕綁定一個單擊事件實現(xiàn)點擊按鈕就彈出dialog對話框的相關(guān)資料,需要的朋友可以參考下2022-11-11vue3.0使用mapState,mapGetters和mapActions的方式
這篇文章主要介紹了vue3.0使用mapState,mapGetters和mapActions的方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06Vue3 Ref獲取真實DOM學(xué)習(xí)實戰(zhàn)
這篇文章主要為大家介紹了Vue3 Ref獲取真實DOM學(xué)習(xí)實戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06