js+h5 canvas實(shí)現(xiàn)圖片驗(yàn)證碼
本文實(shí)例為大家分享了js+h5 canvas實(shí)現(xiàn)圖片驗(yàn)證碼的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)效果
一、使用技術(shù)
原生js技術(shù)+html5 canvas畫圖
利用Math.random()函數(shù)隨機(jī)生成 顏色 字符串 及障礙物
點(diǎn)擊 驗(yàn)證碼可變更驗(yàn)證碼圖案
二、使用步驟
1.html+css
代碼如下(示例):
<div class="login_code_box"> <div class="login_code"> <input type="text" name="login_code" id="login_code" placeholder="請(qǐng)輸入驗(yàn)證碼"> </div> <canvas id="canvas" > </canvas> <!-- <img src="" alt="" title="看不清?單擊此處刷新" class="login_code_img" > --> </div>
#canvas{ width: 130px; height: 45px; display: inline-block; border: 1px solid #c9c9c9; border-radius: 5px; }
2.js
代碼如下(示例):
//驗(yàn)證碼生成 let canvas=document.getElementById('canvas')//畫布對(duì)象 let show_num=[]//裝驗(yàn)證碼的數(shù)組 //產(chǎn)生隨機(jī)顏色 function randomColor(){ var r= Math.floor( Math.random()*256); var g= Math.floor( Math.random()*256); var b= Math.floor( Math.random()*256); return "rgb("+r+","+g+","+b+") " } //畫布 function draw(show_num){ let canvas_clientWidth=document.getElementById('canvas').clientWidth//畫布長(zhǎng)度 let canvas_clientHeight=document.getElementById('canvas').clientHeight//畫布高度 let context = canvas.getContext("2d")//畫布環(huán)境 創(chuàng)建 context 對(duì)象: canvas.width=canvas_clientWidth canvas.height=canvas_clientHeight let str="A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0,q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l,z,x,c,v,b,n,m"; let astr=str.split(',');//分割字符串形成數(shù)組 let sLength=astr.length;//數(shù)組長(zhǎng)度 for (let i = 0; i <= 3; i++) { let j=Math.floor(Math.random()*sLength);//隨機(jī)索引 let deg=Math.random()*30*Math.PI/180;//0-30隨機(jī)弧度 let text=astr[j];//隨機(jī)字符 show_num[i]=text//驗(yàn)證碼字符數(shù)組 let x=10+i*20//x坐標(biāo) let y=20+Math.random()*8//y坐標(biāo) //位移 旋轉(zhuǎn)角度 顏色 文字 樣式開始位置 context.font='bold 23px 微軟雅黑' context.translate(x, y); context.rotate(deg); context.fillStyle = randomColor(); context.fillText(text,0,0) context.rotate(-deg) context.translate(-x,-y) } //驗(yàn)證碼顯示小點(diǎn) for(let i=0;i<=30;i++){ context.strokeStyle=randomColor()//設(shè)置隨機(jī)色用小點(diǎn)的顏色 context.beginPath();//開始一條路徑 let m=Math.random()*canvas_clientWidth; let n=Math.random()*canvas_clientHeight; context.moveTo(m,n)//移動(dòng) context.lineTo(m + 1, n + 1);//添加一個(gè)新點(diǎn),然后在畫布中創(chuàng)建從該點(diǎn)到最后指定點(diǎn)的線條 context.stroke();//畫上面定義好的路徑 } //驗(yàn)證碼顯示線條 for (let i = 0; i < 8; i++) { context.strokeStyle=randomColor() context.beginPath() context.moveTo(Math.random()*canvas_clientWidth,Math.random()*canvas_clientHeight); context.lineTo(Math.random()*canvas_clientWidth,Math.random()*canvas_clientHeight) context.stroke() } } draw(show_num) console.log(show_num); canvas.onclick=()=>{ draw(show_num) console.log(show_num); }
總結(jié)
創(chuàng)建canvas畫布,利用隨機(jī)函數(shù),創(chuàng)建隨機(jī)顏色 。
draw()函數(shù),用于驗(yàn)證碼的生成。字符串分隔存入數(shù)組
循環(huán)遍歷生成驗(yàn)證碼的個(gè)數(shù)
給當(dāng)前字符串一個(gè)位移及旋轉(zhuǎn)角度 文字樣式等
隨機(jī)函數(shù)生成小點(diǎn)及線段 context.stroke()畫好以上定義好的顏色 位移長(zhǎng)短的路徑,并顯示在canvas畫布上
點(diǎn)擊canvas畫布 可重新生成驗(yàn)證碼內(nèi)容
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- js插件實(shí)現(xiàn)圖片滑動(dòng)驗(yàn)證碼
- node.js WEB開發(fā)中圖片驗(yàn)證碼的實(shí)現(xiàn)方法
- JSP實(shí)用教程之簡(jiǎn)易圖片驗(yàn)證碼的實(shí)現(xiàn)方法(附源碼)
- 純JS實(shí)現(xiàn)圖片驗(yàn)證碼功能并兼容IE6-8(推薦)
- Nodejs中使用captchapng模塊生成圖片驗(yàn)證碼
- Qt QThreadPool線程池的實(shí)現(xiàn)
- Node.JS用純JavaScript生成圖片或滑塊式驗(yàn)證碼功能
- 在JSP頁(yè)面中動(dòng)態(tài)生成圖片驗(yàn)證碼的方法實(shí)例
- Js逆向?qū)崿F(xiàn)滑動(dòng)驗(yàn)證碼圖片還原的示例代碼
- JS實(shí)現(xiàn)圖片驗(yàn)證碼功能
相關(guān)文章
基于JS實(shí)現(xiàn)導(dǎo)航條flash導(dǎo)航條
flash導(dǎo)航條在網(wǎng)站建設(shè)中應(yīng)用比較廣泛,此種效果給瀏覽者帶來(lái)極好的視覺(jué)效果,非常棒,下面小編給大家介紹基于JS實(shí)現(xiàn)導(dǎo)航條flash導(dǎo)航條,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-06-06JS實(shí)現(xiàn)點(diǎn)擊圖片在當(dāng)前頁(yè)面放大并可關(guān)閉的漂亮效果
點(diǎn)擊圖片在當(dāng)前頁(yè)面放大的漂亮效果實(shí)現(xiàn)方法有很多,在本文將為大家介紹下使用Lightbox JS是如何實(shí)現(xiàn)的,感興趣的朋友不要錯(cuò)過(guò)2013-10-10原生JS操作網(wǎng)頁(yè)給p元素添加onclick事件及表格隔行變色
原生JS操作網(wǎng)頁(yè),給網(wǎng)頁(yè)中的所有p元素添加onclick事件,使一個(gè)特定的表格隔行變色等等,感興趣的朋友可以參考下2013-12-12extjs中form與grid交互數(shù)據(jù)(record)的方法
這篇文章介紹了extjs中form與grid交互數(shù)據(jù)(record)的方法,有需要的朋友可以參考一下2013-08-08javascript實(shí)現(xiàn)樹形菜單的方法
這篇文章主要介紹了javascript實(shí)現(xiàn)樹形菜單的方法,涉及javascript動(dòng)態(tài)操作頁(yè)面元素與節(jié)點(diǎn)屬性的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07Bootstrap中g(shù)lyphicons-halflings-regular.woff字體報(bào)404錯(cuò)notfound的解
這篇文章主要介紹了 Bootstrap中g(shù)lyphicons-halflings-regular.woff字體報(bào)404錯(cuò)notfound的解決方法,需要的朋友可以參考下2017-01-01JavaScript中常見內(nèi)置函數(shù)用法示例
這篇文章主要介紹了JavaScript中常見內(nèi)置函數(shù)用法,結(jié)合實(shí)例形式分析了JavaScript常用內(nèi)置函數(shù)的功能、參數(shù)、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2018-05-05