-
Notifications
You must be signed in to change notification settings - Fork 2
/
recosigns.min.js
1 lines (1 loc) · 4.71 KB
/
recosigns.min.js
1
function Recognize(t,n,e){this.listenDOM=null,this.signs=[],this.input={start:null,end:null,dots:[]},t&&this.setSingns(t),n&&this.listen(n),e&&this.setAction(e)}function tchstart(t){var n=this.listenRecognize;n.input.dots=[],n.input.start={x:t.changedTouches[0].clientX,y:t.changedTouches[0].clientY},n.input.dots.push([t.changedTouches[0].clientX,t.changedTouches[0].clientY])}function tchend(t){var n=this.listenRecognize;n.input.dots.push([t.changedTouches[0].clientX,t.changedTouches[0].clientY]);for(var e=[null,null,null,null],i=0;i<n.input.dots.length;i++)(null==e[0]||n.input.dots[i][0]<e[0])&&(e[0]=n.input.dots[i][0]),(null==e[1]||n.input.dots[i][1]<e[1])&&(e[1]=n.input.dots[i][1]),(null==e[2]||n.input.dots[i][0]>e[2])&&(e[2]=n.input.dots[i][0]),(null==e[3]||n.input.dots[i][1]>e[3])&&(e[3]=n.input.dots[i][1]);n.input.start.x=(n.input.start.x-e[0])/(e[2]-e[0]),n.input.start.y=(n.input.start.y-e[1])/(e[3]-e[1]),n.input.end={x:0,y:0},n.input.end.x=(t.changedTouches[0].clientX-e[0])/(e[2]-e[0]),n.input.end.y=(t.changedTouches[0].clientY-e[1])/(e[3]-e[1]);for(i=0;i<n.input.dots.length;i++)n.input.dots[i][0]=(n.input.dots[i][0]-e[0])/(e[2]-e[0]),n.input.dots[i][1]=(n.input.dots[i][1]-e[1])/(e[3]-e[1]);var s=[];for(i=0;i<n.signs.length;i++)if(n.input.start.x>=n.signs[i].start.x-.4&&n.input.start.x<=n.signs[i].start.x+.4&&n.input.start.y>=n.signs[i].start.y-.4&&n.input.start.y<=n.signs[i].start.y+.4&&n.input.end.x>=n.signs[i].end.x-.4&&n.input.end.x<=n.signs[i].end.x+.4&&n.input.end.y>=n.signs[i].end.y-.4&&n.input.end.y<=n.signs[i].end.y+.4){for(var l=0,u=0;u<n.input.dots.length;u++)for(var o=0;o<n.signs[i].dots.length;o++)if(n.input.dots[u][0]>=n.signs[i].dots[o][0]-.01&&n.input.dots[u][0]<=n.signs[i].dots[o][0]+.01&&n.input.dots[u][1]>=n.signs[i].dots[o][1]-.01&&n.input.dots[u][1]<=n.signs[i].dots[o][1]+.01){"#059900",l++;break}l/=n.input.dots.length,s.push([i,l]);break}if(s[0]){var h=null,d=null;for(i=0;i<s.length;i++)(null==h||s[i][1]>h)&&(h=s[i][1],d=s[i][0]);n.action&&n.action(d)}}function tchmove(t){this.listenRecognize.input.dots.push([t.changedTouches[0].clientX,t.changedTouches[0].clientY])}function drawtest(t,n){var e=document.getElementById("testcanvas"),i=e.getContext("2d");i.beginPath(),i.strokeStyle=n,i.rect((t.start.x-t.start.width/2)*e.clientWidth,(t.start.y-t.start.height/2)*e.clientHeight,t.start.width*e.clientWidth,t.start.height*e.clientHeight),i.stroke(),i.beginPath(),i.strokeStyle=n,i.rect((t.end.x-t.end.width/2)*e.clientWidth,(t.end.y-t.end.height/2)*e.clientHeight,t.end.width*e.clientWidth,t.end.height*e.clientHeight),i.stroke();for(var s=0;s<t.dots.length;s++)i.beginPath(),i.strokeStyle=n,i.rect(t.dots[s][0]*e.clientWidth-1,t.dots[s][1]*e.clientHeight-1,2,2),i.stroke()}Recognize.prototype.setAction=function(t){this.action=t},Recognize.prototype.clearSingns=function(){this.signs=[]},Recognize.prototype.setSingns=function(t,n,e){if("string"==typeof t&&(t=[t]),e||(e=0),e>=t.length)return n&&n(),!0;var i=document.createElement("canvas"),s=i.getContext("2d"),l=new Image,u=this;l.onload=function(o){var h=l.naturalWidth,d=l.naturalHeight;i.setAttribute("width",h+"px"),i.setAttribute("height",d+"px"),s.drawImage(l,0,0,h,d);for(var c=[],r=[null,null,null,null],a=[null,null,null,null],g=[null,null,null,null],p=0;p<199;p++)for(var v=0;v<199;v++){var y=v*(h/200)+h/200/2,f=p*(d/200)+d/200/2,x=s.getImageData(y,f,1,1).data;(x[0]<=20&&x[1]<=20&&x[2]<=20||x[0]>=230&&x[1]<=20&&x[2]<=20||x[0]<=20&&x[1]<=20&&x[2]>=230)&&((null==r[0]||y<r[0])&&(r[0]=y),(null==r[1]||f<r[1])&&(r[1]=f),(null==r[2]||y>r[2])&&(r[2]=y),(null==r[3]||f>r[3])&&(r[3]=f)),x[0]>=230&&x[1]<=20&&x[2]<=20&&((null==a[0]||y<a[0])&&(a[0]=y),(null==a[1]||f<a[1])&&(a[1]=f),(null==a[2]||y>a[2])&&(a[2]=y),(null==a[3]||f>a[3])&&(a[3]=f)),x[0]<=20&&x[1]<=20&&x[2]>=230&&((null==g[0]||y<g[0])&&(g[0]=y),(null==g[1]||f<g[1])&&(g[1]=f),(null==g[2]||y>g[2])&&(g[2]=y),(null==g[3]||f>g[3])&&(g[3]=f)),x[0]<=20&&x[1]<=20&&x[2]<=20&&c.push([y,f])}for(p=0;p<c.length;p++)c[p]=[(c[p][0]-r[0])/(r[2]-r[0]),(c[p][1]-r[1])/(r[3]-r[1])];u.signs.push({start:{x:((a[0]+a[2])/2-r[0])/(r[2]-r[0]),y:((a[1]+a[3])/2-r[1])/(r[3]-r[1]),width:(a[2]-a[0])/(r[2]-r[0]),height:(a[3]-a[1])/(r[3]-r[1])},end:{x:((g[0]+g[2])/2-r[0])/(r[2]-r[0]),y:((g[1]+g[3])/2-r[1])/(r[3]-r[1]),width:(g[2]-g[0])/(r[2]-r[0]),height:(g[3]-g[1])/(r[3]-r[1])},dots:c}),u.setSingns(t,n,++e)},l.src=t[e]},Recognize.prototype.stopListen=function(){var t=this.listenDOM;t.listenRecognize=null,t.removeEventListener("touchstart",tchstart),t.removeEventListener("touchend",tchend),t.removeEventListener("touchmove",tchmove),this.listenDOM=null},Recognize.prototype.listen=function(t){t.listenRecognize=this,this.listenDOM=t,t.addEventListener("touchstart",tchstart),t.addEventListener("touchend",tchend),t.addEventListener("touchmove",tchmove)};