Make writing a habit together! This is my first day to participate in the “Gold Digging Day New Plan · April More text challenge”, click to see the details of the activity.

background

In the daily development can always encounter login verification problems, today to provide a small partner out of the box picture verification code component

Go straight to code

The code is as follows:

$refs['vercode'].show_num. Join (''); $refs['vercode']. this.$refs.vercode.Refresh(); * /
<template >
  <canvas class="canvas" ref="cav" @click="Refresh"></canvas>
</template>
<script>
export default {
  name: "ver_code",
  data () {
    return {
      show_num: []}; }, mounted () {this.draw();
  },
  methods: {
    draw () {
      var Canvas_DOM = this.$refs.cav;
      var cav_width = Canvas_DOM.clientWidth;
      var cav_height = Canvas_DOM.clientHeight;
      var context = Canvas_DOM.getContext("2d");
      Canvas_DOM.width = cav_width;
      Canvas_DOM.height = cav_height;
      var sCode =
        / / "A, B, C, E, F, G, H, J, K, L, M, N, P, Q, R, S, T, W, X, Y, Z, 1,2,3,4,5,6,7,8,9,0";
        "A, b, c, e, f, g, h, j, k, l, m, n, p, q, r, s, t, w, x, y, z, 1,2,3,4,5,6,7,8,9,0";
      var aCode = sCode.split(",");
      var aLength = aCode.length; // Get the length of the array

      for (var i = 0; i <= 3; i++) {
        var j = Math.floor(Math.random() * aLength); // Get the random index value
        var deg = (Math.random() * 30 * Math.PI) / 180; // Generate random radians between 0 and 30
        var txt = aCode[j]; // Get a random item
        this.show_num[i] = txt.toLowerCase();
        var x = 10 + i * 20; // The x coordinate of the text on the canvas
        var y = 20 + Math.random() * 8; // The y coordinate of the text on the canvas
        context.font = "Bold 23px Microsoft Yahei";

        context.translate(x, y);
        context.rotate(deg);

        context.fillStyle = this._randomColor();
        context.fillText(txt, 0.0);

        context.rotate(-deg);
        context.translate(-x, -y);
      }
      for (var i = 0; i <= 5; i++) {
        // Random lines are displayed on the verification code
        context.strokeStyle = this._randomColor();
        context.beginPath();
        context.moveTo(
          Math.random() * Canvas_DOM.width,
          Math.random() * Canvas_DOM.height
        );
        context.lineTo(
          Math.random() * Canvas_DOM.width,
          Math.random() * Canvas_DOM.height
        );
        context.stroke();
      }
      for (var i = 0; i <= 30; i++) {
        // Random dots are displayed on the verification code
        context.strokeStyle = this._randomColor();
        context.beginPath();
        var x = Math.random() * Canvas_DOM.width;
        var y = Math.random() * Canvas_DOM.height;
        context.moveTo(x, y);
        context.lineTo(x + 1, y + 1);
        context.stroke();
      }
    },
    _randomColor () {
      // Get a random color value
      var r = Math.floor(Math.random() * 256);
      var g = Math.floor(Math.random() * 256);
      var b = Math.floor(Math.random() * 256);
      return "rgb(" + r + "," + g + "," + b + ")";
    },
    Refresh () {
      this.draw();
      this.$emit("refresh_yzm".this.show_num); }}};</script>
<style lang="less" scoped>
.canvas {
  width: 100px;
  height: 40px;
  display: inline-block;
  margin-left: 12px;
  cursor: pointer;
}
</style>
Copy the code

Thank you

Thank you for reading this article. I hope it will be helpful to you. If you have any questions, please point out.

I’m Nicnic, please give me a thumbs up if you think it’s ok ❤

Writing is not easy, “like” + “watching” + “forward” Thanks for your support ❤

Past oliver

1. Publish and subscribe model

Front-end JS high frequency interview questions –2. Singleton mode

Front-end JS high frequency interview questions –3. Agent mode

Front-end JS high frequency interview questions –4. Strategic mode

1.CSS selectors, Priorities, and Inheritance Properties

2. The difference between EM/PX/REM/VH/VW