美文网首页
关于vue字母验证码

关于vue字母验证码

作者: Morbid_D | 来源:发表于2022-06-12 09:59 被阅读0次

直接可以使用

<template>

    <div class="s-canvas">

        <canvas id="s-canvas" :width="contentWidth" :height="contentHeight"></canvas>

    </div>

</template>

<script>

export default {

  name: "Sidentify",

  props: {

    identifyCode: {

      type: String,

      default: "1234"

    },

    fontSizeMin: {

      type: Number,

      default: 25

    },

    fontSizeMax: {

      type: Number,

      default: 30

    },

    backgroundColorMin: {

      type: Number,

      default: 255

    },

    backgroundColorMax: {

      type: Number,

      default: 255

    },

    colorMin: {

      type: Number,

      default: 0

    },

    colorMax: {

      type: Number,

      default: 160

    },

    lineColorMin: {

      type: Number,

      default: 100

    },

    lineColorMax: {

      type: Number,

      default: 255

    },

    dotColorMin: {

      type: Number,

      default: 0

    },

    dotColorMax: {

      type: Number,

      default: 255

    },

    contentWidth: {

      type: Number,

      default: 112

    },

    contentHeight: {

      type: Number,

      default: 31

    }

  },

  methods: {

    // 生成一个随机数

    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 + ")";

    },

    drawPic() {

      let canvas = document.getElementById("s-canvas");

      let ctx = canvas.getContext("2d");

      ctx.textBaseline = "bottom";

      // 绘制背景

      ctx.fillStyle = this.randomColor(

        this.backgroundColorMin,

        this.backgroundColorMax

      );

      ctx.fillRect(0, 0, this.contentWidth, this.contentHeight);

      // 绘制文字

      for (let i = 0; i < this.identifyCode.length; i++) {

        this.drawText(ctx, this.identifyCode[i], i);

      }

      this.drawLine(ctx);

      this.drawDot(ctx);

    },

    drawText(ctx, txt, i) {

      ctx.fillStyle = this.randomColor(this.colorMin, this.colorMax);

      ctx.font =

        this.randomNum(this.fontSizeMin, this.fontSizeMax) + "px SimHei";

      let x = (i + 1) * (this.contentWidth / (this.identifyCode.length + 1));

      let y = this.randomNum(this.fontSizeMax, this.contentHeight - 5);

      var deg = this.randomNum(-45, 45);

      // 修改坐标原点和旋转角度

      ctx.translate(x, y);

      ctx.rotate(deg * Math.PI / 180);

      ctx.fillText(txt, 0, 0);

      // 恢复坐标原点和旋转角度

      ctx.rotate(-deg * Math.PI / 180);

      ctx.translate(-x, -y);

    },

    drawLine(ctx) {

      // 绘制干扰线

      for (let i = 0; i < 5; i++) {

        ctx.strokeStyle = this.randomColor(

          this.lineColorMin,

          this.lineColorMax

        );

        ctx.beginPath();

        ctx.moveTo(

          this.randomNum(0, this.contentWidth),

          this.randomNum(0, this.contentHeight)

        );

        ctx.lineTo(

          this.randomNum(0, this.contentWidth),

          this.randomNum(0, this.contentHeight)

        );

        ctx.stroke();

      }

    },

    drawDot(ctx) {

      // 绘制干扰点

      for (let i = 0; i < 80; i++) {

        ctx.fillStyle = this.randomColor(0, 255);

        ctx.beginPath();

        ctx.arc(

          this.randomNum(0, this.contentWidth),

          this.randomNum(0, this.contentHeight),

          1,

          0,

          2 * Math.PI

        );

        ctx.fill();

      }

    }

  },

  watch: {

    identifyCode() {

      this.drawPic();

    }

  },

  mounted() {

    this.drawPic();

  }

};

</script>

<style scoped lang="scss">

  .s-canvas {

    height: 38px;

  }

  .s-canvas canvas {

    margin-top: 1px;

    margin-left: 8px;

  }

</style>

//使用方法

import codeInfo from "../components/code";

created(){

this.refreshCode();

},

methods:{

    randomNum(min, max) {

      return Math.floor(Math.random() * (max - min) + min);

    },

    refreshCode() {//

      this.identifyCode = "";

      this.makeCode(this.identifyCodes, 4);

    },

    makeCode(o, l) {

        for (let i = 0; i < l; i++) {

          this.identifyCode += this.identifyCodes[

          this.randomNum(0, this.identifyCodes.length)

        ];

      }

    },

  }

相关文章

  • Python生成字母验证码图片

    使用Python生成字母验证码图片:

  • antd-vue 登录 带验证码

    登录页面login.vue 验证码组件identify.vue

  • 验证码的类型

    1、普通型验证码四位数字和字母验证码,可能都是字母,也可能都是数字,随机的4位字符串 2、行为式验证码 ——拖动式...

  • PHP之验证码制作

    php实现字母验证码 php通过session存储验证信息 验证码通过表单提交、校验 使用js实现动态校验验证码

  • 验证码、水印、缩放

    随机生成验证码 1、验证码1、纯数字2、纯字母3、数字字母混合4、计算公式 3 + 5 =打乱字符串:str_s...

  • 前端绘制图形验证码

    一、功能分析 图形验证码由验证码、干扰线、干扰点组合而成 验证码由数字和字母随机组合形成 每次切换验证码,验证码字...

  • 6.验证码

    验证码现在无处不在,比如最常见的字母数字验证码,12306的图形验证码,简书的滑动验证码,还有一些论坛的计算验证码...

  • Java实现验证码功能

    字母数字混合验证码 CaptcahCode.java index.jsp code.jsp 算术验证码 使用kca...

  • 生成16位验证码

    需求1: 生成16位随机验证码 需求2: 要求验证码包含大小写字母,数字,特殊字符

  • Python爬虫 | 普通验证码破解 tesserocr

    验证码是众多网站采取的反爬措施。验证码的花样也很多,主要有下面这几种类验证码:图形验证码:数字、英文字母、混淆曲线...

网友评论

      本文标题:关于vue字母验证码

      本文链接:https://www.haomeiwen.com/subject/okugprtx.html