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

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

相关推荐
Younglina6 分钟前
一个纯前端的网站集合管理工具
前端·vue.js·chrome
木头程序员7 分钟前
前端(包含HTML/JavaScript/DOM/BOM/jQuery)基础-暴力复习篇
开发语言·前端·javascript·ecmascript·es6·jquery·html5
卖火箭的小男孩8 分钟前
# Flutter Provider 状态管理完全指南
前端
小雨青年9 分钟前
鸿蒙 HarmonyOS 6|ArkUI(01):从框架认知到项目骨架
前端
Null15511 分钟前
浏览器唤起本地桌面应用(基础版)
前端·浏览器
哈__15 分钟前
React Native 鸿蒙跨平台开发:PixelRatio 实现鸿蒙端图片的高清显示
javascript·react native·react.js
Data_agent18 分钟前
Cocbuy 模式淘宝 / 1688 代购系统(欧美市场)搭建指南
开发语言·python
wszy180925 分钟前
外部链接跳转:从 App 打开浏览器的正确姿势
java·javascript·react native·react.js·harmonyos
pas13626 分钟前
31-mini-vue 更新element的children
前端·javascript·vue.js
lsx20240627 分钟前
《Foundation 下拉菜单》
开发语言