js生成圖片驗證碼

頁面代碼:(使用到了mui樣式)javascript

<!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="../css/mui/mui.min.css" />
        <style>
            /*驗證碼*/
            .verificationCode{
                width: 4rem;
                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="../js/mui/mui.js" ></script>
    <script type="text/javascript" src="../js/jquery-3.3.1.min.js" ></script>
    <script type="text/javascript" src="verificationCode.js" ></script>
    <script>
        console.log(verVal)
    </script>
</html>

 

verificationCode.js代碼:css

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 str = '';
var verVal = drawCode();
// 繪製驗證碼
function drawCode (str) {
    var canvas = document.getElementById("verifyCanvas"); //獲取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]);
    }
    str = rand.join('').toUpperCase();
    //畫3條隨機線
    for (var i = 0; i < 3; i++) {
        drawline(canvas, context);
    }

    // 畫30個隨機點
    for (var i = 0; i < 30; i++) {
        drawDot(canvas, context);
    }
    convertCanvasToImage(canvas);
    return str;
}

// 隨機線
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() {
    resetCode();
}

function resetCode () {
    $('#verifyCanvas').remove();
    $('#code_img').before('<canvas width="100" height="40" id="verifyCanvas"></canvas>')
    verVal = drawCode();
}
相關文章
相關標籤/搜索