溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

html5生成驗證碼的方法

發(fā)布時間:2021-01-20 10:48:59 來源:億速云 閱讀:172 作者:小新 欄目:web開發(fā)

這篇文章主要介紹html5生成驗證碼的方法,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

思路介紹:

可以利用html5的canvas標簽先生成畫布,然后在畫布上利用隨機數(shù)字生成驗證碼,背景用隨機顏色和雜亂的直線來代替。

高級方法:

利用表單插件屬性綁定驗證碼數(shù)據(jù)(json)可以在發(fā)送時候或者異步通信進行后臺數(shù)據(jù)獲取與檢查。

具體代碼:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>驗證碼</title>
<style type="text/css">
    #canvas{
        cursor:pointer;
    }
</style>
</head>
<body>
    <canvas id="canvas" width="150px" height="50px"></canvas>
<script>
        //生成隨機數(shù)
    function randomNum(min,max){
        return Math.floor(Math.random()*(max-min)+min);
    }
        //生成隨機顏色RGB分量
    function randomColor(min,max){
        var _r = randomNum(min,max);
        var _g = randomNum(min,max);
        var _b = randomNum(min,max);
        return "rgb("+_r+","+_g+","+_b+")";
    }
    //先阻止畫布默認點擊發(fā)生的行為再執(zhí)行drawPic()方法
    document.getElementById("canvas").onclick = function(e){
        e.preventDefault();
        drawPic();
    };
    function drawPic(){
        //獲取到元素canvas
        var $canvas = document.getElementById("canvas");
        var _str = "0123456789";//設置隨機數(shù)庫
        var _picTxt = "";//隨機數(shù)
        var _num = 4;//4個隨機數(shù)字
        var _width = $canvas.width;
        var _height = $canvas.height;
        var ctx = $canvas.getContext("2d");//獲取 context 對象
        ctx.textBaseline = "bottom";//文字上下對齊方式--底部對齊
        ctx.fillStyle = randomColor(180,240);//填充畫布顏色
        ctx.fillRect(0,0,_width,_height);//填充矩形--畫畫
        for(var i=0; i<_num; i++){
            var x = (_width-10)/_num*i+10;
            var y = randomNum(_height/2,_height);
            var deg = randomNum(-45,45);
            var txt = _str[randomNum(0,_str.length)];
            _picTxt += txt;//獲取一個隨機數(shù)
            ctx.fillStyle = randomColor(10,100);//填充隨機顏色
            ctx.font = randomNum(16,40)+"px SimHei";//設置隨機數(shù)大小,字體為SimHei
            ctx.translate(x,y);//將當前xy坐標作為原始坐標
            ctx.rotate(deg*Math.PI/180);//旋轉隨機角度
            ctx.fillText(txt, 0,0);//繪制填色的文本
            ctx.rotate(-deg*Math.PI/180);
            ctx.translate(-x,-y);
        }
        for(var i=0; i<_num; i++){
            //定義筆觸顏色
            ctx.strokeStyle = randomColor(90,180);
            ctx.beginPath();
            //隨機劃線--4條路徑
            ctx.moveTo(randomNum(0,_width), randomNum(0,_height));
            ctx.lineTo(randomNum(0,_width), randomNum(0,_height));
            ctx.stroke();
        }
        for(var i=0; i<_num*10; i++){
            ctx.fillStyle = randomColor(0,255);
            ctx.beginPath();
            //隨機畫原,填充顏色
            ctx.arc(randomNum(0,_width),randomNum(0,_height), 1, 0, 2*Math.PI);
            ctx.fill();
        }
        return _picTxt;//返回隨機數(shù)字符串
    }
    drawPic();
</script>
</body>
</html>

以上是“html5生成驗證碼的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI