JS如何生成隨機(jī)驗證碼
本文實例為大家分享了JS生成隨機(jī)驗證碼的具體代碼,供大家參考,具體內(nèi)容如下
在網(wǎng)站中我們很常見到形形色色的驗證碼,今天我們來用JS來生成一個隨機(jī)的二維碼。
我們需要用到canvas來進(jìn)行驗證碼的繪制
什么是Canvas
HTML5 的 canvas 元素使用 JavaScript 在網(wǎng)頁上繪制圖像。
畫布是一個矩形區(qū)域,您可以控制其每一像素。
canvas 擁有多種繪制路徑、矩形、圓形、字符以及添加圖像的方法。
思路
我們要做的二維碼首先要有隨機(jī)的數(shù)字,其次就是要有隨機(jī)的位置。
HTML
<canvas id="canvas" style="border: 1px solid red; width: 80px; height: 40px;"> </canvas>
JS
function getVerification() { //二維碼 var ctx = document.getElementById("canvas").getContext("2d"); // 清空畫布 ctx.clearRect(0,0, 400, 400); // 設(shè)置字體 ctx.font = "128px bold 黑體"; // 設(shè)置垂直對齊方式 ctx.textBaseline = "top"; // 設(shè)置顏色 ctx.fillStyle = randomColor(); // 繪制文字(參數(shù):要寫的字,x坐標(biāo),y坐標(biāo)) ctx.fillText(getRandomNum(10), 0, getRandomNum(50)); ctx.fillStyle = randomColor(); ctx.fillText(getRandomNum(10), 50, getRandomNum(50)); ctx.fillStyle = randomColor(); ctx.fillText(getRandomNum(10), 100, getRandomNum(50)); ctx.fillStyle = randomColor(); ctx.fillText(getRandomNum(10), 150, getRandomNum(50)); }
我們使用ctx.fillStyle = randomColor();來設(shè)置隨機(jī)的顏色,每寫一個數(shù)字換一個顏色,randomColoe()函數(shù)代碼如下,可以隨機(jī)生成十六進(jìn)制顏色碼。
function randomColor() { var colorValue = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f"; var colorArray = colorValue.split(","); var color = "#"; for (var i = 0; i < 6; i++) { color += colorArray[Math.floor(Math.random() * 16)]; } return color; }
我們使用getRandomNum()來獲取隨機(jī)顯示的數(shù)字和隨機(jī)每次字體的y軸方向的位置。驗證碼的每個數(shù)字分別進(jìn)行獲取。傳入的參數(shù)n來確定隨機(jī)數(shù)范圍。代碼如下:
function getRandomNum(n){ return parseInt(Math.random() * n); }
完整代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>2</title> </head> <body> <canvas id="canvas" style="border: 1px solid red; width: 80px; height: 40px;"></canvas> <span id="yanzhengma"></span><button onclick="getVerification()">看不清</button> <script> function randomColor() { var colorValue = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f"; var colorArray = colorValue.split(","); var color = "#"; for (var i = 0; i < 6; i++) { color += colorArray[Math.floor(Math.random() * 16)]; } return color; } function getRandomNum(n){ return parseInt(Math.random() * n); } function getVerification() { var ctx = document.getElementById("canvas").getContext("2d"); ctx.clearRect(0,0, 400, 400); // 設(shè)置字體 ctx.font = "128px bold 黑體"; // 設(shè)置垂直對齊方式 ctx.textBaseline = "top"; // 設(shè)置顏色 ctx.fillStyle = randomColor(); // 繪制文字(參數(shù):要寫的字,x坐標(biāo),y坐標(biāo)) ctx.fillText(getRandomNum(10), 0, getRandomNum(50)); ctx.fillStyle = randomColor(); ctx.fillText(getRandomNum(10), 50, getRandomNum(50)); ctx.fillStyle = randomColor(); ctx.fillText(getRandomNum(10), 100, getRandomNum(50)); ctx.fillStyle = randomColor(); ctx.fillText(getRandomNum(10), 150, getRandomNum(50)); } getVerification(); </script> </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
小程序顯示彈窗時禁止下層的內(nèi)容滾動實現(xiàn)方法
這篇文章主要介紹了小程序顯示彈窗時禁止下層的內(nèi)容滾動實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03總結(jié)AJAX相關(guān)JS代碼片段和瀏覽器模型
總結(jié)AJAX相關(guān)JS代碼片段和瀏覽器模型...2007-08-08Net微信網(wǎng)頁開發(fā) 使用微信JS-SDK獲取當(dāng)前地理位置過程詳解
這篇文章主要介紹了Net微信網(wǎng)頁開發(fā) 使用微信JS-SDK獲取當(dāng)前地理位置過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08