您现在的位置是:首页 > 网站制作 > CSS3|Html5CSS3|Html5

前端生成图形验证码

刘锁2019-04-02【CSS3|Html5】人已围观

简介这个是看的别人写的,因为代码整齐、结构清晰、注释也详细,看着非常舒服,拿过来先收藏了,感谢原创!

首先上运行效果图:

下面是代码:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no">
    <title>生成图片验证码</title>
    <script src="https://cdn.bootcss.com/jquery/1.12.3/jquery.min.js"></script>
    <style>
        #verify {
            height: 34px;
            vertical-align: top;
            font-size: 16px;
        }
        #code_img {
            width: 100px;
            height: 40px;
            cursor: pointer;
            vertical-align: top;
        }
    </style>
</head>
 
<body>
    点击图片重新生成验证码
    <p>
        <input type="text" class="topAlign" id="verify" name="verify" required>
        <canvas width="100" height="40" id="verifyCanvas"></canvas>
        <img id="code_img">
    </p>
    <button id="submit" type="submit">提交</button>
</body>
<script>
    var nums = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
        'L', 'M', 'N', 'O', 'P', 'Q', 'R',
        'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
        'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
        'y', 'z'
    ];
    var colors = []
    drawCode();
    // 绘制验证码
    function drawCode() {
        var canvas = document.getElementById("verifyCanvas"); //获取HTML端画布
        var context = canvas.getContext("2d"); //获取画布2D上下文
        context.fillStyle = "cornflowerblue"; //画布填充色
        context.fillRect(0, 0, canvas.width, canvas.height);
        // 创建渐变
        var gradient = context.createLinearGradient(0, 0, canvas.width, 0);
        gradient.addColorStop("0", "magenta");
        gradient.addColorStop("0.5", "blue");
        gradient.addColorStop("1.0", "red");
        //清空画布
        context.fillStyle = gradient; //设置字体颜色
        context.font = "25px Arial"; //设置字体
        var rand = new Array();
        var x = new Array();
        var y = new Array();
        for (var i = 0; i < 4; i++) {
            rand[i] = nums[Math.floor(Math.random() * nums.length)]
            x[i] = i * 16 + 10;
            y[i] = Math.random() * 20 + 20;
            context.fillText(rand[i], x[i], y[i]);
        }
        // console.log(rand);
        //画3条随机线
        for (var i = 0; i < 3; i++) {
            drawline(canvas, context);
        }
 
        // 画30个随机点
        for (var i = 0; i < 30; i++) {
            drawDot(canvas, context);
        }
        convertCanvasToImage(canvas)
 
 
        // 点击提交进行验证
        $("#submit").click((e) => {
            var newRand=rand.join('').toUpperCase();
            //下面就是判断是否== 的代码,无需解释
            var oValue = $('#verify').val().toUpperCase();
            console.log(oValue)
            if (oValue == 0) {
                console.log('请输入验证码');
            } else if (oValue != newRand) {
                console.log('验证码不正确,请重新输入');
                oValue = '';
            } else {
                window.open('http://www.baidu.com', '_self');
            }
 
        })
    }
 
    // 随机线
    function drawline(canvas, context) {
        context.moveTo(Math.floor(Math.random() * canvas.width), Math.floor(Math.random() * canvas.height)); //随机线的起点x坐标是画布x坐标0位置,y坐标是画布高度的随机数
        context.lineTo(Math.floor(Math.random() * canvas.width), Math.floor(Math.random() * canvas.height)); //随机线的终点x坐标是画布宽度,y坐标是画布高度的随机数
        context.lineWidth = 0.5; //随机线宽
        context.strokeStyle = 'rgba(50,50,50,0.3)'; //随机线描边属性
        context.stroke(); //描边,即起点描到终点
    }
    // 随机点(所谓画点其实就是画1px像素的线,方法不再赘述)
    function drawDot(canvas, context) {
        var px = Math.floor(Math.random() * canvas.width);
        var py = Math.floor(Math.random() * canvas.height);
        context.moveTo(px, py);
        context.lineTo(px + 1, py + 1);
        context.lineWidth = 0.2;
        context.stroke();
 
    }
    // 绘制图片
    function convertCanvasToImage(canvas) {
        document.getElementById("verifyCanvas").style.display = "none";
        var image = document.getElementById("code_img");
        image.src = canvas.toDataURL("image/png");
        return image;
    }
 
    // 点击图片刷新
    document.getElementById('code_img').onclick = function () {
        $('#verifyCanvas').remove();
        $('#verify').after('<canvas width="100" height="40" id="verifyCanvas"></canvas>')
        drawCode();
    }
</script>
</html>

原创作者:浅浅Chen
原文链接:https://blog.csdn.net/chenjingshen1214/article/details/82627845

Tags:js验证码   前端验证码

很赞哦! ()

文章评论

站点信息