/* * @Author: liyi * @LastEditors: liyi * @Date: 2021-03-31 14:57:30 * @LastEditTime: 2021-03-31 15:02:58 * @Description: 闅忔満楠岃瘉鐮 * @FilePath: /undefined/Users/yili/Desktop/gVerify.js */ !(function (window, document) { function GVerify(options, canvasId) { //鍒涘缓涓€涓浘褰㈤獙璇佺爜瀵硅薄锛屾帴鏀秓ptions瀵硅薄涓哄弬鏁 this.options = { //榛樿options鍙傛暟鍊 id: "", //瀹瑰櫒Id canvasId, //canvas鐨処D width: "100", //榛樿canvas瀹藉害 height: "30", //榛樿canvas楂樺害 type: "blend", //鍥惧舰楠岃瘉鐮侀粯璁ょ被鍨媌lend:鏁板瓧瀛楁瘝娣峰悎绫诲瀷銆乶umber:绾暟瀛椼€乴etter:绾瓧姣 code: "" } if (Object.prototype.toString.call(options) == "[object Object]") {//鍒ゆ柇浼犲叆鍙傛暟绫诲瀷 for (var i in options) { //鏍规嵁浼犲叆鐨勫弬鏁帮紝淇敼榛樿鍙傛暟鍊 this.options[i] = options[i]; } } else {//浼犲叆鍗曚釜瀵硅薄灏辨槸id this.options.id = options; } this.options.numArr = "0,1,2,3,4,5,6,7,8,9".split(",");//鏁板瓧 this.options.letterArr = getAllLetter();//鐢熸垚瀛楁瘝鏁扮粍 this._init();//鍒濆鍖 this.refresh();//鐢熸垚楠岃瘉鐮 } GVerify.prototype = { /**鐗堟湰鍙?*/ version: '1.0.0', /**鍒濆鍖栨柟娉?*/ _init: function () { var con = document.getElementById(this.options.id);//鑾峰緱楠岃瘉鐮佺殑DIV var canvas = document.getElementById(this.options.canvasId);//鑾峰緱鐢诲竷 IE涓嶈兘鏀寔canvas锛屽彲浠ュ鍔爀xcanvas.js鎻掍欢锛屼絾鏄繕鏄笉鏀寔createelement()鐨勫舰寮 this.options.width = con.offsetWidth > 0 ? con.offsetWidth : "100";//濡傛灉鏈夊搴﹀氨浣跨敤鑷繁鐨勶紝娌℃湁灏遍粯璁?00 this.options.height = con.offsetHeight > 0 ? con.offsetHeight : "30";//濡傛灉鏈夐暱搴﹀氨浣跨敤鑷繁鐨勶紝娌℃湁灏遍粯璁?0 canvas.id = this.options.canvasId; canvas.width = this.options.width; canvas.height = this.options.height; canvas.style.cursor = "pointer"; canvas.innerHTML = "鎮ㄧ殑娴忚鍣ㄧ増鏈笉鏀寔canvas"; con.appendChild(canvas); // canvas.id = this.options.canvasId;//涓哄吋瀹笽E鎶婅繖浜涘幓鎺夛紝杩橀渶瑕佸湪HTML涓啓鍏anvas鏍囩 // canvas.width = this.options.width; // canvas.height = this.options.height; // canvas.style.cursor = "pointer"; // canvas.innerHTML = "鎮ㄧ殑娴忚鍣ㄧ増鏈笉鏀寔canvas"; // con.appendChild(canvas); var parent = this;//鎶妕his璧嬪€紁arent 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"; ctx.fillStyle = randomColor(180, 240); ctx.fillRect(0, 0, this.options.width, this.options.height);//缁樺埗鐭╁舰 /* x:鐭╁舰璧风偣妯潗鏍囷紙鍧愭爣鍘熺偣涓篶anvas鐨勫乏涓婅锛屽綋鐒剁‘鍒囩殑鏉ヨ鏄師濮嬪師鐐癸紝鍚庨潰鍐欏埌鍙樺舰鐨勬椂鍊欎綘灏辨噦浜嗭紝鐜板湪鏆傛椂涓嶇敤鍏崇郴锛 y:鐭╁舰璧风偣绾靛潗鏍 width:鐭╁舰闀垮害 height:鐭╁舰楂樺害*/ if (this.options.type == "blend") { //鍒ゆ柇楠岃瘉鐮佺被鍨 blend:鏁板瓧瀛楁瘝娣峰悎绫诲瀷銆乶umber:绾暟瀛椼€乴etter:绾瓧姣 var txtArr = this.options.numArr.concat(this.options.letterArr); } else if (this.options.type == "number") { var txtArr = this.options.numArr; } else { var txtArr = this.options.letterArr; } for (var i = 1; i <= 4; i++) { var txt = txtArr[randomNum(0, txtArr.length)];//鍙栧緱涓€涓瓧绗 if(!txt) txt = 'a'; this.options.code += txt;//杩炴帴楠岃瘉鐮 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); /**璁剧疆鏃嬭浆瑙掑害鍜屽潗鏍囧師鐐 * * 骞崇Щcontext.translate(x,y) * x:鍧愭爣鍘熺偣鍚憍杞存柟鍚戝钩绉粁 * y:鍧愭爣鍘熺偣鍚憏杞存柟鍚戝钩绉粂 * * **/ ctx.translate(x, y); ctx.rotate(deg * Math.PI / 180);//鏃嬭浆context.rotate(angle) ctx.fillText(txt, 0, 0);//context.fillText(text,x,y) /**鎭㈠鏃嬭浆瑙掑害鍜屽潗鏍囧師鐐?*/ ctx.rotate(-deg * Math.PI / 180); ctx.translate(-x, -y); } /**缁樺埗骞叉壈绾?*/ for (var i = 0; i < 4; i++) { ctx.strokeStyle = randomColor(40, 180);//闅忔満棰滆壊 ctx.beginPath();//璺緞 context.beginPath() ctx.moveTo(randomNum(0, this.options.width), randomNum(0, this.options.height));//缁樺埗绾挎 context.moveTo(x,y) context.lineTo(x,y) ctx.lineTo(randomNum(0, this.options.width), randomNum(0, this.options.height)); ctx.stroke(); } /**缁樺埗骞叉壈鐐?*/ 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);// 鍦嗗姬context.arc(x, y, radius, starAngle,endAngle, anticlockwise) ctx.fill(); } }, /**楠岃瘉楠岃瘉鐮?*/ validate: function (code) { var code = code.toLowerCase(); var v_code = this.options.code.toLowerCase(); //console.log(v_code); if (code == v_code) { return true; } else { this.refresh(); return false; } } } /**鐢熸垚瀛楁瘝鏁扮粍**/ function getAllLetter() { var letterStr = "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"; return letterStr.split(","); } /**鐢熸垚涓€涓殢鏈烘暟**/ function randomNum(min, max) { //return Math.floor(Math.random() * (max - min) + min); if(max >= 256) max = 255; var byteArray = new Uint8Array(1); window.crypto.getRandomValues(byteArray); var range = max - min + 1; var max_range = 256; if (byteArray[0] >= Math.floor(max_range / range) * range) return randomNum(min, max); var retNum = min + (byteArray[0] % range); if(retNum > max) { retNum = max - 1; } return retNum; } /**鐢熸垚涓€涓殢鏈鸿壊**/ 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.GVerify = GVerify;//璁剧疆涓簑indow瀵硅薄 })(window, document);