canvas實現隨機驗證碼

来源:https://www.cnblogs.com/sgs123/archive/2019/04/27/10780216.html
-Advertisement-
Play Games

canvas實現隨機驗證碼 知識點 canvas生成背景圖和文字 設置字體樣式和大小 String的fromCharCode(code碼)生成大小寫字母和數字 str.toLowerCase()轉小寫 隨機抽取不重覆的6位數字組成驗證碼字元串 效果圖 html: css: js: javascrip ...


canvas實現隨機驗證碼

知識點

  • canvas生成背景圖和文字 設置字體樣式和大小
  • String的fromCharCode(code碼)生成大小寫字母和數字 str.toLowerCase()轉小寫
  • 隨機抽取不重覆的6位數字組成驗證碼字元串
  • 效果圖

html:

    <div class="wraper">
        <input type="text" maxlength="6" placeholder="請輸入驗證碼,不區分大小寫" class="input">
        <span class="icon"></span>
        <p class="error">驗證碼輸入有誤,請重新輸入</p>
        <div class="canvas-box">
            <canvas id="myCanvas" width="200" height="50"></canvas>
            <input type="button" class="refresh">
        </div>
        <div class="btn">
            <button class="submit">提交</button>
        </div>
    </div>

css:

    * {
        margin: 0;
        padding: 0;
    }
    
    html,
    body {
        width: 100%;
        height: 100%;
        background: hotpink;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    
    .wraper {
        width: 345px;
        margin: 30px;
        padding: 15px;
        border-radius: 5px;
        border: 1px solid #ccc;
        background: #fff;
    }
    
    .input {
        width: 300px;
        padding: 15px;
        border-radius: 5px;
        border: 1px solid #ccc;
        box-sizing: border-box;
    }
    
    .icon {
        float: right;
        width: 20px;
        height: 20px;
        margin-top: 13px;
        background: url('./img/yes.png') no-repeat;
        background-size: 100% 100%;
        display: none;
    }
    
    .error {
        margin-top: 10px;
        color: red;
        font-size: 12px;
        display: none;
    }
    
    .canvas-box {
        margin-top: 15px;
        position: relative;
    }
    
    #myCanvas {
        width: 300px;
        height: 60px;
    }
    
    .canvas-box .refresh {
        position: absolute;
        right: 0;
        top: 50%;
        margin-top: -10px;
        display: inline-block;
        width: 20px;
        height: 20px;
        background: url('./img/refresh.png') no-repeat;
        background-size: 100% 100%;
        border: none;
        outline: none;
        cursor: pointer;
    }
    
    .btn {
        margin-top: 15px;
    }
    
    .btn .submit {
        width: 80px;
        height: 40px;
        border-radius: 5px;
        background: blue;
        color: #fff;
        border: none;
        outline: none;
        cursor: pointer;
    }

