日日行,不怕千万里

前端封装验证码方法,封装验证码类

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
        <meta http-equiv="Pragma" content="no-cache" />
        <meta http-equiv="Expires" content="0" />
        <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <title>验证码生成示例</title>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mui/3.7.1/css/mui.css" />
        <style>
            /*验证码*/
            .verificationCode{
                width: 8rem;
                height: 100%;
                position: absolute;
                right: 0;
            }
            canvas{
                width: 100%;
                height: 100%;
            }
            #code_img {
                width: 100%;
                height: 100%;
                cursor: pointer;
                vertical-align: top;
            }
        </style>
    </head>
    <body>
        <div class="mui-content">
            <div class="mui-input-row login_input">
                <label>验证码</label>
                <input type="text" id="verifital_input" class="mui-input" placeholder="请输入验证码">
                <div id="verificationCode" class="verificationCode">
                    <canvas width="100" height="40" id="verifyCanvas"></canvas>
                    <img id="code_img">
                </div>
            </div>
        </div>
    </body>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mui/3.7.1/js/mui.min.js" ></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.js" integrity="sha256-fNXJFIlca05BIO2Y5zh1xrShK3ME+/lYZ0j+ChxX2DA=" crossorigin="anonymous"></script>
    <!-- <script type="text/javascript" src="verificationCode.js" ></script> -->
    <script>
        // 自行封装验证码类
        class drawCanvas {
            constructor() {
                this.state = {
                    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'
                    ],
                }
            }

            // 绘制验证码
            drawCode =  (dom, imgDom) => {
                let {nums} = this.state;
                var canvas = dom; //获取HTML端画布
                var context = canvas.getContext("2d"); //获取画布2D上下文
                context.fillStyle = "cornflowerblue"; //画布填充色
                context.fillRect(0, 0, canvas.width, canvas.height); //清空画布
                context.fillStyle = "white"; //设置字体颜色
                context.font = "25px Arial"; //设置字体
                var rand = new Array();
                var x = new Array();
                var y = new Array();
                for (var i = 0; i < 4; i++) {
                    rand.push(rand[i]);
                    rand[i] = nums[Math.floor(Math.random() * nums.length)]
                    x[i] = i * 20 + 10;
                    y[i] = Math.random() * 20 + 20;
                    context.fillText(rand[i], x[i], y[i]);
                }
                //画3条随机线
                for (var i = 0; i < 3; i++) {
                    this.drawline(canvas, context);
                }

                // 画30个随机点
                for (var i = 0; i < 30; i++) {
                    this.drawDot(canvas, context);
                }
                this.convertCanvasToImage(canvas, dom, imgDom);
            }

            // 随机线
            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像素的线,方法不再赘述)
            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();

            }

            // 绘制图片
            convertCanvasToImage = (canvas, dom, imgDom) => {
                dom.style.display = "none";
                var image = imgDom;
                image.src = canvas.toDataURL("image/png");
                return image;
            }
        }
        const canvasItem = new drawCanvas(); // 实例化
        canvasItem.drawCode(document.getElementById("verifyCanvas"), document.getElementById("code_img"));

        document.getElementById('code_img').onclick = function() {
            $('#verifyCanvas').remove();
            $('#code_img').before('<canvas width="100" height="40" id="verifyCanvas"></canvas>')
            canvasItem.drawCode(document.getElementById("verifyCanvas"), document.getElementById("code_img"));
        }
        console.log('-----canvans-----')
    </script>
</html>

有什么不对的地方还望指教,谢谢您的赞。。。。。

posted @ 2019-07-03 16:49  GongXiaoZhu  阅读(205)  评论(0编辑  收藏  举报