You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 lines
2.3 KiB
1 lines
2.3 KiB
"use strict";var _canvasPolyfill=require("../helpers/canvasPolyfill"),randomNum=function(t,e){return Math.floor(Math.random()*(e-t)+t)},randomColor=function(t,e){var a=randomNum(t,e),n=randomNum(t,e),r=randomNum(t,e);return"rgb(".concat(a,", ").concat(n,", ").concat(r,")")},render=function(t,e){var a=1<arguments.length&&void 0!==e?e:{},n=a.str,r=a.num,o=a.width,l=a.height,i=a.bgColor,u=a.fontColor,m=a.hasPoint,s=a.hasLine,v=wx.getSystemInfoSync().pixelRatio,h="";t.textBaseline="bottom",t.fillStyle=i||randomColor(180,240),t.scale(v,v),t.fillRect(0,0,o,l);for(var c=0;c<r;c++){var d=(o-10)/r*c+10,f=randomNum(l/2,l),g=randomNum(-45,45),y=n[randomNum(0,n.length)],C=randomNum(16,40),p=parseInt(l/2);h+=y,t.fillStyle=u||randomColor(10,100),t.font="normal normal normal ".concat(p<C?p:C,"px sans-serif"),t.translate(d,f),t.rotate(g*Math.PI/180),t.fillText(y,0,0),t.rotate(-g*Math.PI/180),t.translate(-d,-f)}if(s)for(var N=0;N<r;N++)t.strokeStyle=randomColor(90,180),t.beginPath(),t.moveTo(randomNum(0,o),randomNum(0,l)),t.lineTo(randomNum(0,o),randomNum(0,l)),t.stroke();if(m)for(var b=0;b<10*r;b++)t.fillStyle=randomColor(0,255),t.beginPath(),t.arc(randomNum(0,o),randomNum(0,l),1,0,2*Math.PI),t.fill();return h};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(l){var n=this,i=l.width,u=l.height,t=l.canvasId,e=Promise.resolve();return e=(e=e.then(function(){return(0,_canvasPolyfill.getCanvas)({canvasId:t},n).then(function(t){var e=t.getContext("2d"),a=wx.getSystemInfoSync().pixelRatio,n=i*a,r=u*a;t.width=n,t.height=r;var o=render(e,l);return(0,_canvasPolyfill.toDataURL)({width:i,height:u},t).then(function(t){return e.restore(),{value:o,base64Url:t}})})})).then(function(t){!function(t){var e=t.value,a=t.base64Url;n.triggerEvent("change",{value:e,base64Url:a})}({value:t.value,base64Url:t.base64Url})},function(t){n.triggerEvent("error",t)})},draw:function(){this.createCanvasContext(this.data)}},ready:function(){this.createCanvasContext(this.data)}});
|