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

用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 = reset1btn.onclick = function() {if(verify.value.toUpperCase() == rand.toUpperCase()) {alert("OK!")} else {alert("error")}}</script></body></html>

http://www.mrgr.cn/news/35439.html

相关文章:

  • 在ubuntu上安装ubuntu22.04并ros2 humble版本的docker容器记录
  • 【MySQL】MySQL函数之JSON_EXTRACT
  • 【机器学习】数学知识:标准差,方差,协方差,平均数,中位数,众数
  • 聊天服务器(4)CMake
  • 【3D Slicer】的小白入门使用指南四
  • 【计网不挂科】计算机网络期末考试(综合)——【选择题&填空题&判断题&简述题】完整试卷
  • 第十五章 文件上传
  • 自动化学习3:日志记录及测试报告的生成--自动化框架搭建
  • 【LinuxC高级】汇总
  • 【Linux】多线程:线程池的创建、日志类、RAII互斥锁、单例模式:饿汉方式与懒汉方式
  • 【Java】内部类【主线学习笔记】
  • 【微服务即时通讯系统】——etcd一致性键值存储系统,etcd的介绍,etcd的安装,etcd使用和功能测试
  • 【爬虫】PlayWright使用说明
  • 38.重复的子字符串
  • AI大模型行业应用:企业如何走出一条智能化蜕变之路?
  • 【字符串】介绍
  • 关于YOLOX的一些优势
  • java是干什么的
  • keepalived+lvs集群
  • 屏幕演示工具 | 水豚鼠标助手 v1.0.7
  • 828华为云征文|华为云Flexus X实例:极速搭建个人代码仓库GitLab平台
  • 6种常见位运算符+异或运算符的使用(加密、解密)
  • Liblib:简化开发的强大工具
  • LED驱动电路
  • 编译和链接
  • linux网络编程7