作る

VirtualJoystick

On-screen analog joystick for touch devices. Returns normalized x/y values in [-1, 1] range.

VirtualJoystick

On-screen analog joystick for touch devices. Returns normalized x/y values in [-1, 1] range.

Examples

const joystick = new VirtualJoystick();
 
function loop() {
  player.move(joystick.x, joystick.y);
  requestAnimationFrame(loop);
}

Members

x

x: number

Horizontal axis: -1 (left) to 1 (right), 0 when idle

y

y: number

Vertical axis: -1 (up) to 1 (down), 0 when idle

angle

angle: number | null

Angle in radians (0 = right, PI/2 = down), or null when idle

magnitude

magnitude: number

Distance from center, 0 to 1

show

show(): void

Makes the joystick visible regardless of device detection.

hide

hide(): void

Hides the joystick overlay.

destroy

destroy(): void

Removes DOM listeners and tears down the joystick.