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();
		}

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

相关推荐
星秀日3 分钟前
框架--SpringMVC
java·开发语言·servlet
携欢7 分钟前
PortSwigger靶场之将 XSS 存储到onclick带有尖括号和双引号 HTML 编码以及单引号和反斜杠转义的事件中通关秘籍
前端·html·xss
三小河14 分钟前
工作中的Ai工具汇总
前端
mapbar_front33 分钟前
react项目开发—关于代码架构/规范探讨
前端·react.js
二木一夕39 分钟前
Vue 3 的组合式 API和传统选项式 API区别(vue2转vue3,两者差异)
前端
LuckySusu41 分钟前
【vue篇】Vue 项目中的静态资源管理:assets vs static 终极指南
前端·vue.js
LuckySusu42 分钟前
【vue篇】Vue.delete vs delete:数组删除的“陷阱”与正确姿势
前端·vue.js
LuckySusu44 分钟前
【vue篇】Vue 模板编译原理:从 Template 到 DOM 的翻译官
前端·vue.js
小菜摸鱼1 小时前
Node.js + vue3 大文件-切片上传全流程(视频文件)
前端·node.js
LuckySusu1 小时前
【vue篇】Vue 2 响应式“盲区”破解:如何监听对象/数组属性变化
前端·vue.js