微信小程序canvas2d生成圖形驗(yàn)證碼的方法
本文實(shí)例為大家分享了微信小程序canvas2d生成圖形驗(yàn)證碼的具體代碼,供大家參考,具體內(nèi)容如下
成品展示:
背景:
大致看了一下網(wǎng)上已經(jīng)有一些canvas生成圖形驗(yàn)證碼的demo,發(fā)現(xiàn)使用的也只是舊版canvas,相比于新版canvas2d性能上略有差距,更重要的是舊版canvas在開發(fā)者工具上報(bào)警告,非得整成canvas2d的不成。成果記錄在此。
wxml:
<input class="input font_14" placeholder-class="grey" type="text"? ? ? placeholder="輸入圖形驗(yàn)證碼" maxlength="4" model:value="{{guiCode}}" ? ? bindinput="bindinput" /> <canvas class="guiCode" type="2d" id="guiCodeCanvas" bindtap="guiCodeTap" />
wxss:
.input { ? ? width: calc(165vmin / 3.75); ? ? height: 100%; ? ? padding: 0 calc(12vmin / 3.75); ? ? box-sizing: border-box; } ? .guiCode { ? ? width: calc(70vmin / 3.75); ? ? height: 100%; }
js:
var guiCode = require('../../utils/guiCode.js'); const app = getApp() ? Page({ ? ? ? data: { ? ? ? ? guiCode: '' ? ? }, ? ? ? onLoad: function (options) { ? ? ? ? this.guiCode = new guiCode({ ? ? ? ? ? ? el: '#guiCodeCanvas', ? ? ? ? ? ? width: 70, ? ? ? ? ? ? height: 50, ? ? ? ? ? ? createCodeImg: "" ? ? ? ? }) ? ? }, ? ? ? bindinput() {}, ? ? ? /** ? ? ?* 刷新圖形驗(yàn)證碼 ? ? ?*/ ? ? guiCodeTap() { ? ? ? ? this.guiCode.refresh() ? ? }, ? ? ? /** ? ? ?* 驗(yàn)證圖形驗(yàn)證碼 ? ? ?*/ ? ? validateGuiCode() { ? ? ? ? if (!this.data.guiCode) { ? ? ? ? ? ? wx.showToast({ ? ? ? ? ? ? ? ? title: '請(qǐng)輸入圖形驗(yàn)證碼', ? ? ? ? ? ? ? ? icon: 'none' ? ? ? ? ? ? }) ? ? ? ? ? ? return ? ? ? ? } ? ? ? ? let res = this.guiCode.validate(this.data.guiCode) ? ? ? ? if (!res) { ? ? ? ? ? ? wx.showToast({ ? ? ? ? ? ? ? ? title: '圖形驗(yàn)證碼錯(cuò)誤', ? ? ? ? ? ? ? ? icon: 'none' ? ? ? ? ? ? }) ? ? ? ? } ? ? }, ? ? ? /** ? ? ?* 立即驗(yàn)證按鈕監(jiān)聽 ? ? ?*/ ? ? toValidate() { ? ? ? ? //驗(yàn)證圖形驗(yàn)證碼 ? ? ? ? this.validateGuiCode() ? ? }, ? })
guiCode工具類:
module.exports = class guiCode { ? ? constructor(options) { ? ? ? ? this.options = options ? ? ? ? this.init() ? ? } ? ? init() { ? ? ? ? // 注意:如果在組件中使用,請(qǐng)將這里的 wx 改為 this ? ? ? ? wx.createSelectorQuery().select(this.options.el) ? ? ? ? ? ? ? ? .fields({ ? ? ? ? ? ? ? ? ? ? node: true, ? ? ? ? ? ? ? ? ? ? size: true ? ? ? ? ? ? ? ? }).exec((res) => { ? ? ? ? ? ? ? ? ? ? this.canvas = res[0].node ? ? ? ? ? ? ? ? ? ? this.ctx = this.canvas.getContext('2d') ? ? ? ? ? ? ? ? ? ? this.canvas.width = this.options.width ? ? ? ? ? ? ? ? ? ? this.canvas.height = this.options.height ? ? ? ? ? ? ? ? ? ? this.fontSize = this.options.height * 3 / 6 ? ? ? ? ? ? ? ? ? ? this.ctx.textBaseline = "middle" ? ? ? ? ? ? ? ? ? ? this.ctx.fillStyle = this.randomColor(180, 240) ? ? ? ? ? ? ? ? ? ? this.refresh() ? ? ? ? ? ? ? ? }) ? ? } ? ? refresh() { ? ? ? ? //清除上一次繪制 ? ? ? ? this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height) ? ? ? ? var code = '' ? ? ? ? var txtArr = ['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', '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', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] ? ? ? ? for (var i = 0; i < 4; i++) { ? ? ? ? ? ? code += txtArr[this.randomNum(0, txtArr.length)] ? ? ? ? } ? ? ? ? this.options.createCodeImg = code ? ? ? ? let arr = (code + '').split('') ? ? ? ? if (arr.length === 0) { ? ? ? ? ? ? arr = ['e', 'r', 'r', 'o', 'r'] ? ? ? ? } ? ? ? ? let offsetLeft = this.options.width * 0.6 / (arr.length - 1) ? ? ? ? let marginLeft = this.options.width * 0.2 ? ? ? ? arr.forEach((item, index) => { ? ? ? ? ? ? this.ctx.fillStyle = this.randomColor(0, 180) ? ? ? ? ? ? let size = this.randomNum(18, this.fontSize) ? ? ? ? ? ? this.ctx.font = size + "px Arial" ? ? ? ? ? ? let dis = offsetLeft * index + marginLeft - size * 0.3 ? ? ? ? ? ? let deg = this.randomNum(-30, 30) ? ? ? ? ? ? this.ctx.translate(dis, this.options.height * 0.5) ? ? ? ? ? ? this.ctx.rotate(deg * Math.PI / 180) ? ? ? ? ? ? this.ctx.fillText(item, 0, 0) ? ? ? ? ? ? this.ctx.rotate(-deg * Math.PI / 180) ? ? ? ? ? ? this.ctx.translate(-dis, -this.options.height * 0.5) ? ? ? ? }) ? ? ? ? for (var i = 0; i < 4; i++) { ? ? ? ? ? ? this.ctx.strokeStyle = this.randomColor(40, 180) ? ? ? ? ? ? this.ctx.beginPath() ? ? ? ? ? ? this.ctx.moveTo(this.randomNum(0, this.options.width), this.randomNum(0, this.options.height)) ? ? ? ? ? ? this.ctx.lineTo(this.randomNum(0, this.options.width), this.randomNum(0, this.options.height)) ? ? ? ? ? ? this.ctx.stroke() ? ? ? ? } ? ? ? ? for (var i = 0; i < this.options.width / 4; i++) { ? ? ? ? ? ? this.ctx.fillStyle = this.randomColor(0, 255) ? ? ? ? ? ? this.ctx.beginPath() ? ? ? ? ? ? this.ctx.arc(this.randomNum(0, this.options.width), this.randomNum(0, this.options.height), 1, 0, 2 * Math.PI) ? ? ? ? ? ? this.ctx.fill() ? ? ? ? } ? ? } ? ? validate(code) { ? ? ? ? var code = code.toLowerCase() ? ? ? ? var v_code = this.options.createCodeImg.toLowerCase() ? ? ? ? if (code == v_code.substring(v_code.length - 4)) { ? ? ? ? ? ? return true ? ? ? ? } else { ? ? ? ? ? ? return false ? ? ? ? } ? ? } ? ? randomNum(min, max) { ? ? ? ? return Math.floor(Math.random() * (max - min) + min) ? ? } ? ? randomColor(min, max) { ? ? ? ? let r = this.randomNum(min, max) ? ? ? ? let g = this.randomNum(min, max) ? ? ? ? let b = this.randomNum(min, max) ? ? ? ? return "rgb(" + r + "," + g + "," + b + ")" ? ? } }
至此就成功了。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序 功能函數(shù)小結(jié)(手機(jī)號(hào)驗(yàn)證*、密碼驗(yàn)證*、獲取驗(yàn)證碼*)
- 微信小程序6位或多位驗(yàn)證碼密碼輸入框功能的實(shí)現(xiàn)代碼
- 微信小程序發(fā)送短信驗(yàn)證碼完整實(shí)例
- 微信小程序?qū)崿F(xiàn)倒計(jì)時(shí)60s獲取驗(yàn)證碼
- 微信小程序如何獲取手機(jī)驗(yàn)證碼
- 微信小程序綁定手機(jī)號(hào)獲取驗(yàn)證碼功能
- 微信小程序?qū)崿F(xiàn)隨機(jī)驗(yàn)證碼功能
- 微信小程序?qū)崿F(xiàn)驗(yàn)證碼獲取倒計(jì)時(shí)效果
- 微信小程序?qū)崿F(xiàn)發(fā)送驗(yàn)證碼按鈕效果
- 詳解如何使用微信小程序云函數(shù)發(fā)送短信驗(yàn)證碼
相關(guān)文章
一文詳解如何在項(xiàng)目中和平時(shí)練習(xí)中應(yīng)用es6語法
ES6是JavaScript的一個(gè)版本,因?yàn)槲覀兦懊嬗玫降膙ue默認(rèn)使用ES6語法開發(fā),所以我們?cè)谶@一節(jié)補(bǔ)充ES6的知識(shí)點(diǎn),下面這篇文章主要給大家介紹了關(guān)于如何在項(xiàng)目中和平時(shí)練習(xí)中應(yīng)用es6語法的相關(guān)資料,需要的朋友可以參考下2022-11-11購物車前端開發(fā)(jQuery和bootstrap3)
針對(duì)購物車的操作,進(jìn)行產(chǎn)品數(shù)量的增加減少,刪除購物車中產(chǎn)品項(xiàng),本文使用JQuery1.11和bootstrap3進(jìn)行購物車開發(fā),感興趣的小伙伴們2016-08-08javascript Array對(duì)象基礎(chǔ)知識(shí)小結(jié)
感覺自己對(duì)Array對(duì)象總是有種朦朧的感覺,今天自己手寫總結(jié),加深一下印象。2010-11-11webpack學(xué)習(xí)--webpack經(jīng)典7分鐘入門教程
這篇文章主要介紹了webpack學(xué)習(xí)--webpack經(jīng)典7分鐘入門教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06