Touch

The Touch component provides access to touch events generated by touchscreens and other touch-sensitive input devices. It allows web applications to respond to user gestures such as taps, swipes, pinches, rotations, and multi-touch interactions, enabling rich mobile experiences.

This component is particularly useful for:

  • Drawing and painting applications

  • Gesture-based navigation and controls

  • Multi-touch games and interactive experiences

  • Image manipulation (pinch-to-zoom, rotation)

  • Custom touch-optimized UI components

  • Signature capture and handwriting recognition

  • Touch-based animations and effects

  • Canvas-based interactive applications

  • Mobile-first interactive dashboards

Browser Support

The Touch API is widely supported on all modern mobile browsers and touch-enabled devices. Desktop browsers typically support touch events when used with touch-capable hardware (touchscreen monitors, trackpads with multi-touch support).

Supported Platforms:

  • iOS Safari: Full support

  • Android Chrome: Full support

  • Mobile Firefox: Full support

  • Desktop browsers with touch hardware: Full support

circle-info

The component automatically detects touch capability. On non-touch devices, the events won't be dispatched.

Usage

Basic Touch Drawing

Swipe Gesture Detection

Pinch-to-Zoom

Multi-Touch Drawing with Different Colors

Rotation Gesture

Touch-Based Signature Pad

Touch-Based Game (Bubble Pop)

Parameters

None

Actions

None - This component automatically monitors touch events on the controller element.

Targets

canvas

Optional target for canvas elements. While not required, using this target ensures proper touch event handling on canvas elements.

Events

pwa--touch:started

Dispatched when a new touch contact is detected (finger touches the screen). Can fire multiple times if multiple fingers touch simultaneously.

Payload: Native Touch object

Properties include:

  • identifier (number): Unique identifier for this touch point

  • clientX (number): X coordinate relative to viewport

  • clientY (number): Y coordinate relative to viewport

  • pageX (number): X coordinate relative to document

  • pageY (number): Y coordinate relative to document

  • screenX (number): X coordinate relative to screen

  • screenY (number): Y coordinate relative to screen

  • radiusX (number): X radius of contact area

  • radiusY (number): Y radius of contact area

  • rotationAngle (number): Rotation angle of contact area

  • force (number): Pressure of touch (0-1, if supported)

Example:

pwa--touch:moved

Dispatched when a touch contact moves across the screen.

Payload: Native Touch object (same properties as started)

Example:

pwa--touch:ended

Dispatched when a touch contact ends (finger lifts from screen).

Payload: Native Touch object (same properties as started)

Example:

pwa--touch:cancelled

Dispatched when a touch contact is canceled by the browser or OS. This can happen due to:

  • Context switches (app switching, notifications)

  • Gesture interruptions (system gestures)

  • Too many simultaneous touches

  • Touch moved outside tracking area

Payload: Native Touch object (same properties as started)

Example:

pwa--touch:updated

Dispatched after each native touch event, once per DOM event cycle. Provides a complete snapshot of all active touches managed by the controller. This is particularly useful for multi-touch gestures like pinch-zoom and rotation that require tracking multiple touch points simultaneously.

Payload: {touches: Array<TouchData>}

Each touch in the array contains:

  • identifier (number): Unique touch identifier

  • clientX (number): X coordinate relative to viewport

  • clientY (number): Y coordinate relative to viewport

  • pageX (number): X coordinate relative to document

  • pageY (number): Y coordinate relative to document

  • screenX (number): X coordinate relative to screen

  • screenY (number): Y coordinate relative to screen

  • radiusX (number): X radius of contact area

  • radiusY (number): Y radius of contact area

  • force (number): Pressure of touch

  • rotationAngle (number): Rotation angle of contact area

  • top (number): Y coordinate (alias for clientY)

  • left (number): X coordinate (alias for clientX)

Example:

Best Practices

  1. Prevent default scrolling: Use touch-action: none CSS on touch-interactive elements

  2. Handle cancellation: Always handle cancelled events to clean up state

  3. Track touch identifiers: Use identifier to track individual touches in multi-touch scenarios

  4. Optimize performance: Minimize work in touch event handlers, especially for moved

  5. Provide visual feedback: Show immediate visual response to touch interactions

  6. Consider touch size: Design touch targets at least 44x44 pixels

  7. Test on devices: Touch behavior varies between devices - test on real hardware

  8. Avoid hover-based UI: Touch devices don't have hover - design accordingly

  9. Handle orientation changes: Account for device rotation

  10. Be mindful of gestures: Some gestures may conflict with system gestures

Gesture Detection Patterns

Tap Detection

Long Press Detection

Double Tap Detection

Velocity Calculation

Complete Example: Advanced Drawing App

Troubleshooting

Touch events not firing

Possible causes:

  1. Element doesn't have touch-action CSS property set

  2. Event listeners on wrong element

  3. Browser doesn't support touch events

Solutions:

Accidental scrolling during drawing

Issue: Page scrolls when trying to draw

Solution: Use touch-action: none on interactive elements:

Multi-touch not working

Issue: Only one touch point detected

Cause: Using started/moved/ended events instead of updated

Solution: Use the updated event for multi-touch gestures:

Touch coordinates incorrect

Issue: Touch position doesn't match visual location

Cause: Not accounting for element offset

Solution: Calculate relative coordinates:

Performance issues with many touch events

Issue: App lags during touch interaction

Solutions:

  1. Throttle touch event processing

  2. Use requestAnimationFrame for rendering

  3. Minimize work in event handlers

  4. Use Web Workers for heavy computations

Ghost clicks on touch devices

Issue: Touch triggers both touch and click events

Solution: Prevent default on touch events when needed:

circle-exclamation

Last updated

Was this helpful?