Skip to main content

Analog

The analog element is a touch input mimicking analog inputs like joysticks on controllers.

It is a versatile element, designed specifically for game-like controls with touch, mirroring the Gamepad API axes properties.

<xyba-analog></xyba-analog>

Install

npm install xyba-elements

Importing

Once installed, you can import via xyba-elements.

// Custom Elements
import "xyba-elements/elements/analog/analog.js";

// React
import { Analog } from "xyba-elements/react";

Or import from CDN,

<script src="https://cdn.jsdelivr.net/npm/xyba-elements/dist/cdn/elements/analog/analog.js">

Examples

Adapt to initial value

Sets the value from the initial touch point always calculated from the center of the input, also when track-from is in use.

<xyba-analog adapt></xyba-analog>

Trail along the pointer

Have the analog input trailing along the pointer when exceeding the edge.

<xyba-analog trail></xyba-analog>

Relocate to pointer location

Relocates to pointer touch point, useful is combination with track-from to track from a larger area.

<xyba-analog relocate></xyba-analog>

Persist the location

Leaves the analog input where it is inactivated.

<xyba-analog persistent adapt trail></xyba-analog>

Capture pointer

Add the attribute or property capture to prevent interacting with other elements when active.

A
<xyba-analog trail capture></xyba-analog> <xyba-button>A</xyba-button>

Track from element or document

You can have analog input track from another HTML element using the attribute track-from or property .trackFrom using a query selector.

<div id="track-from-element">
<xyba-analog track-from="#track-from-element" trail></xyba-analog>
</div>

Limit to axis X / Y

Limit the analog input to only x or y direction.

X
Y
<xyba-analog axis="x">X</xyba-analog>
<br />
<xyba-analog axis="y">Y</xyba-analog>

Specify deadzone

The threshold from 0 (center) to 1 (outer) before triggering a direction and direction event.

<xyba-analog id="deadzone-example" deadzone="0.5"></xyba-analog>

Offset knob

Set the offset of the knob in percentage of the knob size eg. 50% or in pixel value eg. 20px or 20.

<xyba-analog knob-offset="50%"></xyba-analog>

Set custom radius

Set a custom radius in px instead of automatically calculating radius from the size of the analog input element.

<xyba-analog radius="60"></xyba-analog>

Customizing style

Disable default visual style

Add the attribute no-style to remove all default visual styling to implement your own style. The base styles for layout and pointer-events are preserved.

A
<xyba-analog no-style>A</xyba-analog>

Style using ::part

The input can be styled using ::parts

<xyba-analog id="parts-demo" no-style knob-offset="33%" adapt>
<svg viewBox="0 0 24 24" width="24" height="24">
<path
fill="currentColor"
d="M9.37,5.51C9.19,6.15,9.1,6.82,9.1,7.5c0,4.08,3.32,7.4,7.4,7.4c0.68,0,1.35-0.09,1.99-0.27C17.45,17.19,14.93,19,12,19 c-3.86,0-7-3.14-7-7C5,9.07,6.81,6.55,9.37,5.51z M12,3c-4.97,0-9,4.03-9,9s4.03,9,9,9s9-4.03,9-9c0-0.46-0.04-0.92-0.1-1.36 c-0.98,1.37-2.58,2.26-4.4,2.26c-2.98,0-5.4-2.42-5.4-5.4c0-1.81,0.89-3.42,2.26-4.4C12.92,3.04,12.46,3,12,3L12,3z"
></path>
</svg>
</xyba-analog>

Slotted elements

The analog elements provides the slots base, knob and overlay.

<xyba-analog>
<img
src="https://cdn.theatlantic.com/thumbor/vDZCdxF7pRXmZIc5vpB4pFrWHKs=/559x0:2259x1700/1080x1080/media/img/mt/2017/06/shutterstock_319985324/original.jpg"
slot="base"
width="128"
height="128"
loading="eager"
/>
<img
src="https://cdn.iconscout.com/icon/premium/png-256-thumb/yarn-ball-4163435-3452680.png?f=webp"
slot="knob"
width="48px"
height="48px"
loading="eager"
/>
</xyba-analog>

Events

Use inline events

Call global functions directly in the html using inline events. All events are available with the prefix on{event_name} as attributes and properties.

<xyba-analog
onupenter="console.log('upenter')"
onupleave="console.log('upleave')"
onrightenter="console.log('rightenter')"
onrightleave="console.log('rightleave')"
ondownenter="console.log('downenter')"
ondownleave="console.log('downleave')"
onleftenter="console.log('leftenter')"
onleftleave="console.log('leftleave')"
onactive="console.log('active')"
oninactive="console.log('inactive')"
ondirection="(e) => console.log(e)"
onchange="(e) => console.log(e)"
></xyba-analog>

Use event listeners

You can also listen to all events with event listeners.

<xyba-analog id="event-listener-demo"></xyba-analog>

Read state in game loop

It's also possible to read the current state instead of handling events

<xyba-analog id="game-loop-demo"></xyba-analog>

Properties / Attributes

