前端验证码之-数字运算验证码

效果:

 

完整代码记录:随机生成两个数组,进行加减乘操作

<template>
    <div>
        <div class="flex-center">
            <input type="text" v-model="value" placeholder="请输入验证码" class="input-view" id="text">
            <canvas id="canvas" width="150" height="43" @click="dj" style="border: 1px solid #ccc"></canvas>
        </div>
        <div class="flex-center margin-t20">
            <button @click="dj">换一个</button>
            <button @click="sublim">提交</button>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            show_num: [],
            value: ''
        }
    },
    mounted() {
        this.show_num=[];
        this.draw();
    },
    methods: {
        sublim() {
            let [num1=0, type, num2=0] = this.show_num||[];
            let NUM_MAP = {
                '+': num1+num2,
                '-': num1-num2,
                '*': num1*num2
            }
            if(!this.value) return alert('请输入验证码!');
            if(this.value == NUM_MAP[type]){
                alert('提交成功!');
                this.dj()
            }else{
                alert('验证码错误!\n你输入的是:  '+this.value+"\n正确的是:  "+NUM_MAP[type]+'\n请重新输入!');
                this.dj()
            }
        },
        // 随机颜色
        randomColor() {
            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 + ")";
        },
        // 绘制干扰(线条,原点)
        drawLine(context, canvas_width, canvas_height) {
            // for (var i = 0; i <= 5; i++) { //验证码上显示线条
            //     context.strokeStyle = this.randomColor();
            //     context.beginPath();
            //     context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
            //     context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
            //     context.stroke();
            // }
            for (var i = 0; i <= 30; i++) { //验证码上显示小点
                context.strokeStyle = this.randomColor();
                context.beginPath();
                var x = Math.random() * canvas_width;
                var y = Math.random() * canvas_height;
                context.moveTo(x, y);
                context.lineTo(x + 1, y + 1);
                context.stroke();
            }
        },
        // 绘制单个字符
        drawStr(context, txt, x) {
            var y = 20 + Math.random() * 8;//文字在canvas上的y坐标(20+0到8的随机数)
            context.font = "bold 23px 微软雅黑";

            context.fillStyle = this.randomColor(); // 随机生成一个颜色
            context.fillText(txt, x, y);
            return x + 10 + context.measureText(txt).width; //宽度
        },
        drawNum(context, randomList, currentWidth) {
            var j = Math.floor(Math.random() * randomList.length);//获取到随机的索引值 Math.floor(Math.random()*(max-min+1)+min);
            var txt = randomList[j];//得到随机的一个内容
            this.show_num.push(txt);
            return this.drawStr(context, txt, currentWidth);
        },
        draw(codeLength=3) { // codeLength: 设置验证码长度
            let canvas = document.getElementById("canvas");//获取到canvas的对象,演员
            let context = canvas.getContext("2d");//获取到canvas画图的环境,演员表演的舞台
            let canvas_width=canvas.width;
            let canvas_height=canvas.height;
            context.clearRect(0,0, canvas_width, canvas_height);

            this.show_num=[];
            let currentWidth=0;
            // 数字一
            currentWidth = this.drawNum(context, new Array(100).fill(null).map((v, i) => i), currentWidth);
            // 运算类型
            currentWidth = this.drawNum(context, ['+', '-', '*'], currentWidth);
            // 数字二
            currentWidth = this.drawNum(context, new Array(10).fill(null).map((v, i) => i), currentWidth);
            // 绘制= ?
            this.drawStr(context, '= ?', currentWidth)
            // 一些干扰因素(线条,原点)
            this.drawLine(context, canvas_width, canvas_height); 
        },
        
        dj() {
            this.value=''
            this.draw();   
        },
    }
}
</script>
<style scoped>
.input-view {
    height: 43px;
    width: 220px;
    font-size: 20px;
    border: 1px solid #999;
    font-size: 16px;
    padding: 0 10px;
}
.flex-center {
    display: flex;
    align-items: center;
}
.margin-t20 {
    margin-top: 15px;
}
button {
    background: transparent;
    border: 1px solid #0440b6;
    color: #0440b6;
    margin-right: 10px;
}
</style>
    

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值