joystick overlay only appears on first touch

main
Codinget 5 years ago
parent 6ea0a3487d
commit 6fcb603835
  1. 12
      src/js/input.js

@ -23,7 +23,11 @@ const handleAngleMagnitude=(x, y, threshold=0, fn=null) => {
if(fn) fn(angle, magnitude); if(fn) fn(angle, magnitude);
} }
} };
const removeChild=(parent, child) => {
if(child.parentNode==parent) parent.removeChild(child);
};
const handleCrosspad=(() => { const handleCrosspad=(() => {
const ns='http://www.w3.org/2000/svg'; const ns='http://www.w3.org/2000/svg';
@ -50,7 +54,7 @@ const handleCrosspad=(() => {
let enabled=false; let enabled=false;
const displayOverlay=() => { const displayOverlay=() => {
if(useOverlay && enabled) hud.appendChild(cross); if(useOverlay && enabled) hud.appendChild(cross);
else hud.removeChild(cross); else removeChild(hud, cross);
}; };
config.watchB('input.touchscreen.crosspad.overlay', (k, v) => { config.watchB('input.touchscreen.crosspad.overlay', (k, v) => {
useOverlay=v; useOverlay=v;
@ -98,6 +102,7 @@ const handleJoystick=(() => {
let ctx=cvs.getContext('2d'); let ctx=cvs.getContext('2d');
let enabled=false; let enabled=false;
let useOverlay=false; let useOverlay=false;
let firstTouch=false;
let center={ let center={
x: 0, x: 0,
@ -106,7 +111,7 @@ const handleJoystick=(() => {
let deadzone; let deadzone;
const displayOverlay=() => { const displayOverlay=() => {
if(!enabled || !useOverlay) return hud.removeChild(cvs); if(!enabled || !useOverlay || !firstTouch) return removeChild(hud, cvs);
cvs.width=cvs.height=4*deadzone+120; cvs.width=cvs.height=4*deadzone+120;
hud.appendChild(cvs); hud.appendChild(cvs);
@ -160,6 +165,7 @@ const handleJoystick=(() => {
touchstart: e => { touchstart: e => {
center.x=e.touches[0].clientX; center.x=e.touches[0].clientX;
center.y=e.touches[0].clientY; center.y=e.touches[0].clientY;
firstTouch=true;
displayOverlay(); displayOverlay();
}, },
touchmove: e => touchmove: e =>

Loading…
Cancel
Save