forked from bobboteck/JoyStick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoy.min.js
1 lines (1 loc) · 3.78 KB
/
joy.min.js
1
let StickStatus={x:0,y:0,cardinalDirection:"C",axesX:0,axesY:1};var JoyStick=function(t,e,i){var n=void 0===(e=e||{}).title?"joystick":e.title,o=void 0===e.width?0:e.width,a=void 0===e.height?0:e.height,r=void 0!==e.limitX&&e.limitX,c=void 0!==e.limitY&&e.limitY,h=void 0===e.internalFillColor?"#00AA00":e.internalFillColor,s=void 0===e.internalLineWidth?2:e.internalLineWidth,d=void 0===e.internalStrokeColor?"#003300":e.internalStrokeColor,u=void 0===e.externalLineWidth?2:e.externalLineWidth,l=void 0===e.externalStrokeColor?"#008000":e.externalStrokeColor,f=void 0===e.autoReturnToCenter||e.autoReturnToCenter,S=void 0!==e.limitToCircle&&e.limitToCircle;StickStatus.axesX=void 0===e.axesX?0:e.axesX,StickStatus.axesY=void 0===e.axesY?1:e.axesY,i=i||function(t){};var v=document.getElementById(t);v.style.touchAction="none";var g=document.createElement("canvas");g.id=n,0===o&&(o=v.clientWidth),0===a&&(a=v.clientHeight),g.width=o,g.height=a,v.appendChild(g);var k=g.getContext("2d"),m=0,p=2*Math.PI,C=(g.width-(g.width/2+10))/2,w=C+5,P=C+30,T=g.width/2,L=g.height/2,y=g.width/10,E=-1*y,Y=g.height/10,M=-1*Y,W=T,X=L;function G(){k.beginPath(),0==r&&0==c?k.arc(T,L,P,0,p,!1):1==r&&0==c?(W=T,k.arc(T,L+P-C,C,0,Math.PI,!1),k.lineTo(L-C,T-P+C),k.arc(T,L-P+C,C,Math.PI,0,!1),k.lineTo(L+C,T+P-C)):0==r&&1==c?(X=L,k.arc(T-P+C,L,C,Math.PI/2,-Math.PI/2,!1),k.lineTo(T+P-C,L-C),k.arc(T+P-C,L,C,-Math.PI/2,Math.PI/2,!1),k.lineTo(T-P+C,L+C)):(X=L,W=T,k.arc(T,L,C,0,p,!1)),k.lineWidth=u,k.strokeStyle=l,k.stroke()}function I(){if(k.beginPath(),S){var t=Math.sqrt(Math.pow(1.12*C,2)/(Math.pow(W-T,2)+Math.pow(X-L,2)));t<1&&(W=T+(W-T)*t,X=L+(X-L)*t)}else W<C&&(W=w),W+C>g.width&&(W=g.width-w),X<C&&(X=w),X+C>g.height&&(X=g.height-w);k.arc(W,X,C,0,p,!1);var e=k.createRadialGradient(T,L,5,T,L,200);e.addColorStop(0,h),e.addColorStop(1,d),k.fillStyle=e,k.fill(),k.lineWidth=s,k.strokeStyle=d,k.stroke()}function R(){if(StickStatus.x=(W-T)/w,StickStatus.y=(X-L)/w*-1,StickStatus.x>1&&(StickStatus.x=1),StickStatus.x<-1&&(StickStatus.x=-1),StickStatus.y>1&&(StickStatus.y=1),StickStatus.y<-1&&(StickStatus.y=-1),StickStatus.cardinalDirection=A(),i instanceof Function)i(StickStatus);else{if(!(i.callback instanceof Function))return StickStatus;i.callback(StickStatus,i)}}"ontouchstart"in document.documentElement?(g.addEventListener("touchstart",(function(t){m=1,D=t.targetTouches[0].identifier}),!1),document.addEventListener("touchmove",(function(t){1===m&&t.targetTouches[0].target===g&&(W=t.targetTouches[0].pageX,X=t.targetTouches[0].pageY,"BODY"===g.offsetParent.tagName.toUpperCase()?(W-=g.offsetLeft,X-=g.offsetTop):(W-=g.offsetParent.offsetLeft,X-=g.offsetParent.offsetTop),k.clearRect(0,0,g.width,g.height),G(),I(),R())}),!1),document.addEventListener("touchend",b,!1),v.addEventListener("touchleave",b,!1)):(g.addEventListener("mousedown",(function(t){m=1}),!1),document.addEventListener("mousemove",(function(t){1===m&&(W=t.pageX,X=t.pageY,"BODY"===g.offsetParent.tagName.toUpperCase()?(W-=g.offsetLeft,X-=g.offsetTop):(W-=g.offsetParent.offsetLeft,X-=g.offsetParent.offsetTop),k.clearRect(0,0,g.width,g.height),G(),I(),R())}),!1),document.addEventListener("mouseup",F,!1),v.addEventListener("mouseleave",F,!1)),G(),I();let D=null;function b(t){t.changedTouches[0].identifier===D&&(m=0,f&&(W=T,X=L),k.clearRect(0,0,g.width,g.height),G(),I(),R())}function F(t){m=0,f&&(W=T,X=L),k.clearRect(0,0,g.width,g.height),G(),I(),R()}function A(){let t="",e=W-T,i=X-L;return i>=M&&i<=Y&&(t="C"),i<M&&(t="N"),i>Y&&(t="S"),e<E&&("C"===t?t="W":t+="W"),e>y&&("C"===t?t="E":t+="E"),t}this.GetWidth=function(){return g.width},this.GetHeight=function(){return g.height},this.GetPosX=function(){return W},this.GetPosY=function(){return X},this.GetX=function(){return x=(W-T)/w,x>1?1:x<-1?-1:x},this.GetY=function(){var t=(X-L)/w*-1;return t>1?1:t<-1?-1:t},this.GetDir=function(){return A()}};