zmc
2023-12-01 f4062e41dfbe26ca7664a963357cc0b9bea37b44
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<template>
    <canvas ref="canvas" @click="draw" width="140" height="40" style="cursor: pointer;"></canvas>
  </template>
  <script>
  export default {
    data() {
      return {
        codes: [],
        ctx: "",
        colors: ["red", "yellow", "blue", "green", "pink", "black"],
        code_Len: 4
      };
    },
    mounted() {
      this.draw();
    },
    computed: {
      codeString() {
        let result = "";
        for (let i = 0; i < this.codes.length; i++) {
          result += this.codes[i];
        }
        return result.toUpperCase();
      }
    },
    watch: {
      codeString: function(newValue) {
        this.$emit("change", newValue);
      }
    },
    methods: {
      generateRandom() {
        this.codes = [];
        const chars = "abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";
        const charsArr = chars.split("");
   
        for (let i = 0; i < this.code_Len; i++) {
          const num = Math.floor(Math.random() * charsArr.length);
          this.codes.push(charsArr[num]);
        }
      },
      draw() {
        this.generateRandom();
        this.drawText();
      },
      drawLine() {
        const lineNumber = 3; // 线条条数
        const lineX = 140;
        const lineY = 30; // 最大线条坐标
        for (let i = 0; i < lineNumber; i++) {
          this.ctx.strokeStyle = this.colors[Math.floor(Math.random() * 5)];
          this.ctx.beginPath();
          this.ctx.moveTo(
            Math.floor(Math.random() * lineX),
            Math.floor(Math.random() * lineY)
          );
          this.ctx.lineTo(
            Math.floor(Math.random() * lineX),
            Math.floor(Math.random() * lineY)
          );
          this.ctx.stroke();
        }
      },
      drawText() {
        const canvas = this.$refs["canvas"];
        this.ctx = canvas.getContext("2d");
   
        this.ctx.fillStyle = "#BFEFFF";
        this.ctx.fillRect(0, 0, 140, 40);
        this.ctx.font = "20px Verdana";
   
        let x = 15;
   
        for (let i = 0; i < this.code_Len; i++) {
          this.ctx.fillStyle = this.colors[Math.floor(Math.random() * 5)];
          this.ctx.fillText(this.codes[i], x, 25);
          x = x + 30;
        }
   
        this.drawLine();
      }
    }
  };
  </script>