獲取驗證碼canvas

1,html部分html

<div class="box">
        <label class="side">驗證碼</label>
        <input class="box-flex input-full" id="verify" type="text" placeholder="請輸入驗證碼" >
        <div class="side" style="width: 100px;margin-left:10px;"><canvas width="100" height="40" id="verifyCanvas"></canvas>
            <img id="code_img"></div>
    </div>

2,js部分canvas

//下面爲驗證碼
    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'];

    drawCode();
    // 繪製驗證碼
    function drawCode() {
        var canvas = document.getElementById("verifyCanvas");  //獲取HTML端畫布
        var context = canvas.getContext("2d");                 //獲取畫布2D上下文
        context.fillStyle = "#565656";                  //畫布填充色
        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 < 5; i++) {
            rand[i] = nums[Math.floor(Math.random() * nums.length)]
            x[i] = i * 16 + 10;
            y[i] = Math.random() * 20 + 20;
            context.fillText(rand[i], x[i], y[i]);
        }
        //畫3條隨機線
        for (var i = 0; i < 3; i++) {
            drawline(canvas, context);
        }

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

    // 隨機線
    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 () {
        $('#verifyCanvas').remove();
        $('#verify').after('<canvas width="100" height="40" id="verifyCanvas"></canvas>');
        drawCode();
    }
相關文章
相關標籤/搜索