diff --git a/joy.html b/joy.html index 5f24cc8..c40f9dd 100644 --- a/joy.html +++ b/joy.html @@ -73,7 +73,12 @@ border: 1px solid #0000FF; } - + diff --git a/joy.js b/joy.js index 09608c4..dbd1bcd 100644 --- a/joy.js +++ b/joy.js @@ -1,48 +1,5 @@ /* - * Name : joy.js - * @author : Roberto D'Amico (Bobboteck) - * Last modified : 09.06.2020 - * Revision : 1.1.6 - * - * Modification History: - * Date Version Modified By Description - * 2021-12-21 2.0.0 Roberto D'Amico New version of the project that integrates the callback functions, while - * maintaining compatibility with previous versions. Fixed Issue #27 too, - * thanks to @artisticfox8 for the suggestion. - * 2020-06-09 1.1.6 Roberto D'Amico Fixed Issue #10 and #11 - * 2020-04-20 1.1.5 Roberto D'Amico Correct: Two sticks in a row, thanks to @liamw9534 for the suggestion - * 2020-04-03 Roberto D'Amico Correct: InternalRadius when change the size of canvas, thanks to - * @vanslipon for the suggestion - * 2020-01-07 1.1.4 Roberto D'Amico Close #6 by implementing a new parameter to set the functionality of - * auto-return to 0 position - * 2019-11-18 1.1.3 Roberto D'Amico Close #5 correct indication of East direction - * 2019-11-12 1.1.2 Roberto D'Amico Removed Fix #4 incorrectly introduced and restored operation with touch - * devices - * 2019-11-12 1.1.1 Roberto D'Amico Fixed Issue #4 - Now JoyStick work in any position in the page, not only - * at 0,0 - * - * The MIT License (MIT) - * - * This file is part of the JoyStick Project (https://github.com/bobboteck/JoyStick). - * Copyright (c) 2015 Roberto D'Amico (Bobboteck). - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. + * @author Roberto D'Amico (Bobboteck) https://github.com/bobboteck/JoyStick/blob/master/joy.js */ let StickStatus = @@ -115,19 +72,14 @@ var JoyStick = (function(container, parameters, callback) var movedX=centerX; var movedY=centerY; - // Check if the device support the touch or not - if("ontouchstart" in document.documentElement) - { - canvas.addEventListener("touchstart", onTouchStart, false); - document.addEventListener("touchmove", onTouchMove, false); - document.addEventListener("touchend", onTouchEnd, false); - } - else - { - canvas.addEventListener("mousedown", onMouseDown, false); - document.addEventListener("mousemove", onMouseMove, false); - document.addEventListener("mouseup", onMouseUp, false); - } + canvas.addEventListener("touchstart", onTouchStart, false); + document.addEventListener("touchmove", onTouchMove, false); + document.addEventListener("touchend", onTouchEnd, false); + + canvas.addEventListener("mousedown", onMouseDown, false); + document.addEventListener("mousemove", onMouseMove, false); + document.addEventListener("mouseup", onMouseUp, false); + // Draw the object drawExternal(); drawInternal(); diff --git a/joy.min.js b/joy.min.js index 98c4e7f..21ec4ec 100644 --- a/joy.min.js +++ b/joy.min.js @@ -1,47 +1,4 @@ /* - * Name : joy.js - * @author : Roberto D'Amico (Bobboteck) - * Last modified : 09.06.2020 - * Revision : 1.1.6 - * - * Modification History: - * Date Version Modified By Description - * 2021-12-21 2.0.0 Roberto D'Amico New version of the project that integrates the callback functions, while - * maintaining compatibility with previous versions. Fixed Issue #27 too, - * thanks to @artisticfox8 for the suggestion. - * 2020-06-09 1.1.6 Roberto D'Amico Fixed Issue #10 and #11 - * 2020-04-20 1.1.5 Roberto D'Amico Correct: Two sticks in a row, thanks to @liamw9534 for the suggestion - * 2020-04-03 Roberto D'Amico Correct: InternalRadius when change the size of canvas, thanks to - * @vanslipon for the suggestion - * 2020-01-07 1.1.4 Roberto D'Amico Close #6 by implementing a new parameter to set the functionality of - * auto-return to 0 position - * 2019-11-18 1.1.3 Roberto D'Amico Close #5 correct indication of East direction - * 2019-11-12 1.1.2 Roberto D'Amico Removed Fix #4 incorrectly introduced and restored operation with touch - * devices - * 2019-11-12 1.1.1 Roberto D'Amico Fixed Issue #4 - Now JoyStick work in any position in the page, not only - * at 0,0 - * - * The MIT License (MIT) - * - * This file is part of the JoyStick Project (https://github.com/bobboteck/JoyStick). - * Copyright (c) 2015 Roberto D'Amico (Bobboteck). - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. + * @author Roberto D'Amico (Bobboteck) https://github.com/bobboteck/JoyStick/blob/master/joy.js */ -let StickStatus={xPosition:0,yPosition:0,x:0,y:0,cardinalDirection:"C"};var JoyStick=function(t,e,i){var o=void 0===(e=e||{}).title?"joystick":e.title,n=void 0===e.width?0:e.width,a=void 0===e.height?0:e.height,r=void 0===e.internalFillColor?"#00AA00":e.internalFillColor,c=void 0===e.internalLineWidth?2:e.internalLineWidth,s=void 0===e.internalStrokeColor?"#003300":e.internalStrokeColor,d=void 0===e.externalLineWidth?2:e.externalLineWidth,u=void 0===e.externalStrokeColor?"#008000":e.externalStrokeColor,h=void 0===e.autoReturnToCenter||e.autoReturnToCenter;i=i||function(t){};var S=document.getElementById(t);S.style.touchAction="none";var f=document.createElement("canvas");f.id=o,0===n&&(n=S.clientWidth),0===a&&(a=S.clientHeight),f.width=n,f.height=a,S.appendChild(f);var l=f.getContext("2d"),k=0,g=2*Math.PI,x=(f.width-(f.width/2+10))/2,v=x+5,P=x+30,m=f.width/2,C=f.height/2,p=f.width/10,y=-1*p,w=f.height/10,L=-1*w,F=m,E=C;function W(){l.beginPath(),l.arc(m,C,P,0,g,!1),l.lineWidth=d,l.strokeStyle=u,l.stroke()}function T(){l.beginPath(),Ff.width&&(F=f.width-v),Ef.height&&(E=f.height-v),l.arc(F,E,x,0,g,!1);var t=l.createRadialGradient(m,C,5,m,C,200);t.addColorStop(0,r),t.addColorStop(1,s),l.fillStyle=t,l.fill(),l.lineWidth=c,l.strokeStyle=s,l.stroke()}function D(){let t="",e=F-m,i=E-C;return i>=L&&i<=w&&(t="C"),iw&&(t="S"),ep&&("C"===t?t="E":t+="E"),t}"ontouchstart"in document.documentElement?(f.addEventListener("touchstart",function(t){k=1},!1),document.addEventListener("touchmove",function(t){1===k&&t.targetTouches[0].target===f&&(F=t.targetTouches[0].pageX,E=t.targetTouches[0].pageY,"BODY"===f.offsetParent.tagName.toUpperCase()?(F-=f.offsetLeft,E-=f.offsetTop):(F-=f.offsetParent.offsetLeft,E-=f.offsetParent.offsetTop),l.clearRect(0,0,f.width,f.height),W(),T(),StickStatus.xPosition=F,StickStatus.yPosition=E,StickStatus.x=((F-m)/v*100).toFixed(),StickStatus.y=((E-C)/v*100*-1).toFixed(),StickStatus.cardinalDirection=D(),i(StickStatus))},!1),document.addEventListener("touchend",function(t){k=0,h&&(F=m,E=C);l.clearRect(0,0,f.width,f.height),W(),T(),StickStatus.xPosition=F,StickStatus.yPosition=E,StickStatus.x=((F-m)/v*100).toFixed(),StickStatus.y=((E-C)/v*100*-1).toFixed(),StickStatus.cardinalDirection=D(),i(StickStatus)},!1)):(f.addEventListener("mousedown",function(t){k=1},!1),document.addEventListener("mousemove",function(t){1===k&&(F=t.pageX,E=t.pageY,"BODY"===f.offsetParent.tagName.toUpperCase()?(F-=f.offsetLeft,E-=f.offsetTop):(F-=f.offsetParent.offsetLeft,E-=f.offsetParent.offsetTop),l.clearRect(0,0,f.width,f.height),W(),T(),StickStatus.xPosition=F,StickStatus.yPosition=E,StickStatus.x=((F-m)/v*100).toFixed(),StickStatus.y=((E-C)/v*100*-1).toFixed(),StickStatus.cardinalDirection=D(),i(StickStatus))},!1),document.addEventListener("mouseup",function(t){k=0,h&&(F=m,E=C);l.clearRect(0,0,f.width,f.height),W(),T(),StickStatus.xPosition=F,StickStatus.yPosition=E,StickStatus.x=((F-m)/v*100).toFixed(),StickStatus.y=((E-C)/v*100*-1).toFixed(),StickStatus.cardinalDirection=D(),i(StickStatus)},!1)),W(),T(),this.GetWidth=function(){return f.width},this.GetHeight=function(){return f.height},this.GetPosX=function(){return F},this.GetPosY=function(){return E},this.GetX=function(){return((F-m)/v*100).toFixed()},this.GetY=function(){return((E-C)/v*100*-1).toFixed()},this.GetDir=function(){return D()}}; \ No newline at end of file +let StickStatus={xPosition:0,yPosition:0,x:0,y:0,cardinalDirection:"C"};var JoyStick=function(t,e,i){var o=void 0===(e=e||{}).title?"joystick":e.title,n=void 0===e.width?0:e.width,a=void 0===e.height?0:e.height,r=void 0===e.internalFillColor?"#00AA00":e.internalFillColor,c=void 0===e.internalLineWidth?2:e.internalLineWidth,s=void 0===e.internalStrokeColor?"#003300":e.internalStrokeColor,d=void 0===e.externalLineWidth?2:e.externalLineWidth,u=void 0===e.externalStrokeColor?"#008000":e.externalStrokeColor,h=void 0===e.autoReturnToCenter||e.autoReturnToCenter;i=i||function(t){};var S=document.getElementById(t);S.style.touchAction="none";var f=document.createElement("canvas");f.id=o,0===n&&(n=S.clientWidth),0===a&&(a=S.clientHeight),f.width=n,f.height=a,S.appendChild(f);var l=f.getContext("2d"),k=0,g=2*Math.PI,x=(f.width-(f.width/2+10))/2,v=x+5,P=x+30,C=f.width/2,p=f.height/2,y=f.width/10,m=-1*y,w=f.height/10,L=-1*w,F=C,T=p;function E(){l.beginPath(),l.arc(C,p,P,0,g,!1),l.lineWidth=d,l.strokeStyle=u,l.stroke()}function W(){l.beginPath(),Ff.width&&(F=f.width-v),Tf.height&&(T=f.height-v),l.arc(F,T,x,0,g,!1);var t=l.createRadialGradient(C,p,5,C,p,200);t.addColorStop(0,r),t.addColorStop(1,s),l.fillStyle=t,l.fill(),l.lineWidth=c,l.strokeStyle=s,l.stroke()}f.addEventListener("touchstart",(function(t){k=1,D=t.targetTouches[0].identifier}),!1),document.addEventListener("touchmove",(function(t){1===k&&t.targetTouches[0].target===f&&(F=t.targetTouches[0].pageX,T=t.targetTouches[0].pageY,"BODY"===f.offsetParent.tagName.toUpperCase()?(F-=f.offsetLeft,T-=f.offsetTop):(F-=f.offsetParent.offsetLeft,T-=f.offsetParent.offsetTop),l.clearRect(0,0,f.width,f.height),E(),W(),StickStatus.xPosition=F,StickStatus.yPosition=T,StickStatus.x=((F-C)/v*100).toFixed(),StickStatus.y=((T-p)/v*100*-1).toFixed(),StickStatus.cardinalDirection=G(),i(StickStatus))}),!1),document.addEventListener("touchend",(function(t){if(t.changedTouches[0].identifier!==D)return;k=0,h&&(F=C,T=p);l.clearRect(0,0,f.width,f.height),E(),W(),StickStatus.xPosition=F,StickStatus.yPosition=T,StickStatus.x=((F-C)/v*100).toFixed(),StickStatus.y=((T-p)/v*100*-1).toFixed(),StickStatus.cardinalDirection=G(),i(StickStatus)}),!1),f.addEventListener("mousedown",(function(t){k=1}),!1),document.addEventListener("mousemove",(function(t){1===k&&(F=t.pageX,T=t.pageY,"BODY"===f.offsetParent.tagName.toUpperCase()?(F-=f.offsetLeft,T-=f.offsetTop):(F-=f.offsetParent.offsetLeft,T-=f.offsetParent.offsetTop),l.clearRect(0,0,f.width,f.height),E(),W(),StickStatus.xPosition=F,StickStatus.yPosition=T,StickStatus.x=((F-C)/v*100).toFixed(),StickStatus.y=((T-p)/v*100*-1).toFixed(),StickStatus.cardinalDirection=G(),i(StickStatus))}),!1),document.addEventListener("mouseup",(function(t){k=0,h&&(F=C,T=p);l.clearRect(0,0,f.width,f.height),E(),W(),StickStatus.xPosition=F,StickStatus.yPosition=T,StickStatus.x=((F-C)/v*100).toFixed(),StickStatus.y=((T-p)/v*100*-1).toFixed(),StickStatus.cardinalDirection=G(),i(StickStatus)}),!1),E(),W();let D=null;function G(){let t="",e=F-C,i=T-p;return i>=L&&i<=w&&(t="C"),iw&&(t="S"),ey&&("C"===t?t="E":t+="E"),t}this.GetWidth=function(){return f.width},this.GetHeight=function(){return f.height},this.GetPosX=function(){return F},this.GetPosY=function(){return T},this.GetX=function(){return((F-C)/v*100).toFixed()},this.GetY=function(){return((T-p)/v*100*-1).toFixed()},this.GetDir=function(){return G()}}; \ No newline at end of file