# Joystick

A pixel-art arcade stick in an octagonal gate. The stick is drawn onto a 64 × 52 canvas on every frame and scaled up by whole pixels, so each tilt stays crisp, outlined and dithered. Drag the ball and it follows your hand with a little mass. Let go and the return spring overshoots and settles. Four microswitches click as the stick crosses into each direction, the shaft knocks on the gate, and the spring twangs on release. All sound is made with Web Audio. Focus the stick and use the arrow keys or WASD. A connected gamepad's stick or d-pad also drives it.

<demo-joystick>



</demo-joystick>

## Installation

```bash
npx shadcn-vue@latest add "https://nxui.geoql.in/r/joystick.json"
```

## Usage

```vue
<script setup lang="ts">
  import { computed } from 'vue';
  import Joystick from '~/components/ui/joystick/Joystick.vue';
  import type { JoystickDirection } from '~/components/ui/joystick/types';

  const colorMode = useColorMode();
  const theme = computed(() => (colorMode.value === 'dark' ? 'dark' : 'light'));

  function onMove(x: number, y: number) {
    console.log(x.toFixed(2), y.toFixed(2));
  }

  function onSwitch(direction: JoystickDirection, on: boolean) {
    console.log(direction, on ? 'make' : 'break');
  }
</script>

<template>
  <Joystick
    :pixel="8"
    :sound="true"
    :theme="theme"
    @move="onMove"
    @switch="onSwitch"
  />
</template>
```

The stick sizes itself. It uses the `pixel` size when its parent has room, and otherwise the largest whole pixel that fits the parent's content box (never less than 2).

## Props

<table>
<thead>
  <tr>
    <th>
      Prop
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Default
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        pixel
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <code>
        8
      </code>
    </td>
    
    <td>
      Size of one art pixel in CSS px. It shrinks to fit a narrow container.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        sound
      </code>
    </td>
    
    <td>
      <code>
        boolean
      </code>
    </td>
    
    <td>
      <code>
        true
      </code>
    </td>
    
    <td>
      Microswitch clicks, the gate knock and the spring's twang.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        theme
      </code>
    </td>
    
    <td>
      <code>
        'light' | 'dark'
      </code>
    </td>
    
    <td>
      <code>
        'light'
      </code>
    </td>
    
    <td>
      Colour of the pool of light behind the stick. Pass your site's colour mode.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        auto-focus
      </code>
    </td>
    
    <td>
      <code>
        boolean
      </code>
    </td>
    
    <td>
      <code>
        false
      </code>
    </td>
    
    <td>
      Take keyboard focus on mount, so the arrow keys work at once.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        label
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        'Arcade joystick…'
      </code>
    </td>
    
    <td>
      Accessible name of the stick.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        class
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        ''
      </code>
    </td>
    
    <td>
      Extra classes for the root element.
    </td>
  </tr>
</tbody>
</table>

## Events

<table>
<thead>
  <tr>
    <th>
      Event
    </th>
    
    <th>
      Payload
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        move
      </code>
    </td>
    
    <td>
      <code>
        (x: number, y: number)
      </code>
    </td>
    
    <td>
      Fires as the stick moves. <code>
        x
      </code>
      
       and <code>
        y
      </code>
      
       are in −1…1, +x is right, +y toward the user.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        switch
      </code>
    </td>
    
    <td>
      <code>
        (direction: JoystickDirection, on: boolean)
      </code>
    </td>
    
    <td>
      Fires when a microswitch makes (<code>
        true
      </code>
      
      ) at 0.42 travel and breaks (<code>
        false
      </code>
      
      ) at 0.3.
    </td>
  </tr>
</tbody>
</table>

## Methods

Get a template ref to call these. They do nothing while a pointer, key or gamepad holds the stick.

<table>
<thead>
  <tr>
    <th>
      Method
    </th>
    
    <th>
      Signature
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        steer
      </code>
    </td>
    
    <td>
      <code>
        (x: number, y: number) => void
      </code>
    </td>
    
    <td>
      Moves the stick toward <code>
        x
      </code>
      
      , <code>
        y
      </code>
      
      , clamped to the gate.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        release
      </code>
    </td>
    
    <td>
      <code>
        () => void
      </code>
    </td>
    
    <td>
      Lets the stick go, so the return spring brings it home.
    </td>
  </tr>
</tbody>
</table>

## Accessibility

- The stick is a focusable `role="application"` element with an accessible name, so a screen reader passes the arrow keys through.
- The arrow keys and WASD work only while the stick has focus, so the page keeps its own arrow keys. Opposite keys cancel out and two keys give a diagonal.
- A dashed focus ring shows when you reach the stick with the keyboard. It does not show after a mouse or touch grab.
- Sound starts only after a gesture on the page. Haptics use vibration on Android, the switch tap on iOS 18 and later, and rumble on gamepads.
- With reduced motion the stick returns without overshoot, and the entrance drop and the gate knock do not play.

Ported from [Joystick](https://pinui.xyz/components/joystick) by Pin UI, MIT licence.
