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

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

<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绘制,并在图片中设置随机圆点、横线;

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

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

http://www.lryc.cn/news/389411.html

相关文章:

  • [开源软件] 支持链接汇总
  • 从零开始搭建spring boot多模块项目
  • Iot解决方案开发的体系结构模式和技术
  • 02.C1W1.Sentiment Analysis with Logistic Regression
  • Stable Diffusion秋叶AnimateDiff与TemporalKit插件冲突解决
  • PCL 渐进形态过滤器实现地面分割
  • 第十四届蓝桥杯省赛C++B组E题【接龙数列】题解(AC)
  • Ubuntu 20.04.4 LTS 离线安装docker 与docker-compose
  • vue3+ts 写echarts 中国地图
  • 【设计模式】【行为型模式】【责任链模式】
  • 超越所有SOTA达11%!媲美全监督方法 | UC伯克利开源UnSAM
  • 享元模式(设计模式)
  • 【机器学习】大模型训练的深入探讨——Fine-tuning技术阐述与Dify平台介绍
  • 【Linux从入门到放弃】探究进程如何退出以进程等待的前因后果
  • QT5 static_cast实现显示类型转换
  • 【ES】--Elasticsearch的翻页详解
  • 3.js - 纹理的重复、偏移、修改中心点、旋转
  • RS232隔离器的使用
  • 一切为了安全丨2024中国应急(消防)品牌巡展武汉站成功召开!
  • 【面试系列】PHP 高频面试题
  • JAVA极简图书管理系统,初识springboot后端项目
  • MySQL 重新初始化实例
  • VCS编译bug汇总
  • 【2024LLM应用-数据预处理】之如何从PDF,PPT等非结构化数据提取有效信息(结构化数据JSON)?
  • 冯雷老师:618大退货事件分析
  • JAVA基础教程DAY0-基础知识
  • 鸿蒙开发Ability Kit(程序访问控制):【安全控件概述】
  • 【信息系统项目管理师】18年~23年案例概念型知识
  • 什么是字符串常量池?如何利用它来节省内存?
  • Selenium自动化测试20条常见异常+处理方案