JS如何生成隨機驗證碼
本文實例為大家分享了JS生成隨機驗證碼的具體代碼,供大家參考,具體內(nèi)容如下

在網(wǎng)站中我們很常見到形形色色的驗證碼,今天我們來用JS來生成一個隨機的二維碼。
我們需要用到canvas來進行驗證碼的繪制
什么是Canvas
HTML5 的 canvas 元素使用 JavaScript 在網(wǎng)頁上繪制圖像。
畫布是一個矩形區(qū)域,您可以控制其每一像素。
canvas 擁有多種繪制路徑、矩形、圓形、字符以及添加圖像的方法。
思路
我們要做的二維碼首先要有隨機的數(shù)字,其次就是要有隨機的位置。
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);
// 設置字體
ctx.font = "128px bold 黑體";
// 設置垂直對齊方式
ctx.textBaseline = "top";
// 設置顏色
ctx.fillStyle = randomColor();
// 繪制文字(參數(shù):要寫的字,x坐標,y坐標)
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ù)字換一個顏色,randomColoe()函數(shù)代碼如下,可以隨機生成十六進制顏色碼。
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()來獲取隨機顯示的數(shù)字和隨機每次字體的y軸方向的位置。驗證碼的每個數(shù)字分別進行獲取。傳入的參數(shù)n來確定隨機數(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);
// 設置字體
ctx.font = "128px bold 黑體";
// 設置垂直對齊方式
ctx.textBaseline = "top";
// 設置顏色
ctx.fillStyle = randomColor();
// 繪制文字(參數(shù):要寫的字,x坐標,y坐標)
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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
小程序顯示彈窗時禁止下層的內(nèi)容滾動實現(xiàn)方法
這篇文章主要介紹了小程序顯示彈窗時禁止下層的內(nèi)容滾動實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03
Net微信網(wǎng)頁開發(fā) 使用微信JS-SDK獲取當前地理位置過程詳解
這篇文章主要介紹了Net微信網(wǎng)頁開發(fā) 使用微信JS-SDK獲取當前地理位置過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08

