Some user agents have connected joystick devices (also known as gamepads). These devices are desirable and suited to input for gaming applications, and for "10 foot" user interfaces (presentations, media viewers).
Currently, the only way for a joystick to be used as input would be to emulate mouse or keyboard events, however this would lose information and require additional software outside of the user agent to accomplish emulation.
Meanwhile, native applications are capable of accessing these devices via system APIs.
The Joystick API provides a solution to this problem by specifying interfaces that allow web applications to directly act on joystick data.
This specification references interfaces from a number of other specifications:
This specification defines conformance criteria that apply to a single product: the user agent that implements the interfaces that it contains.
Implementations that use ECMAScript to implement the APIs defined in this specification MUST implement them in a manner consistent with the ECMAScript Bindings defined in the Web IDL specification [[!WEBIDL]] as this specification uses that specification and terminology.
A conforming implementation is required to implement all fields defined in this specification.
This interface defines an individual joystick device.
axes
and button
data
have been updated from the hardware, relative to a previously saved
timestamp.
This interface defines a collection of Joysticks. The DocumentJoysticks interface MUST be implemented on the Document object.
The example below demonstrates typical access to joysticks. Note the relationship with the WindowAnimationTiming [[!ANIMATION-TIMING]] interface.
function runAnimation() { window.requestAnimationFrame(runAnimation); for (var i = 0; i < document.joysticks.length; ++i) { var joy = document.joysticks[i]; // todo; simple demo of displaying joy.axes and joy.buttons } } window.requestAnimationFrame(runAnimation);
Coordination with WindowAnimationTiming
Interactive applications will typically be using the WindowAnimationTiming interface to drive animation, and will want coordinate animation with user joystick input. As such, the joystick data should be polled as closely as possible to immediately before the animation callbacks are executed, and with frequency matching that of the animation. That is, if the animation callbacks are running at 60Hz, the joystick inputs should also be sampled at that rate.
A user agent MUST dispatch this event type to indicate the user has connected a joystick. If a joystick was already connected when the page was loaded, the joystickconnected event will be dispatched when the user presses a button or moves an axis.
When a joystick is disconnected from the user agent, if the user agent has previously dispatched a joystickconnected event, a joystickdisconnected event MUST be dispatched.
More discussion needed, on whether to include or exclude axis and button
changed events, and whether to roll them more together
(joystickchanged
?), separate somewhat
(joystickaxischanged
?), or separate by individual axis
and button.
Many have made contributions in code, comments, or documentation:
Please let me know if I have inadvertently omitted your name.