<!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> body { font-family: Arial, sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; background-color: #f5f5f5; margin: 0; } .container { background: white; padding: 20px 30px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); text-align: center; } canvas { border: 1px solid #ccc; cursor: pointer; margin-top: 10px; } input[type="text"] { padding: 8px; margin-top: 10px; width: 120px; text-align: center; border: 1px solid #ccc; border-radius: 4px; } button { margin-top: 15px; padding: 8px 16px; background-color: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; } button:hover { background-color: #0056b3; } .message { margin-top: 10px; font-size: 14px; color: #d9534f; } </style> </head> <body> <div class="container"> <h3>请输入验证码</h3> <canvas id="captchaCanvas" width="120" height="40"></canvas> <br /> <input type="text" id="userInput" maxlength="4" placeholder="输入验证码" autocomplete="off" /> <br /> <button onclick="validateCaptcha()">验证</button> <div id="message" class="message"></div> </div> <script> let captchaCode = ''; // 生成随机验证码 function generateCaptcha() { const chars = 'ABCDEFGHJKMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789'; let result = ''; for (let i = 0; i < 4; i++) { result += chars.charAt(Math.floor(Math.random() * chars.length)); } return result; } // 绘制验证码到 Canvas function drawCaptcha() { const canvas = document.getElementById('captchaCanvas'); const ctx = canvas.getContext('2d'); captchaCode = generateCaptcha(); // 清空画布 ctx.clearRect(0, 0, canvas.width, canvas.height); // 背景色 ctx.fillStyle = '#ffffff'; ctx.fillRect(0, 0, canvas.width, canvas.height); // 干扰线 for (let i = 0; i < 5; i++) { ctx.strokeStyle = `rgb(${Math.floor(Math.random() * 100)}, ${Math.floor(Math.random() * 100)}, ${Math.floor(Math.random() * 100)})`; ctx.beginPath(); ctx.moveTo(Math.random() * canvas.width, Math.random() * canvas.height); ctx.lineTo(Math.random() * canvas.width, Math.random() * canvas.height); ctx.stroke(); } // 验证码文字 ctx.font = 'bold 24px Arial'; ctx.textBaseline = 'middle'; for (let i = 0; i < captchaCode.length; i++) { ctx.fillStyle = `rgb(${Math.floor(Math.random() * 100 + 100)}, ${Math.floor(Math.random() * 100 + 100)}, ${Math.floor(Math.random() * 100 + 100)})`; const x = 10 + i * 25; const y = canvas.height / 2 + (Math.random() - 0.5) * 10; ctx.save(); ctx.translate(x, y); ctx.rotate((Math.random() - 0.5) * 0.5); // 轻微旋转 ctx.fillText(captchaCode[i], 0, 0); ctx.restore(); } // 干扰点 for (let i = 0; i < 30; i++) { ctx.fillStyle = `rgb(${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)})`; ctx.beginPath(); ctx.arc(Math.random() * canvas.width, Math.random() * canvas.height, 1, 0, 2 * Math.PI); ctx.fill(); } } // 刷新验证码(点击 Canvas) document.getElementById('captchaCanvas').addEventListener('click', drawCaptcha); // 验证用户输入 function validateCaptcha() { const userInput = document.getElementById('userInput').value.trim(); const messageEl = document.getElementById('message'); if (userInput === '') { messageEl.textContent = '请输入验证码!'; return; } if (userInput.toLowerCase() === captchaCode.toLowerCase()) { messageEl.style.color = '#28a745'; messageEl.textContent = '验证成功!'; // 可在此处提交表单或执行其他操作 } else { messageEl.style.color = '#d9534f'; messageEl.textContent = '验证码错误,请重试!'; drawCaptcha(); // 验证失败自动刷新 document.getElementById('userInput').value = ''; } } // 页面加载时生成验证码 window.onload = drawCaptcha; </script> </body> </html>图形验证码示例
张小明
前端开发工程师
GPT-SoVITS语音克隆应用场景全景图:20个行业用例
GPT-SoVITS语音克隆应用场景全景图:20个行业用例 在数字内容爆炸式增长的今天,用户对个性化、情感化的声音体验需求正以前所未有的速度攀升。从智能助手到有声书,从虚拟偶像到远程教育,传统千篇一律的机械音早已无法满足人们对“像…
Keil4串口调试输出分析:操作指南配合仿真
Keil4串口调试输出实战:用软件仿真高效定位嵌入式问题你有没有遇到过这种情况——代码写完了,烧进板子却“没反应”?断点调试又太慢,变量太多根本抓不住重点。这时候,最直接的办法是什么?让程序自己“说话”…
GPT-SoVITS与RVC对比:哪个更适合语音克隆新手?
GPT-SoVITS与RVC对比:哪个更适合语音克隆新手? 在AI生成内容爆发的今天,个性化语音不再是影视特效或大厂专属的技术。越来越多的内容创作者、独立开发者甚至普通用户开始尝试“克隆”自己的声音——用于制作有声书、虚拟主播、智能助手&…
GPT-SoVITS语音合成动态范围分析:高低频表现均衡性
GPT-SoVITS语音合成动态范围分析:高低频表现均衡性 在智能语音助手、虚拟偶像、有声读物等应用日益普及的今天,用户对“像人”的声音不再满足于基本可懂,而是追求更细腻的情感表达与真实的听觉质感。尤其当一段合成语音出现在安静的夜晚阅读场…
Unity游戏自动翻译插件完全指南:轻松实现多语言游戏体验
Unity游戏自动翻译插件完全指南:轻松实现多语言游戏体验 【免费下载链接】XUnity.AutoTranslator 项目地址: https://gitcode.com/gh_mirrors/xu/XUnity.AutoTranslator 在当今全球化的游戏市场中,Unity游戏翻译已成为玩家突破语言障碍的关键技术…
GPT-SoVITS虚拟偶像配音实战:打造专属声线IP
GPT-SoVITS虚拟偶像配音实战:打造专属声线IP 在虚拟主播直播间里,一个声音甜美、语调自然的AI助手正与观众实时互动;在有声书平台,一段由用户自定义音色朗读的小说片段悄然上线;而在某部独立动画制作现场,主…