您好,登錄后才能下訂單哦!
本篇內容主要講解“怎么使用微信小程序canvas2d生成圖形驗證碼”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“怎么使用微信小程序canvas2d生成圖形驗證碼”吧!
成品展示:
背景:
大致看了一下網上已經有一些canvas生成圖形驗證碼的demo,發現使用的也只是舊版canvas,相比于新版canvas2d性能上略有差距,更重要的是舊版canvas在開發者工具上報警告,非得整成canvas2d的不成。成果記錄在此。
wxml:
<input class="input font_14" placeholder-class="grey" type="text" placeholder="輸入圖形驗證碼" 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() {}, /** * 刷新圖形驗證碼 */ guiCodeTap() { this.guiCode.refresh() }, /** * 驗證圖形驗證碼 */ validateGuiCode() { if (!this.data.guiCode) { wx.showToast({ title: '請輸入圖形驗證碼', icon: 'none' }) return } let res = this.guiCode.validate(this.data.guiCode) if (!res) { wx.showToast({ title: '圖形驗證碼錯誤', icon: 'none' }) } }, /** * 立即驗證按鈕監聽 */ toValidate() { //驗證圖形驗證碼 this.validateGuiCode() }, })
guiCode工具類:
module.exports = class guiCode { constructor(options) { this.options = options this.init() } init() { // 注意:如果在組件中使用,請將這里的 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 + ")" } }
到此,相信大家對“怎么使用微信小程序canvas2d生成圖形驗證碼”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。