当前位置: 首页 > article >正文

用canvas画一个验证码

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>canvas验证码</title>
	</head>
		<style type="text/css">
			body{
				text-align: center;
				background: #989997;
			}
		</style>
	<body>
		<p>点击验证码刷新:</p>
		<p><input type="text" class="topAlign" id="verify" name="verify" value="" required> <button id="btn">验证</button>
			<canvas width="100" height="30" id="verifyCanvas"></canvas></p>
		<script type="text/javascript">
			var nums = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "A", "B", "C", "D", "E", "F", "G", "H", "I", "G", "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", "g", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
			var canvas = document.getElementById("verifyCanvas");
			var context = canvas.getContext("2d");
					
			var rand;
			reset1();

			function reset1() {
				var rands = [];

				for(var i = 0; i < 4; i++){
					rands.push(nums[random(0, nums.length - 1)])
				}

				rand = rands[0] + rands[1] + rands[2] + rands[3];
				context.clearRect(0, 0, canvas.width, canvas.height);
				// 创建画布
				context.fillStyle = "#ffffff";
				context.fillRect(0, 0, canvas.width, canvas.height);
				context.beginPath();
				var i = 0;
				// 画线  
				for(i = 0; i < 4; i++) {
					drawline(canvas, context);
				}

				// 随机点
				for(i = 0; i < 50; i++) {
					drawDot(canvas, context);
				}
				
				// 验证码 
				context.font = "25px Arial";

				context.save();
				for(var i = 0; i < 4; i++){
					context.fillStyle = randomColor();
					context.fillText(rands[i], (i * 20 + 10), 20);
					context.rotate(random(-2,2)*Math.PI/180)
				}
				context.restore();

			}

			function drawline(canvas, context) {
				context.moveTo(0, random(0, canvas.height));
				context.lineTo(canvas.width, random(0, canvas.height));
				context.lineWidth = 0.5;
				context.strokeStyle = randomColor();
				context.stroke();
			}

			function drawDot(canvas, context) {
				var px = random(0, canvas.width);
				var py = random(0, canvas.height);
				context.moveTo(px, py);
				context.lineTo(px + 1, py + 1);
				context.lineWidth = 0.2;
				context.stroke();
			}

			function random(min, max) {
				return Math.floor(Math.random() * (max - min + 1) + min);
			}

			function randomColor() {
				return "rgb(" + random(0, 255) + "," + random(0, 255) + "," + random(0, 225) + ")";
			}

			var btn = document.getElementById("btn");
			var verify = document.getElementById("verify");
			var verifyCanvas = document.getElementById("verifyCanvas");
			verifyCanvas.onclick = reset1
			btn.onclick = function() {
				if(verify.value.toUpperCase() == rand.toUpperCase()) {
					alert("OK!")
				} else {
					alert("error")
				}
			}
		</script>
	</body>

</html>

http://www.kler.cn/a/318322.html

相关文章:

  • 【excel】easy excel如何导出动态列
  • Linux git-bash配置
  • 【插件】多断言 插件pytest-assume
  • Electron 项目启动外部可执行文件的几种方式
  • 【Vue】Vue3.0(十九)Vue 3.0 中一种组件间通信方式-自定义事件
  • 车载空气净化器语音芯片方案
  • 从 Oracle 集群到单节点环境(详细记录一次数据迁移过程)之一:生产环境与目标服务器详情
  • 基于物联网的火灾报警器设计与实现(论文+源码)
  • 高维数据和超高维数据
  • CX8903:电动车手机充电器降压芯片,搭配协议实现快充
  • Linux入门学习:进程概念
  • k8s前置准备:配置虚拟机网络
  • 计算机网络 --- 初识协议
  • 多人在线聊天服务器
  • P9235 [蓝桥杯 2023 省 A] 网络稳定性
  • Unity教程(十六)敌人攻击状态的实现
  • 【WebLogic】WebLogic 11g 控制台模式下的集群创建(一)
  • JetBrains系列产品无限重置免费试用方法
  • ATTCK实战系列-Vulnstack靶场内网域渗透(二)
  • Spring-bean的生命周期-中篇
  • 光伏开发:一分钟生成光伏项目报告
  • 大数据可视化-三元图
  • 【MySQL 04】数据类型
  • linux-安全管理-文件系统安全
  • 计算机组成原理(笔记4)
  • 八大排序——万字长文带你剖析八大排序(C语言)