propertyattributetypedefaultdescriptionreadonly
trailtrailbooleanfalseIf the analog element should follow the pointer when active
relocaterelocatebooleanfalseIf the analog element should relocate to the pointer when activated
knobOffsetknob-offset"string" | `${number}%` | number0How much the knob should offset from the edge of the analog element in pixel or percentage
axisaxis'x' | 'y' | undefinedThe axis to limit the analog input to
adaptadaptbooleanfalseIf the analog element starts with the position of the initial pointer position
deadzonedeadzonenumber | undefinedThe center threshold of the analog element where it's considered to be in the center
radiusradiusnumber | undefinedThe effective radius of the analog element
trackFromtrack-fromstring | "document" | undefinedThe query selector of the element to use as the track container or document
activeactivebooleanfalseIf the analog element is active
persistentpersistentbooleanfalseIf the analog element should be persistent in the location is is when inactive.
capturecapturebooleanfalseIf the analog element should capture the pointer events
directiondirection'none' | 'up' | 'up_right' | 'right' | 'down_right' | 'down' | 'down_left' | 'left' | 'up_left' |"none"The current direction of the analog element
directions{ up: boolean; right: boolean; down: boolean; left: boolean; }The current directions of the analog elementreadonly
noStyleno-stylebooleanfalseWhether the element should use default style
valuenumber[]Gets the axes value
axesnumber[]Gets the axes valuereadonly
angleRadiansnumberGets the angle in radiansreadonly
angleDegreesnumberGets the angle in degreesreadonly
magnitudenumberGets the magnitude from center (0 - 1)readonly
inDeadzonebooleanWhether it is withing the deadzone thresholdreadonly
positionVector | undefinedThe current position of the elementreadonly
movement{ x: number; y: number }The movement of the pad since last time it was requested.readonly
onupenteronupenterThe event attribute / property for the `upenter` event.
onupleaveonupleaveThe event attribute / property for the `upleave` event.
onrightenteronrightenterThe event attribute / property for the `rightenter` event.
onrightleaveonrightleaveThe event attribute / property for the `rightleave` event.
ondownenterondownenterThe event attribute / property for the `downenter` event.
ondownleaveondownleaveThe event attribute / property for the `downleave` event.
onleftenteronleftenterThe event attribute / property for the `leftenter` event.
onleftleaveonleftleaveThe event attribute / property for the `leftleave` event.
onactiveonactiveThe event attribute / property for the `active` event.
oninactiveoninactiveThe event attribute / property for the `inactive` event.
onchangeonchangeThe event attribute / property for the `change` event.
ondirectionondirectionThe event attribute / property for the `direction` event.

CSS Slots

namedescription
The default content slotted into the knob (Only when no knob slot element present).
baseThe base element of the analog element.
overlayThe overlay element of the analog element.
knobThe knob element of the analog element.

Events

namedescriptiontype
upenterDispatched when the analog element enters the up direction.AnalogDirectionEvent
upleaveDispatched when the analog element leaves the up direction.AnalogDirectionEvent
rightenterDispatched when the analog element enters the right direction.AnalogDirectionEvent
rightleaveDispatched when the analog element leaves the right direction.AnalogDirectionEvent
downenterDispatched when the analog element enters the down direction.AnalogDirectionEvent
downleaveDispatched when the analog element leaves the down direction.AnalogDirectionEvent
leftenterDispatched when the analog element enters the left direction.AnalogDirectionEvent
leftleaveDispatched when the analog element leaves the left direction.AnalogDirectionEvent
activeDispatched when the analog element is activated.AnalogActiveEvent
inactiveDispatched when the analog element is deactivated.AnalogActiveEvent
changeDispatched when the analog element changes.ChangeEvent
directionDispatched when the analog element changes direction.AnalogDirectionEvent

CSS Custom Properties

namedescriptiondefault
--xyba-analog-colorThe color used for inheritance.
--xyba-analog-sizeThe size of the analog element.
--xyba-analog-base-backdrop-filterThe backdrop filter of the analog element.
--xyba-analog-base-backgroundThe background of the analog element.
--xyba-analog-base-background-activeThe background of the analog element when active.
--xyba-analog-base-border-widthThe border width of the analog element.
--xyba-analog-base-border-colorThe border color of the analog element.
--xyba-analog-base-border-colorThe border color of the analog element when active.
--xyba-analog-base-border-styleThe border style of the analog element.
--xyba-analog-knob-sizeThe size of the knob element.
--xyba-analog-knob-backgroundThe background of the knob element.
--xyba-analog-knob-backdrop-filterThe backdrop filter of the knob element.
--xyba-analog-knob-border-widthThe border width of the knob element.
--xyba-analog-knob-border-colorThe border color of the knob element.
--xyba-analog-knob-border-colorThe border color of the knob element when active.
--xyba-analog-knob-border-styleThe border style of the knob element.
--xyba-analog-knob-text-colorThe text or icon color of the knob element.
--xyba-analog-knob-text-color-activeThe text or icon color of the knob element when active.

CSS Parts

namedescription
baseThe base element of the analog element.
overlayThe overlay element of the analog element.
knobThe knob element of the analog element.