1 |
- "use strict";var _useNativeAPI=require("../helpers/hooks/useNativeAPI"),_useCanvasAPI=require("../helpers/hooks/useCanvasAPI"),randomNum=function(e,t){return Math.floor(Math.random()*(t-e)+e)},randomColor=function(e,t){var a=randomNum(e,t),r=randomNum(e,t),e=randomNum(e,t);return"rgb(".concat(a,", ").concat(r,", ").concat(e,")")},render=function(e){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},a=t.str,r=t.num,n=t.width,o=t.height,i=t.bgColor,u=t.fontColor,l=t.hasPoint,t=t.hasLine,s=(0,_useNativeAPI.getSystemInfoSync)(["window"]).pixelRatio,m="";e.textBaseline="bottom",e.fillStyle=i||randomColor(180,240),e.scale(s,s),e.fillRect(0,0,n,o);for(var v=0;v<r;v++){var h=(n-10)/r*v+10,d=randomNum(o/2,o),c=randomNum(-45,45),f=a[randomNum(0,a.length)],g=randomNum(16,40),C=parseInt(o/2);m+=f,e.fillStyle=u||randomColor(10,100),e.font="normal normal normal ".concat(C<g?C:g,"px sans-serif"),e.translate(h,d),e.rotate(c*Math.PI/180),e.fillText(f,0,0),e.rotate(-c*Math.PI/180),e.translate(-h,-d)}if(t)for(var N=0;N<r;N++)e.strokeStyle=randomColor(90,180),e.beginPath(),e.moveTo(randomNum(0,n),randomNum(0,o)),e.lineTo(randomNum(0,n),randomNum(0,o)),e.stroke();if(l)for(var p=0;p<10*r;p++)e.fillStyle=randomColor(0,255),e.beginPath(),e.arc(randomNum(0,n),randomNum(0,o),1,0,2*Math.PI),e.fill();return m};Component({properties:{str:{type:String,value:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"},num:{type:Number,value:6},width:{type:Number,value:120},height:{type:Number,value:40},bgColor:{type:String,value:""},fontColor:{type:String,value:""},hasPoint:{type:Boolean,value:!0},hasLine:{type:Boolean,value:!0},canvasId:{type:String,value:"wux-vcode"}},methods:{createCanvasContext:function(o){var a=this,i=o.width,u=o.height,e=o.canvasId,t=Promise.resolve();return t=(t=t.then(function(){return(0,_useCanvasAPI.getCanvasRef)(e,a).then(function(e){var t=e.getContext("2d"),a=(0,_useNativeAPI.getSystemInfoSync)(["window"]).pixelRatio,r=u*a,n=(e.width=i*a,e.height=r,render(t,o));return(0,_useCanvasAPI.toDataURL)({width:i,height:u},e).then(function(e){return t.restore(),{value:n,base64Url:e}})})})).then(function(e){var t=e.value,e=e.base64Url;e=(t={value:t,base64Url:e}).value,t=t.base64Url,a.triggerEvent("change",{value:e,base64Url:t})},function(e){a.triggerEvent("error",e)})},draw:function(){this.createCanvasContext(this.data)}},ready:function(){this.createCanvasContext(this.data)}});
|