js:

    var arr = []; //篩選驗證碼的數組
    var value = '';
    //48-57 數字 65-90 大寫字母  97-122 小寫字母 
    for (var i = 48; i <= 57; i++) {
        arr.push(String.fromCharCode(i));
    }
    for (let i = 65; i <= 90; i++) {
        arr.push(String.fromCharCode(i));
    }
    for (let i = 97; i <= 122; i++) {
        arr.push(String.fromCharCode(i));
    }

    //生成隨機驗證碼
    function getVerification() {
        var codeStr = '';
        var codeArr = [];
        value = '';
        while (true) {
            let a = Math.floor(Math.random() * arr.length);
            if (codeArr.indexOf(a) == -1) {
                codeArr.push(arr[a]);
            }
            if (codeArr.length == 6) {
                break;
            }
        }
        codeStr = codeArr.join(' ');
        value = codeArr.join('').toLowerCase();
        console.log(value)
        var myCanvas = document.getElementById('myCanvas');
        var ctx = myCanvas.getContext('2d');
        var img = new Image();
        img.src = './img/bg_pic.jpg';
        img.onload = function() {
            var pat = ctx.createPattern(img, 'no-repeat');
            ctx.fillStyle = pat;
            ctx.fillRect(0, 0, myCanvas.width, myCanvas.height);
            ctx.textAlign = 'center';
            ctx.fillStyle = '#ccc';
            ctx.font = '30px Roboto Slab';
            ctx.setTransform(1, -0.12, 0.3, 1, 0, 12);
            ctx.fillText(codeStr, myCanvas.width / 2, 35);
        }
    }
    getVerification();

    //事件
    var refresh = document.getElementsByClassName('refresh')[0];
    var submit = document.getElementsByClassName('submit')[0];
    var inputValue = document.getElementsByClassName('input')[0];
    var icon = document.getElementsByClassName('icon')[0];
    var error = document.getElementsByClassName('error')[0];

    refresh.onclick = function() {
        getVerification();
    }
    submit.onclick = function() {
        if (inputValue.value.toLowerCase() === value) {
            icon.style.display = 'inline-block';
            icon.style.background = "url('./img/yes.png') no-repeat";
            icon.style.backgroundSize = "100% 100%";
            error.style.display = 'none';
            getVerification();
        } else {
            icon.style.display = 'inline-block';
            icon.style.background = "url('./img/error.png') no-repeat";
            icon.style.backgroundSize = "100% 100%";
            error.style.display = 'block';
            inputValue.value = '';
        }
    }

參考至騰訊課堂渡一教育


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 第一題 代碼生成表格如: 根據以上代碼生成的表寫出一條查詢語句,查詢結果如下: 第二題 第三題 第四題(這道題難度相對較高) ...
  • 版權聲明:本文為HaiyuKing原創文章,轉載請註明出處! 前言 使用Poi實現android中根據模板文件生成Word文檔的功能。這裡的模板文件是doc文件。如果模板文件是docx文件的話,請閱讀下一篇文章《PoiDocxDemo【Android將表單數據生成Word文檔的方案之二(基於Poi4 ...
  • 前言 最近利用業餘時間閱讀了鬍子大哈寫的《React小書》,從基本的原理講解了React,Redux等等受益頗豐。眼過千遍不如手寫一遍,跟著作者的思路以及參考代碼可以實現基本的Demo,下麵根據自己的理解和參考一些資料,用原生JS從零開始實現一個Redux架構。 一.Redux基本概念 經常用Rea ...
  • 概述:本文描述TypeScript環境搭建,以及基於VSCode的自動編譯設置和調試設置。網路上很多相應文章的方式過時了或者無法試驗成功。 TypeScript簡介:由微軟開發的開源免費的編程語言,是JavaScript語言的一個超集,本質上為JavaScript語言添加了可選的靜態類型和基於類的面 ...
  • 前提摘要 尤大大的vue3.0即將到來,雖然學不動了,但是還要學的啊,據說vue3.0是基於proxy來進行對值進行攔截並操作,所以es6的proxy也是要學習一下的。 一 什麼是proxy Proxy 對象用於定義基本操作的自定義行為(如屬性查找,賦值,枚舉,函數調用等) 摘自MDN Proxy ...
  • 關閉jquery-easui tab標簽頁前觸發事件 by:授客 QQ:1033553122 測試環境 jquery-easyui-1.5.3 需求場景 點擊父頁面tab 頁關閉按鈕時,需要做判斷,判斷該tab頁面是否可以關閉:獲取子頁面js中定義的taskStatus,如果taskStatu不為t ...
  • 所謂js的中的傳值,其實也就是說5種基本數據類型(null,undefind,boolean,number,string) 傳引用也就是說的那個引用數據類型,(array和object) 基本數據類型的值不可變,而引用數據類型的值是可變的 所以當你比較數組和對象時,都是false;除非你是克隆的原份 ...
  • JS的基本語法要求 1. 嚴格區分大小寫 2. 標識符命名要求(與java完全相同) 首個字元只能是 字母、下劃線、美元符號 其他位置只能是 字母、下劃線、美元符號、數字 3. 變數的聲明 JavaScript的聲明必須要使用var關鍵字,聲明方式有兩種。 聲明並且賦值:var 變數名 = 值 先聲 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...