欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序canvas2d生成圖形驗(yàn)證碼的方法

 更新時(shí)間:2022年05月24日 09:43:35   作者:秋山大大  
這篇文章主要為大家詳細(xì)介紹了微信小程序canvas2d生成圖形驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • JS不要再到處使用絕對(duì)等于運(yùn)算符了

    JS不要再到處使用絕對(duì)等于運(yùn)算符了

    這篇文章主要介紹了JS不要再到處使用絕對(duì)等于運(yùn)算符了,對(duì)此感興趣的同學(xué),可以參考下
    2021-04-04
  • 一文詳解如何在項(xiàng)目中和平時(shí)練習(xí)中應(yīng)用es6語法

    一文詳解如何在項(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)

    購物車前端開發(fā)(jQuery和bootstrap3)

    針對(duì)購物車的操作,進(jìn)行產(chǎn)品數(shù)量的增加減少,刪除購物車中產(chǎn)品項(xiàng),本文使用JQuery1.11和bootstrap3進(jìn)行購物車開發(fā),感興趣的小伙伴們
    2016-08-08
  • 關(guān)于前端面試中常提到的LRU緩存策略詳析

    關(guān)于前端面試中常提到的LRU緩存策略詳析

    LRU緩存就是一種常用策略,下面這篇文章主要給大家介紹了關(guān)于前端面試中常提到的LRU緩存策略的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • JS生成一維碼(條形碼)功能示例

    JS生成一維碼(條形碼)功能示例

    這篇文章主要介紹了JS生成一維碼(條形碼)功能,結(jié)合完整實(shí)例形式分析了JS插件生成條形碼的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2017-01-01
  • javascript Array對(duì)象基礎(chǔ)知識(shí)小結(jié)

    javascript Array對(duì)象基礎(chǔ)知識(shí)小結(jié)

    感覺自己對(duì)Array對(duì)象總是有種朦朧的感覺,今天自己手寫總結(jié),加深一下印象。
    2010-11-11
  • JavaScript中獲取HTML元素值的三種方法

    JavaScript中獲取HTML元素值的三種方法

    這篇文章主要為大家詳細(xì)介紹了JavaScript中獲取HTML元素值的三種方法,分享了JavaScript中取得元素的方法,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Bootstrap創(chuàng)建可折疊的組件

    Bootstrap創(chuàng)建可折疊的組件

    這篇文章主要為大家詳細(xì)介紹了Bootstrap創(chuàng)建可折疊組件的對(duì)應(yīng)方法,以實(shí)例為大家分享了Bootstrap折疊組件,感興趣的小伙伴們可以參考一下
    2016-02-02
  • webpack學(xué)習(xí)--webpack經(jīng)典7分鐘入門教程

    webpack學(xué)習(xí)--webpack經(jīng)典7分鐘入門教程

    這篇文章主要介紹了webpack學(xué)習(xí)--webpack經(jīng)典7分鐘入門教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • JavaScript中fetch()用法實(shí)例

    JavaScript中fetch()用法實(shí)例

    在前端快速發(fā)展地過程中,為了契合更好的設(shè)計(jì)模式,產(chǎn)生了 fetch 框架,下面這篇文章主要給大家介紹了關(guān)于JavaScript中fetch()用法的相關(guān)資料,需要的朋友可以參考下
    2022-06-06

最新評(píng)論