用Canvas画的验证码,效果图如下
1.验证码的JS代码,保存到一个名称是validatedCode.js的文件内,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | ( function (window,document){ function ValidatedCode(options){ this .options = { id: "" , //容器的ID canvasId: "verifyCanvas" , //CANVAS的ID width:100, height:30, type: "blend" , background: "" , code: "" , interferingLine: true , interferingPoint: true } if (Object.prototype.toString.call(options)== "[object Object]" ) { //判断传入的参数类型 //如果传入的是对象则修改默认值 for (field in options) { this .options[field]=options[field]; } } else { this .options.id = options; } this .options.numberArray = "1,2,3,4,5,6,7,8,9,0" .split( "," ); this .options.letterArray = "a,b,c,d,e,f,g,h,i,j,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,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z" .split( "," ); this .init(); this .refresh(); } ValidatedCode.prototype = { varsion: "1.0.0" , //版本号 init: function (){ var container = document.getElementById( this .options.id); var canvas = document.createElement( "canvas" ); canvas.id = this .options.canvasId; canvas.width = this .options.width; canvas.height = this .options.height; canvas.style.cursor= "pointer" ; canvas.innerHTML= "您的浏览器不支持CANVAS" ; canvas.style.boxShadow= "0 0 10px 2px rgba(0,0,0,.36)" ; container.appendChild(canvas); var parent = this ; canvas.onclick= function (){ parent.refresh(); } }, //生成验证码 refresh: function (){ this .options.code = "" ; var canvas = document.getElementById( this .options.canvasId); if (canvas.getContext) { var ctx = canvas.getContext( "2d" ); } else { return ; } ctx.textBaseline= "middle" ; if ( this .options.background!= '' ) { ctx.fillStyle= this .options.background; } else { ctx.fillStyle=randomColor(180, 240); } ctx.fillRect(0, 0, this .options.width, this .options.height); var textArray = this .options.numberArray; if ( this .options.type== "blend" ) { textArray.concat( this .options.letterArray); } else if ( this .options.type== "letter" ) { textArray = this .options.letterArray; } for ( var i=1;i<5;i++) { var code = textArray[randomNum(0, textArray.length)]; this .options.code+=code; ctx.font = randomNum( this .options.height/2, this .options.height) + 'px SimHei' ; //随机生成字体大小 ctx.fillStyle = randomColor(50, 160); //随机生成字体颜色 ctx.shadowOffsetX = randomNum(-3, 3); ctx.shadowOffsetY = randomNum(-3, 3); ctx.shadowBlur = randomNum(-3, 3); ctx.shadowColor = "rgba(0, 0, 0, 0.3)" ; var x = this .options.width / 5 * i; var y = this .options.height / 2; var deg = randomNum(-30, 30); /**设置旋转角度和坐标原点**/ ctx.translate(x, y); ctx.rotate(deg * Math.PI / 180); ctx.fillText(code, 0, 0); /**恢复旋转角度和坐标原点**/ ctx.rotate(-deg * Math.PI / 180); ctx.translate(-x, -y); } if ( this .options.interferingLine) { /**绘制干扰线**/ for ( var i = 0; i < 4; i++) { ctx.strokeStyle = randomColor(40, 180); ctx.beginPath(); ctx.moveTo(randomNum(0, this .options.width), randomNum(0, this .options.height)); ctx.lineTo(randomNum(0, this .options.width), randomNum(0, this .options.height)); ctx.stroke(); } } if ( this .options.interferingPoint) { /**绘制干扰点**/ for ( var i = 0; i < this .options.width/4; i++) { ctx.fillStyle = randomColor(0, 255); ctx.beginPath(); ctx.arc(randomNum(0, this .options.width), randomNum(0, this .options.height), 1, 0, 2 * Math.PI); ctx.fill(); } } }, /**验证验证码**/ validate: function (code){ code = code.toLowerCase(); var v_code = this .options.code.toLowerCase(); if (code == v_code){ return true ; } else { this .refresh(); return false ; } } } /**生成一个随机数**/ function randomNum(min, max) { return Math.floor(Math.random() * (max - min) + min); } /**生成一个随机色**/ function randomColor(min, max) { var r = randomNum(min, max); var g = randomNum(min, max); var b = randomNum(min, max); return "rgb(" + r + "," + g + "," + b + ")" ; } window.ValidatedCode = ValidatedCode; })(window,document); |
2.HTML代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <! doctype html> < html lang="en"> < head > < meta charset="UTF-8"> < meta name="viewport" content="width=device-width, initial-scale=1.0"> < meta http-equiv="X-UA-Compatible" content="ie=edge"> < title >Document</ title > < script type="text/javascript" src="validateCode.js"></ script > </ head > < body > < div id="validatedCode"></ div > < div > < input type="text" id="code" style="width:180px;height:25px;border-radius: 5px;border:1px solid #ccc;" /> < input type="button" value="验证" onclick="validate()" /> </ div > < script type="text/javascript"> var validatedCode = new ValidatedCode({id:"validatedCode",width:120,type:"blend",interferingLine:false,interferingPoint:false,background:"#fff"}); function validate(){ var code = document.getElementById("code").value; if(validatedCode.validate(code)) { alert("正确"); } else { alert("错误"); } } </ script > </ body > </ html > |
本验证码参考自:https://github.com/ace0109/verifyCode
自己也记录并学习
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
2015-10-10 WKWebView捕获HTML弹出的Alert和Confirm