Appearance
touch-raycaster
The touch-raycaster component enables touch-based interaction on mobile devices by converting screen taps into raycast-driven click events on scene entities.
It acts as a lightweight fallback interaction system for platforms without hand-tracking (e.g., iOS, Android), allowing UI elements such as buttons or text inputs to be activated through simple screen touches.
Example
Add the touch-raycaster component to the <a-scene> to enable touch gesture support:
js
import 'spatial-design-system/components/autoXr.js';
import 'spatial-design-system/components/touch-raycaster.js';
import 'spatial-design-system/primitives/ar-button.js';html
<a-scene auto-xr touch-raycaster>
<!-- This button will be clickable by touch gestures -->
<a-ar-button position="0 1.5 -1" content="Button"></a-ar-button>
</a-scene>How It Works
- The component listens for
pointerupevents (screen taps) on the document. - When a tap occurs, the touch position is converted into normalized device coordinates.
- A ray is cast from the camera through the tap position into the scene.
- The first visible intersected entity is identified.
- That entity receives a
clickevent. - The component can be enabled or disabled at runtime using the event
toggle-touch-raycaster-disable-state.
Events
| Event | Parameters | Description |
|---|---|---|
click | { el: HTMLElement } | Emitted on the first visible intersected entity when a screen tap occurs. The el is the entity that was clicked. |
Features
- Enables touch-based interaction across the entire scene.
- Emits
clickon the first intersected visible object. - Works as a fallback for devices without hand-tracking support.
- Invisible interaction layer (no visual cursor).
- Can be globally toggled on/off using a custom event.
Limitations
- Intended mainly for mobile browsers without WebXR hand-tracking.
- Does not emit hover events (
mouseenter,mouseleave). - Does not provide visual feedback like
<a-cursor>. - No
click-endedevent, only a singleclick. - Depends on raycasting accuracy, which may vary with camera angle and object size.
