使用数字和字母生成随机的四位数验证码,代码注释很清楚。
<template>
<div class="img-canvas">
<canvas
id="canvas"
:width="width"
:height="height"
@click="handleDraw"
></canvas>
</div>
</template>
<script>
export default {
data() {
return {
allTexts: "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", // 字符串
width: 120,
height: 40,
imgCode: "",
};
},
mounted() {
// 初始化绘制图片验证码
this.imgCode = this.draw();
},
methods: {
//todo 点击图片刷新
handleDraw() {
this.imgCode = this.draw();
},
//todo 产生随机数
randomNum(min, max) {
return parseInt(Math.random() * (max - min) + min);
},
//todo 产生随机颜色
randomColor(min, max) {
const r