微信小程序canvas2d生成图形验证码的方法

这篇文章主要为大家详细介绍了微信小程序canvas2d生成图形验证码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了微信小程序canvas2d生成图形验证码的具体代码,供大家参考,具体内容如下

成品展示:

背景:

大致看了一下网上已经有一些canvas生成图形验证码的demo,发现使用的也只是旧版canvas,相比于新版canvas2d性能上略有差距,更重要的是旧版canvas在开发者工具上报警告,非得整成canvas2d的不成。成果记录在此。

wxml:

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 

至此就成功了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持0133技术站。

以上就是微信小程序canvas2d生成图形验证码的方法的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » JavaScript 教程