渡一教育公开课web前端开发JavaScript精英课学习笔记(三十四)jQuery Canvas生成验证码

jQuery Canvas生成验证码

 

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>js验证码</title>
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.js"></script>
    <link href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body,
        html {
            width: 100%;
            height: 100%;
            background-color: #aaa;
        }

        .container {
            width: 300px;
            height: 200px;
            border-radius: 10px;
            border: 1px solid #777;
            margin: 50px auto;
            background-color: #fff;
            padding: 20px 10px;
        }

        .box {
            width: 100%;
            height: 100%;
            position: relative;
            /* border:1px solid blue; */
        }

        .inp-box {
            position: relative;
        }

        .cas-box {
            position: relative;
        }

        input {
            width: 250px;
            height: 30px;
            border: 1px solid #777;
            border-radius: 5px;
            outline: none;
            padding: 0 5px;
            box-sizing: border-box;
        }

        .icon {
            position: absolute;
            font-size: 24px;
            color: rgb(230, 72, 9);
            font-weight: 700;
            top: 2px;
            right: 10px;
            display: none;
        }

        .cicon {
            position: absolute;
            font-size: 24px;
            color: red;
            top: 15px;
            right: 5px;
            padding: 8px;
            background-color: #222;
            color: #ddd;
            border-radius: 10px;
            /* border-radius:50%; */
        }

        .cicon:hover {
            color: #ddd;
            background-color: #222;
            box-shadow: 0 0 10px rgba(0, 0, 0, .8);
            animation: play 2s;
        }

        canvas {
            /* width: 250px;
            height: 50px; */
            background-color: #222;
            border-radius: 5px;
            margin-top: 10px;
        }

        .submit {
            width: 100px;
            height: 30px;
            background-color: rgb(8, 197, 71);
            color: white;
            border: 0;
            font-weight: 700;
        }

        .tip {
            font-size: 12px;
            color: red;
            margin-left: 1em;
            display: none;
        }

        @keyframes play {
            0% {
                transform: rotateZ(0deg);
            }

            100% {
                transform: rotateZ(360deg);
            }
        }
    </style>
</head>

<body>

    <div class="container">
        <div class="box">
            <div class="inp-box">
                <input type="text" name="" id=""><i class="icon fa fa-smile-o"></i></div>
            <p class="tip">验证码输入错误</p>
            <div class="cas-box">
                <canvas class='cas' width='250' height='50'></canvas><i class="cicon fa fa-refresh"></i></div>
            <button class="submit">submit</button>
        </div>
    </div>

    <script>
        let chars = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
        let curText = '';
        function init() {
            for (let i = 65; i < 123; i++) {
                if (i <= 90 || i >= 97) {
                    chars.push(String.fromCharCode(i));
                }
            }
            createText();
            bindEvent();
        }
        init();

        function createText() {
            let can = $('.cas')[0];
            let ctx = can.getContext('2d');//获取画笔
            let fontSize = Math.floor(Math.random() * 20) + 20,
                tStartX = Math.floor(Math.random() * 30) + 10,
                tStartY = Math.floor(Math.random() * 20) + fontSize,
                tRotate = Math.floor((Math.random() - 0.5) * 10),
                lStartX = Math.floor(Math.random() * 50),
                lStartY = Math.floor(Math.random() * 50),
                lEndX = Math.floor(Math.random() * 50) + 200,
                lEndY = Math.floor(Math.random() * 50),
                lWidth = Math.floor(Math.random() * 10) + 10;

            ctx.beginPath();
            ctx.clearRect(0, 0, 250, 50);

            ctx.lineCap = 'round';
            ctx.lineWidth = lWidth;
            ctx.strokeStyle = "#eee";
            ctx.moveTo(lStartX, lStartY);
            ctx.lineTo(lEndX, lEndY);
            ctx.closePath();
            ctx.stroke();


            ctx.beginPath();
            // ctx.translate(tStartX, tStartY);//定好中心点好,旋转起来就简单了
            // ctx.rotate(tRotate * Math.PI / 180);//中心点定好,再调角度
            ctx.globalCompositeOperation = "xor";
            ctx.font = `${fontSize}px sans-serif`;//文字大小、字体样式
            ctx.fillStyle = '#eee';//文字颜色 样式
            curText = '';
            let drawText = '';
            for (let i = 0; i < 6; i++) {
                let tChar = chars[Math.floor(Math.random() * chars.length)];
                curText += tChar;
                drawText += tChar + ' ';
            }
            ctx.fillText(drawText, tStartX, tStartY);//填充的文字,起始坐标点
            ctx.closePath();
            console.log(drawText);
        }

        function bindEvent() {
            $('.cicon').on('click', (event) => {
                $('.icon').css('display', "none");
                $('.tip').css('display', 'none');
                createText();
            });
            $('.submit').on('click', (event) => {
                if ($('input').val() == '') {
                    $('.tip').text('文本框不能为空 !').css('display', 'block');
                } else if ($('input').val() != curText) {
                    $('.tip').text('输入的验证码错误 !').css('display', 'block');
                } else {
                    $('.icon').css('display', "block");
                }
            });
            $('input').focus((event) => {
                $('.tip').css('display', 'none');
                if ($('input').val() != curText) {
                    $('.icon').css('display', "none");
                }
            });
            $("input").bind("input propertychange", (event) => {
                console.log($("input").val());
                if ($("input").val() == curText) {
                    $('.icon').css('display', "block");
                }
            });
        }


    </script>
</body>

</html>

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值