zoukankan      html  css  js  c++  java
  • 小程序图形验证码前端实现

    图形验证码小程序版实现

    ../../utils/mcaptcha.js
    module.exports = class Mcaptcha {
      constructor(options) {
        this.options = options;
        this.fontSize = options.height * 3 / 4;
        this.init();
        this.refresh(this.options.code);
      }
      init() {
        this.ctx = wx.createCanvasContext(this.options.el);
        this.ctx.setTextBaseline("middle");
        this.ctx.setFillStyle(this.randomColor(180, 240));
        this.ctx.fillRect(0, 0, this.options.width, this.options.height);
      }
      refresh(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.setFillStyle(this.randomColor(0, 180));
          let size = this.randomNum(24, this.fontSize);
          this.ctx.setFontSize(size);
          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);
        })
        this.ctx.draw();
      }
      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 + ")";
      }
    
    }

    页面内引入

    js

    let Mcaptcha = require('../../utils/mcaptcha.js'); ............ onReady: function () { new Mcaptcha({ el: 'canvas', 120, height: 40, code: "2444" }); } ..........
    wxml

    <canvas style="{{cvs.width}};height:{{cvs.height}};" canvas-id="canvas"></canvas>

     git地址>>> 

    https://github.com/CaraXiaoKe/wechat-captcha

    参考:https://www.jianshu.com/p/064a80a3561a

  • 相关阅读:
    拒绝采样
    概率函数P(x)、概率分布函数F(x)、概率密度函数f(x)
    Dynamic Filter Networks
    ECC ~ Edge-Conditioned Filter in CNN on Graphs
    Graph-GCN
    Graph-to-ID task
    non-local denoising methods
    Graph-GraphSage
    CNN作为denoiser的优势总结
    论文解读《Deep Plug-and-Play Super-Resolution for Arbitrary Blur Kernel》
  • 原文地址:https://www.cnblogs.com/dupd/p/8204191.html
Copyright © 2011-2022 走看看