Skip to content

Joystick

Usage

Basic Usage

html
<Canvas>
    <Joystick />
</Canvas>

With Controls Integration

The Joystick can be automatically integrated with the controls system, similar to gamepad controls:

html
<Canvas>
    <Rect controls={controlsConfig} x={x} y={y} />
    
    <Joystick 
        controls={element.directives.controls}
        outerColor="#34495e"
        innerColor="#3498db"
    />
</Canvas>

<script>
    import { signal, mount } from "canvasengine";

    const x = signal(200);
    const y = signal(200);
    const speed = 10;

    const controls = signal({
        up: {
            repeat: true,
            bind: "up",
            keyDown() {
                y.update((y) => Math.max(0, y - speed));
            },
        },
        down: {
            repeat: true,
            bind: "down",
            keyDown() {
                y.update((y) => y + speed);
            },
        },
        left: {
            repeat: true,
            bind: "left",
            keyDown() {
                x.update((x) => Math.max(0, x - speed));
            },
        },
        right: {
            repeat: true,
            bind: "right",
            keyDown() {
                x.update((x) => x + speed);
            },
        },
        joystick: {
            enabled: true,
            directionMapping: {
                'top': 'up',
                'bottom': 'down',
                'left': 'left',
                'right': 'right',
                'top_left': ['up', 'left'],
                'top_right': ['up', 'right'],
                'bottom_left': ['down', 'left'],
                'bottom_right': ['down', 'right']
            },
            moveInterval: 50,
            threshold: 0.1
        }
    });

    mount((element) => {
        // The joystick will automatically use the controls from the Rect element
    });
</script>

Properties

PropertyTypeDefaultDescription
outerstring-Path to the outer joystick image
innerstring-Path to the inner joystick image
outerColorstring-Color of the outer joystick element
innerColorstring-Color of the inner joystick element
outerScale{ x: number; y: number }Scale of the outer joystick element
innerScale{ x: number; y: number }Scale of the inner joystick element
onChange(data: JoystickChangeEvent) => void-Callback function triggered when joystick position changes
onStart() => void-Callback function triggered when joystick interaction starts
onEnd() => void-Callback function triggered when joystick interaction ends
controlsControlsDirective | JoystickControls-Controls instance to automatically apply joystick events to. Can be accessed via element.directives.controls

And use all the properties of the Container component.

JoystickChangeEvent

The onChange callback receives a JoystickChangeEvent object with the following properties:

PropertyTypeDescription
anglenumberAngle in degrees (0-360)
directionDirectionDirection enum value (LEFT, RIGHT, TOP, BOTTOM, TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT)
powernumberPower/distance from center (0-1)

Joystick Configuration

When using the joystick with the controls system, you can configure it in the controls configuration:

PropertyTypeDefaultDescription
enabledbooleantrueWhether joystick controls are enabled
directionMappingobjectSee belowMapping of joystick directions to control names
moveIntervalnumber50Interval in milliseconds for repeating movement actions
thresholdnumber0.1Minimum power value (0-1) required to trigger movement

Default Direction Mapping

typescript
{
    'top': 'up',
    'bottom': 'down',
    'left': 'left',
    'right': 'right',
    'top_left': ['up', 'left'],
    'top_right': ['up', 'right'],
    'bottom_left': ['down', 'left'],
    'bottom_right': ['down', 'right']
}

You can override this mapping in your controls configuration. Diagonal directions can map to multiple controls (array) or a single control (string).