UNPKG

ng-captcha

Version:

A pure front-end angular captcha module.

2 lines 3.18 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/core"),require("@angular/common")):"function"==typeof define&&define.amd?define("ng-captcha",["exports","@angular/core","@angular/common"],e):e(t["ng-captcha"]={},t.ng.core,t.ng.common)}(this,function(t,e,n){"use strict";var a=function(){function t(){this.generateCode=new e.EventEmitter,this.canvas=document.createElement("canvas"),this.letters=["0","1","2","3","4","5","6","7","8","9","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"],this.code=""}return t.prototype.ngOnInit=function(){this.init(),this.refresh()},t.prototype.onClick=function(){this.refresh()},t.prototype.init=function(){this.canvas.width=this.width||100,this.canvas.height=this.height||50,this.canvas.style.cursor="pointer",this.canvas.innerHTML="您的浏览器版本不支持canvas",this.container.nativeElement.appendChild(this.canvas)},t.prototype.refresh=function(){this.code="";var t=this.canvas.getContext("2d");t.textBaseline="middle",t.fillStyle=this.randomColor(180,240),t.fillRect(0,0,this.canvas.width,this.canvas.height);for(var e=0;e<4;e++){var n=this.letters[this.randomNum(0,this.letters.length)];this.code+=n,t.font=this.randomNum(this.canvas.height/2,this.canvas.height)+"px SimHei",t.fillStyle=this.randomColor(50,160),t.shadowOffsetX=this.randomNum(-3,3),t.shadowOffsetY=this.randomNum(-3,3),t.shadowBlur=this.randomNum(-3,3),t.shadowColor="rgb(0, 0, 0, 0.3)";var a=this.canvas.width/5*e,i=this.canvas.height/2,o=this.randomNum(-30,30);t.translate(a,i),t.rotate(o*Math.PI/180),t.fillText(n,0,0),t.rotate(-o*Math.PI/180),t.translate(-a,-i)}for(e=0;e<4;e++)t.strokeStyle=this.randomColor(40,180),t.beginPath(),t.moveTo(this.randomNum(0,this.canvas.width),this.randomNum(0,this.canvas.height)),t.lineTo(this.randomNum(0,this.canvas.width),this.randomNum(0,this.canvas.height)),t.stroke();for(e=0;e<this.canvas.width/4;e++)t.fillStyle=this.randomColor(),t.beginPath(),t.arc(this.randomNum(0,this.canvas.width),this.randomNum(0,this.canvas.height),1,0,2*Math.PI),t.fill();this.generateCode.emit(this.code.toLowerCase())},t.prototype.randomNum=function(t,e){return Math.floor(Math.random()*(e-t)+t)},t.prototype.randomColor=function(t,e){return void 0===t&&(t=0),void 0===e&&(e=255),"rgb("+this.randomNum(t,e)+", "+this.randomNum(t,e)+", "+this.randomNum(t,e)+")"},t.decorators=[{type:e.Component,args:[{selector:"captcha",template:'<div class="captcha-container" #captchaContainer (click)="onClick()"></div>',styles:[".captcha-container{display:inline-block;line-height:0}"]}]}],t.ctorParameters=function(){return[]},t.propDecorators={width:[{type:e.Input}],height:[{type:e.Input}],generateCode:[{type:e.Output}],container:[{type:e.ViewChild,args:["captchaContainer"]}]},t}(),i=function(){function t(){}return t.decorators=[{type:e.NgModule,args:[{imports:[n.CommonModule],declarations:[a],exports:[a]}]}],t}();t.CaptchaModule=i,t.ɵa=a,Object.defineProperty(t,"__esModule",{value:!0})}); //# sourceMappingURL=ng-captcha.umd.min.js.map