js制作随机四位数验证码图片

html 复制代码
<div class="lable lable2">
				<div class="l"><span>*</span>验证码</div>
				<div class="r">
					<input type="number" name="vercode"  placeholder="请输入验证码">
				</div>
				<div class="verCode_div" style="margin-left: 5px;">
					<canvas id="digitCanvas" width="120" height="40"></canvas>
				</div>
				<a href="javascript:;" class="chang_verCode_img" id="de_chang_verCodeImg"
					style="font-size: 12px;">换一张</a>
</div>

通过canvas绘制,并在图片中设置随机圆点、横线;

javascript 复制代码
var imgcode;
getimgcode();
function generateRandomNumber() {
			imgcode = Math.floor(1000 + Math.random() * 9000);
		}
		function getimgcode() {
			const canvas = document.getElementById('digitCanvas');
			const ctx = canvas.getContext('2d');
			ctx.clearRect(0, 0, canvas.width, canvas.height);
			let numToShow = imgcode; // 要显示的数字
			const fontSize = 30;
			const animationDuration = 100; // 动画持续时间(ms)
			const dotCount = 20; // 点的数量
			const lineCount = 2; // 线的数量
			// 设置字体样式
			ctx.font = `bold ${fontSize}px Arial`;
			// 获取数字的边界框,以便居中显示
			const numWidth = ctx.measureText(numToShow).width;
			const centerX = canvas.width / 2;
			const centerY = canvas.height / 2;
			// 渐显动画
			function fadeinText(text, x, y, duration) {
				return new Promise(resolve => {
					// 在开始绘制之前清空画布
					ctx.clearRect(0, 0, canvas.width, canvas.height);
					ctx.fillStyle = 'black'; // 设置文本颜色为黑色
					let startOpacity = 0;
					const step = 1 / (duration / 16.67); // 基于每帧约16.67ms计算步长
					function animate(currentTime) {
						if (startOpacity >= 1) {
							resolve();
							return;
						}
						ctx.globalAlpha = startOpacity;
						ctx.fillText(text, x, y);
						startOpacity += step;
						requestAnimationFrame(animate);
					}
					requestAnimationFrame(animate);
				});
			}
			// 绘制随机点
			function drawRandomDots(count) {
				for (let i = 0; i < count; i++) {
					const x = Math.random() * canvas.width;
					const y = Math.random() * canvas.height;
					const radius = Math.random() * 3 + 1;
					ctx.fillStyle = `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${Math.random() * 255})`;
					ctx.beginPath();
					ctx.arc(x, y, radius, 0, Math.PI * 2);
					ctx.fill();
				}
			}
			// 绘制随机线条
			function drawRandomLines(count) {
				for (let i = 0; i < count; i++) {
					ctx.beginPath();
					ctx.moveTo(Math.random() * canvas.width, Math.random() * canvas.height);
					ctx.lineTo(Math.random() * canvas.width, Math.random() * canvas.height);
					ctx.strokeStyle = `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${Math.random() * 255})`;
					ctx.lineWidth = Math.random() * 3 + 1;
					ctx.stroke();
				}
			}
			// 主程序
			async function startAnimation() {
				await fadeinText(numToShow, centerX - numWidth / 2, centerY + fontSize / 2, animationDuration);
				drawRandomDots(dotCount);
				drawRandomLines(lineCount);
			}
			// 启动动画
			startAnimation();
		}

手动获取随机数,可以在输入时自己进行校验;

相关推荐
Dxy12393102163 分钟前
Python ExcelWriter详解:从基础到高级的完整指南
开发语言·python
chao_7896 分钟前
frame 与新窗口切换操作【selenium 】
前端·javascript·css·selenium·测试工具·自动化·html
天蓝色的鱼鱼19 分钟前
从零实现浏览器摄像头控制与视频录制:基于原生 JavaScript 的完整指南
前端·javascript
三原42 分钟前
7000块帮朋友做了2个小程序加一个后台管理系统,值不值?
前端·vue.js·微信小程序
popoxf1 小时前
在新版本的微信开发者工具中使用npm包
前端·npm·node.js
爱编程的喵1 小时前
React Router Dom 初步:从传统路由到现代前端导航
前端·react.js
阳火锅2 小时前
Vue 开发者的外挂工具:配置一个 JSON,自动造出一整套页面!
javascript·vue.js·面试
每天吃饭的羊2 小时前
react中为啥使用剪头函数
前端·javascript·react.js
源代码•宸2 小时前
C++高频知识点(十三)
开发语言·c++·经验分享·面经
wa的一声哭了2 小时前
python基础知识pip配置pip.conf文件
java·服务器·开发语言·python·pip·risc-v·os