Canvas<文字驗(yàn)證碼效果>

效果圖:
20200316.gif

因?yàn)槿羰窃陧?xiàng)目中使用,一般會(huì)根據(jù)后端返回的信息,前端進(jìn)行生成驗(yàn)證碼,所以此處并沒(méi)有針對(duì)驗(yàn)證碼做隨機(jī)處理。

 /* 畫(huà)布背景色 */
background: #f1f1f1;
    let dotNum = 20; //背景小點(diǎn)數(shù)量
    let lineNum = 4; //背景線(xiàn)條數(shù)量
    let codeText = "FG2H" //驗(yàn)證碼文字
代碼如下:
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body,
        html {
            height: 100%;
        }

        body {
            display: flex;
            justify-content: center;
            align-items: center;
            margin: 0;
        }

        .verification_box {
            box-sizing: border-box;
            display: flex;
        }
        .code canvas {
            display: block;
            /* 畫(huà)布背景色 */
            background: #f1f1f1;
        }
    </style>
</head>

<body>
    <div class="verification_box">
        <div class="code">
            <canvas id="code_canvas" width="150" height="60"></canvas>
        </div>
    </div>
</body>
<script>
    let canvas = document.getElementById("code_canvas");
    let ctx = canvas.getContext("2d");
    let c_w = canvas.width;
    let c_h = canvas.height;
    let dotNum = 20; //背景小點(diǎn)數(shù)量
    let lineNum = 4; //背景線(xiàn)條數(shù)量
    let codeText = "FG2H" //驗(yàn)證碼文字

    //隨即方法
    function random(min, max) {
        return Math.random() * (max - min + 1) + min;
    }
    //定義一個(gè)隨機(jī)顏色
    function rndCol() {
        var r = Math.floor(Math.random() * 255);
        var g = Math.floor(Math.random() * 255);
        var b = Math.floor(Math.random() * 255);
        return "rgb(" + r + "," + g + "," + b + ")";
    }
    //畫(huà)小點(diǎn)
    function drawDot() {
        for (let i = 0; i < dotNum; i++) {
            ctx.beginPath();
            ctx.fillStyle = "#cccccc";
            ctx.arc(Math.random() * c_w, Math.random() * c_h, 1, 0, Math.PI * 2);
            ctx.fill();
            ctx.closePath();
        }
    }
    //畫(huà)線(xiàn)
    function drawLine() {
        for (let i = 0; i < lineNum; i++) {
            let start_x = Math.random() * c_w;
            let start_y = Math.random() * c_h;
            let end_x = Math.random() * c_w;
            let end_y = Math.random() * c_h;
            ctx.beginPath();
            ctx.strokeStyle = rndCol()
            ctx.lineWidth = 2;
            ctx.moveTo(start_x, start_y);
            ctx.lineTo(end_x, end_y);
            ctx.stroke();
            ctx.closePath();
        }
    }
    //畫(huà)文字
    function drawText() {
        let textArray = codeText.split('');
        for (let i = 0; i < textArray.length; i++) {
            let fs = random(30, 40);//字體的大小
            let deg = random(-30, 30);//字體旋轉(zhuǎn)角度
            ctx.font = fs + "px Verdana"
            ctx.fillStyle = rndCol();
            ctx.textBaseline = "top";
            // canvas.save()用來(lái)保存先前狀態(tài)的
            // canvas.restore()用來(lái)恢復(fù)之前保存的狀態(tài)
            // 注:兩種方法必須搭配使用,否則沒(méi)有效果
            ctx.save();
            ctx.translate(c_w / textArray.length * i + 15, random(15, c_h - 15));
            ctx.rotate(deg * Math.PI / 180);
            ctx.fillText(textArray[i], -15 + 5, -15);
            ctx.restore();
        }
    }
    drawText()
    drawDot()
    drawLine()
</script>

</html>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 馬奇諾防線(xiàn)是二戰(zhàn)前法國(guó)耗時(shí)十余年修建的防御工事,十分堅(jiān)固,但是由于造價(jià)昂貴,僅修建了法德邊境部分,綿延數(shù)百公里,而...
    點(diǎn)融黑幫閱讀 3,439評(píng)論 4 26
  • 前端開(kāi)發(fā)面試題 面試題目: 根據(jù)你的等級(jí)和職位的變化,入門(mén)級(jí)到專(zhuān)家級(jí),廣度和深度都會(huì)有所增加。 題目類(lèi)型: 理論知...
    怡寶丶閱讀 2,684評(píng)論 0 7
  • 生活不止眼前的茍且,還有詩(shī)和遠(yuǎn)方。 當(dāng)生活的緊迫使人感到孤獨(dú)時(shí),每個(gè)人又是多么想逃離現(xiàn)實(shí)去尋找內(nèi)心的“詩(shī)和遠(yuǎn)方?!?..
    竹胤的桃花源閱讀 750評(píng)論 9 5
  • 做一位無(wú)可替代的教練,和善堅(jiān)定并行,有權(quán)威,有規(guī)矩有自由,有限制地選擇,感恩s賦予我這個(gè)身份。
    志_c3e9閱讀 157評(píng)論 0 4
  • 叮鈴鈴......五點(diǎn)鐘的鬧鐘響起,起床的第一個(gè)信號(hào);當(dāng)然我也可以選擇取消鬧鈴,不過(guò)我還設(shè)置了五點(diǎn)半的手表震動(dòng)鬧鈴...
    江風(fēng)閱讀 387評(píng)論 1 1

友情鏈接更多精彩內(nèi)容