本博客只需要你會Ctrl+C、Ctrl+V就可以了。這裏先給大家看一下效果吧,覺得是你想要的代碼複製過去就可以了。喜歡的朋友記得點關注哦,謝謝您的支持!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no">
<title>生成圖片驗證碼</title>
<script src="https://cdn.bootcss.com/jquery/1.12.3/jquery.min.js"></script>
<style>
#verify {
height: 34px;
vertical-align: top;
font-size: 16px;
}
#code_img {
width: 100px;
height: 40px;
cursor: pointer;
vertical-align: top;
}
</style>
</head>
<body>
點擊圖片重新生成驗證碼
<p>
<input type="text" class="topAlign" id="verify" name="verify" required>
<canvas width="100" height="40" id="verifyCanvas"></canvas>
<img id="code_img">
</p>
<button id="submit" type="submit">提交</button>
</body>
<script>
var nums = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z'
];
var colors = []
drawCode();
// 繪製驗證碼
function drawCode() {
var canvas = document.getElementById("verifyCanvas"); //獲取HTML端畫布
var context = canvas.getContext("2d"); //獲取畫布2D上下文
context.fillStyle = "cornflowerblue"; //畫布填充色
context.fillRect(0, 0, canvas.width, canvas.height);
// 創建漸變
var gradient = context.createLinearGradient(0, 0, canvas.width, 0);
gradient.addColorStop("0", "magenta");
gradient.addColorStop("0.5", "blue");
gradient.addColorStop("1.0", "red");
//清空畫布
context.fillStyle = gradient; //設置字體顏色
context.font = "25px Arial"; //設置字體
var rand = new Array();
var x = new Array();
var y = new Array();
for (var i = 0; i < 4; i++) {
rand[i] = nums[Math.floor(Math.random() * nums.length)]
x[i] = i * 16 + 10;
y[i] = Math.random() * 20 + 20;
context.fillText(rand[i], x[i], y[i]);
}
// console.log(rand);
//畫3條隨機線
for (var i = 0; i < 3; i++) {
drawline(canvas, context);
}
// 畫30個隨機點
for (var i = 0; i < 30; i++) {
drawDot(canvas, context);
}
convertCanvasToImage(canvas)
// 點擊提交進行驗證
$("#submit").click((e) => {
var newRand=rand.join('').toUpperCase();
console.log(newRand);
//下面就是判斷是否== 的代碼,無需解釋
var oValue = $('#verify').val().toUpperCase();
console.log(oValue)
if (oValue == 0) {
alert('請輸入驗證碼');
} else if (oValue != newRand) {
alert('驗證碼不正確,請重新輸入');
oValue = ' ';
} else {
window.open('http://www.baidu.com', '_self');
}
})
}
// 隨機線
function drawline(canvas, context) {
context.moveTo(Math.floor(Math.random() * canvas.width), Math.floor(Math.random() * canvas.height)); //隨機線的起點x座標是畫布x座標0位置,y座標是畫布高度的隨機數
context.lineTo(Math.floor(Math.random() * canvas.width), Math.floor(Math.random() * canvas.height)); //隨機線的終點x座標是畫布寬度,y座標是畫布高度的隨機數
context.lineWidth = 0.5; //隨機線寬
context.strokeStyle = 'rgba(50,50,50,0.3)'; //隨機線描邊屬性
context.stroke(); //描邊,即起點描到終點
}
// 隨機點(所謂畫點其實就是畫1px像素的線,方法不再贅述)
function drawDot(canvas, context) {
var px = Math.floor(Math.random() * canvas.width);
var py = Math.floor(Math.random() * canvas.height);
context.moveTo(px, py);
context.lineTo(px + 1, py + 1);
context.lineWidth = 0.2;
context.stroke();
}
// 繪製圖片
function convertCanvasToImage(canvas) {
document.getElementById("verifyCanvas").style.display = "none";
var image = document.getElementById("code_img");
image.src = canvas.toDataURL("image/png");
return image;
}
// 點擊圖片刷新
document.getElementById('code_img').onclick = function () {
$('#verifyCanvas').remove();
$('#verify').after('<canvas width="100" height="40" id="verifyCanvas"></canvas>')
drawCode();
}
</script>
</html>