欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS如何生成隨機(jī)驗(yàn)證碼

 更新時(shí)間:2020年03月02日 13:49:07   作者:mencre  
這篇文章主要為大家詳細(xì)介紹了JS如何生成隨機(jī)驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

在網(wǎng)站中我們很常見到形形色色的驗(yàn)證碼,今天我們來(lái)用JS來(lái)生成一個(gè)隨機(jī)的二維碼。

我們需要用到canvas來(lái)進(jìn)行驗(yàn)證碼的繪制

什么是Canvas

HTML5 的 canvas 元素使用 JavaScript 在網(wǎng)頁(yè)上繪制圖像。
畫布是一個(gè)矩形區(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è)置垂直對(duì)齊方式
 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();來(lái)設(shè)置隨機(jī)的顏色,每寫一個(gè)數(shù)字換一個(gè)顏色,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()來(lái)獲取隨機(jī)顯示的數(shù)字和隨機(jī)每次字體的y軸方向的位置。驗(yàn)證碼的每個(gè)數(shù)字分別進(jìn)行獲取。傳入的參數(shù)n來(lái)確定隨機(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è)置垂直對(duì)齊方式
 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>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論