|
发表于 2025-8-18 13:17:55
|
显示全部楼层
海南省海口市
自己扣下对你有用的,有生成部分
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>验证码示例</title>
<style>
.captcha-container {
position: relative;
width: 200px;
height: 60px;
margin: 20px 0;
}
#captchaCanvas {
border: 1px solid #ccc;
border-radius: 4px;
cursor: pointer;
}
.refresh-icon {
position: absolute;
right: 10px;
top: 50%;
transform: translateY(-50%);
cursor: pointer;
color: #666;
}
.refresh-icon:hover {
color: #333;
}
input {
padding: 8px;
width: 180px;
margin-top: 10px;
}
button {
margin-top: 10px;
padding: 8px 16px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
#message {
margin-top: 10px;
color: #f44336;
}
</style>
</head>
<body>
<h3>验证码示例</h3>
<div class="captcha-container">
<canvas id="captchaCanvas" width="200" height="60"></canvas>
<span class="refresh-icon">↺</span>
</div>
<input type="text" id="inputCode" placeholder="请输入验证码">
<button>验证</button>
<div id="message"></div>
<script>
// 验证码字符集
const codeChars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
let canvas = document.getElementById('captchaCanvas');
let ctx = canvas.getContext('2d');
let refreshIcon = document.querySelector('.refresh-icon');
let currentCode = '';
// 生成随机验证码
function generateCode(length = 4) {
let code = '';
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * codeChars.length);
code += codeChars[randomIndex];
}
return code;
}
// 绘制验证码
function drawCaptcha() {
// 清空画布
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 绘制背景
ctx.fillStyle = '#f3f3f3';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 生成验证码
currentCode = generateCode();
// 设置字体
ctx.font = 'bold 30px Arial, sans-serif';
// 绘制每个字符(随机位置和旋转角度)
for (let i = 0; i < currentCode.length; i++) {
// 随机颜色
ctx.fillStyle = `rgb(${Math.random() * 100}, ${Math.random() * 100}, ${Math.random() * 100})`;
// 保存当前状态
ctx.save();
// 随机位置和旋转
ctx.translate(40 + i * 30, 35);
ctx.rotate((Math.random() - 0.5) * 0.5);
// 绘制字符
ctx.fillText(currentCode[i], 0, 0);
// 恢复状态
ctx.restore();
}
// 绘制干扰线
for (let i = 0; i < 5; i++) {
ctx.strokeStyle = `rgb(${Math.random() * 200}, ${Math.random() * 200}, ${Math.random() * 200})`;
ctx.beginPath();
ctx.moveTo(Math.random() * canvas.width, Math.random() * canvas.height);
ctx.lineTo(Math.random() * canvas.width, Math.random() * canvas.height);
ctx.stroke();
}
// 绘制噪点
for (let i = 0; i < 50; i++) {
ctx.fillStyle = `rgb(${Math.random() * 150}, ${Math.random() * 150}, ${Math.random() * 150})`;
ctx.beginPath();
ctx.arc(Math.random() * canvas.width, Math.random() * canvas.height, 1, 0, 2 * Math.PI);
ctx.fill();
}
}
// 验证输入的验证码
function checkCaptcha() {
const inputCode = document.getElementById('inputCode').value;
const message = document.getElementById('message');
if (inputCode === '') {
message.textContent = '请输入验证码';
return;
}
if (inputCode.toUpperCase() === currentCode.toUpperCase()) {
message.textContent = '验证成功!';
message.style.color = '#4CAF50';
} else {
message.textContent = '验证码错误,请重新输入';
message.style.color = '#f44336';
// 自动刷新验证码
drawCaptcha();
}
}
// 点击刷新验证码
refreshIcon.addEventListener('click', drawCaptcha);
canvas.addEventListener('click', drawCaptcha);
// 初始化验证码
drawCaptcha();
</script>
</body>
</html>
|
|