js+canvas實(shí)現(xiàn)驗(yàn)證碼功能
剛剛開(kāi)始接觸canvas,寫(xiě)個(gè)驗(yàn)證碼小功能練練手,實(shí)現(xiàn)效果圖如下:
主要代碼如下:
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <link rel="stylesheet" href="index.css" rel="external nofollow" > </head> <body> <div class="wrapper"> <div class="inputBox"> <input type="text" class = 'inputCaptcha' placeholder = "請(qǐng)輸入驗(yàn)證碼"> <span class="captchaIcon"></span> </div> <p class="errorText"></p> <div class="canvasBox"> <div class="imageBox"> <canvas width =300 height=80 id = 'canvasCaptcha'></canvas> <input type="button" class='refresh'> </div> </div> <button class="captchaSubmit">submit</button> </div> <script src='./jquery.js'></script> <script src = './index.js'></script> </body> </html>
css
* { margin: 0; padding: 0; } .wrapper { width: 345px; border: 1px solid #ccc; border-radius: 10px; padding: 20px 10px; margin: 30px 30px; } .inputBox { width: 345px; margin: 0 auto 10px; position: relative; } .inputBox .inputCaptcha { display: inline-block; height: 50px; width: 86%; text-indent: 1em; border: 1px solid #ddd; border-radius: 5px; } .inputBox .captchaIcon { display: none; position: absolute; top: 50%; right: 0px; margin-top: -16px; width: 32px; height: 32px; background-size: 100% 100%; } .errorText { width: 345px; margin: 0 auto; font-size: 12px; color: red; display: none; } .canvasBox { width: 345px; margin: 10px auto; position: relative; } #canvasCaptcha { border-radius: 10px; } .canvasBox .refresh { width: 34px; height: 34px; position: absolute; right: 0px; top: 50%; margin-top: -17px; border: 0; border-radius: 6px; background-image: url('./images/update.png'); background-size: cover; } .captchaSubmit { padding: 10px 20px; background-color: #62b900; border: 0; font-size: 20px; border-radius: 5px; color: #fff; }
js
var arr = [0,1,2,3,4,5,6,7,8,9]; for(var i = 65;i < 122;i++){ if(i>90&&i<97){ continue; } arr.push(String.fromCharCode(i)); } //每個(gè)驗(yàn)證碼可能出現(xiàn)的字母或數(shù)字(區(qū)分大小寫(xiě)) var len = arr.length; //驗(yàn)證碼的長(zhǎng)度 var canvasStr,value; //驗(yàn)證碼值 function createCanvas(){ canvasStr = ''; value = ''; for(var i =0 ;i < 6;i++){ var a = arr[Math.floor(Math.random()*len)]; canvasStr += a+' '; value += a; } //canvas var oCanvas = document.getElementById('canvasCaptcha'); var ctx = oCanvas.getContext('2d'); var w = oCanvas.width; var h = oCanvas.height; var oImg = new Image(); oImg.src = './images/bg.jpg'; oImg.onload = function(){ var pattern = ctx.createPattern(oImg,'repeat'); ctx.fillStyle = pattern; ctx.fillRect(0,0,w,h); ctx.fillStyle = '#ccc'; ctx.textAlign = 'center'; ctx.font = '46px Roboto Slab'; ctx.setTransform(1,-0.12,0.2,1,0,12); ctx.fillText(canvasStr,w/2,60); } } createCanvas(); //驗(yàn)證輸入的驗(yàn)證碼與圖中驗(yàn)證碼時(shí)候相等 function captcha(e){ if(e == value){ return true; } else{ return false; } } //點(diǎn)擊提交按鈕時(shí)的結(jié)果 function showResult(){ var inputValue = $('.inputCaptcha').val(); if(inputValue == '' ||inputValue == null || inputValue == 'undefined'){ $('.errorText').css({display:'inline-block'}).html('驗(yàn)證碼不能為空,請(qǐng)重新輸入!'); $('.captchaIcon').css({display:'inline-block',backgroundImage:"url('./images/false.png')"}); }else{ var flag = captcha(inputValue); if(!flag){ $('.errorText').css({display:'inline-block'}).html('驗(yàn)證碼錯(cuò)誤,請(qǐng)重新輸入!'); $('.captchaIcon').css({display:'inline-block',backgroundImage:"url('./images/false.png')"}); }else{ $('.captchaIcon').css({display:'inline-block',backgroundImage:"url('./images/true.png')"}); } } } $('.captchaSubmit').click(showResult);//提交 $('.refresh').click(createCanvas);//刷新 //點(diǎn)擊input框 $('.inputCaptcha').focus(function(){ $('.errorText').add($('.captchaIcon')).fadeOut(100); })
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 原生js+canvas實(shí)現(xiàn)驗(yàn)證碼
- js+h5 canvas實(shí)現(xiàn)圖片驗(yàn)證碼
- js+canvas繪制圖形驗(yàn)證碼
- JavaScript使用canvas繪制隨機(jī)驗(yàn)證碼
- JS+HTML5 canvas繪制驗(yàn)證碼示例
- JavaScript Canvas實(shí)現(xiàn)驗(yàn)證碼
- js+canvas實(shí)現(xiàn)滑動(dòng)拼圖驗(yàn)證碼功能
- 使用canvas及js簡(jiǎn)單生成驗(yàn)證碼方法
- js實(shí)現(xiàn)點(diǎn)擊獲取驗(yàn)證碼倒計(jì)時(shí)效果
- js canvas實(shí)現(xiàn)驗(yàn)證碼并獲取驗(yàn)證碼功能
相關(guān)文章
JS項(xiàng)目中對(duì)本地存儲(chǔ)進(jìn)行二次的封裝的實(shí)現(xiàn)
這篇文章主要介紹了JS項(xiàng)目中對(duì)本地存儲(chǔ)進(jìn)行二次的封裝,這里我們將要使用到的key存儲(chǔ)下來(lái),新建一個(gè)叫constant-storage.js的文件,對(duì)外暴露一些key的鍵名,也方便后期統(tǒng)一修改,這里因?yàn)槎际呛懔?,所以名稱(chēng)我們都用大寫(xiě)表示,需要的朋友可以參考下2022-07-07layui2.0使用table+laypage實(shí)現(xiàn)真分頁(yè)
這篇文章主要為大家詳細(xì)介紹了layui2.0使用table+laypage實(shí)現(xiàn)真分頁(yè),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07TypeScript實(shí)現(xiàn)數(shù)組和樹(shù)的相互轉(zhuǎn)換
樹(shù)或者圖是個(gè)比較抽象的概念,并不存在這樣的數(shù)據(jù)類(lèi)型。數(shù)組就比較簡(jiǎn)單了,因此數(shù)組和樹(shù)的轉(zhuǎn)換可以理解為數(shù)組和對(duì)象之間的轉(zhuǎn)換。本文將用TypeScript實(shí)現(xiàn)數(shù)組和樹(shù)的相互轉(zhuǎn)換,感興趣的可以了解一下2022-06-06js實(shí)現(xiàn)網(wǎng)頁(yè)版貪吃蛇游戲
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)網(wǎng)頁(yè)版貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02JavaScript+Java實(shí)現(xiàn)HTML頁(yè)面轉(zhuǎn)為PDF文件保存的方法
借助iText這個(gè)Java庫(kù),我們可以將HTML文件保存為圖片文件進(jìn)而轉(zhuǎn)換成PDF格式,接下來(lái)就來(lái)具體看下JavaScript+Java實(shí)現(xiàn)HTML頁(yè)面轉(zhuǎn)為PDF文件保存的方法2016-05-05