Vue realizes random verification code function

Time:2021-4-14

Step 1 create a identify.vue Subcomponents of

export default {
 name: 'SIdentify',
 props: {
 //Default registration code
identifyCode: {
    type: String,
    default: "1234"
},
//Minimum font size
fontSizeMin: {
    type: Number,
    default: 35
},
//Maximum font size
fontSizeMax: {
    type: Number,
    default: 35
},
//Background color minimum
backgroundColorMin: {
    type: Number,
    default: 180
},
//Background color maximum
backgroundColorMax: {
    type: Number,
    default: 240
},
//Minimum font color value
colorMin: {
    type: Number,
    default: 50
},
//Maximum font color value
colorMax: {
    type: Number,
    default: 160
},
//Minimum value of interference line color value
lineColorMin: {
    type: Number,
    default: 100
},
//Maximum interference line color value
lineColorMax: {
    type: Number,
    default: 200
},
//Minimum value of interference point color value
dotColorMin: {
    type: Number,
    default: 0
},
//Maximum color value of interference point
dotColorMax: {
    type: Number,
    default: 255
},
//Canvas width
contentWidth: {
    type: Number,
    default: 120
},
//Canvas height
contentHeight: {
    type: Number,
    default: 40
}
 },
 methods: {
 //Generate a random number
 randomNum(min, max) {
  return Math.floor(Math.random() * (max - min) + min)
 },
 //Generate a random color
 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'
  //Draw background
  ctx.fillStyle = this.randomColor(this.backgroundColorMin, this.backgroundColorMax)
  ctx.fillRect(0, 0, this.contentWidth, this.contentHeight)
  //Draw text
  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) {
 //Random production font color
  ctx.fillStyle = this.randomColor(this.colorMin, this.colorMax)
  //Randomly generate font size
  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)
  //Modify coordinate origin and rotation angle
  ctx.translate(x, y)
  ctx.rotate(deg * Math.PI / 180)
  ctx.fillText(txt, 0, 0)
  //Restore coordinate origin and rotation angle
  ctx.rotate(-deg * Math.PI / 180)
  ctx.translate(-x, -y)
 },
 drawLine(ctx) {
  //Draw interference line
  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) {
  //Draw interference points
  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()
 }
}

Step 2: register and reference in subcomponents

import SIdentify from "./common/sIdentify.vue";
    export default {
        components: { SIdentify },
    }

Step 3 – use subcomponents in the main page
1. In template:

2. In data:

 data() {
	 return {
	     identifyCode: "",
	     identifyCodes: "",
	 }
},

3. In methods:

methods: {
    //Generating random numbers
    randomNum(min, max) {
        max = max + 1
        return Math.floor(Math.random() * (max - min) + min);
    },
    //Update captcha
    refreshCode() {
        this.identifyCode = "";
        this.makeCode(this.identifyCodes, 4);
        console.log ('current captcha = = ', this.identifyCode );
    },
    //Randomly generated verification code string
    makeCode(data, len) {
        for (let i = 0; i < len; i++) {
            this.identifyCode += this.identifyCodes[this.randomNum(0, this.identifyCodes)]
        }
    },
}

Reproduced in: https://blog.csdn.net/Serena_ tz/article/details/111866600