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
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
canvasOptional target for canvas elements. While not required, using this target ensures proper touch event handling on canvas elements.
Events
pwa--touch:started
pwa--touch:startedDispatched 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 pointclientX(number): X coordinate relative to viewportclientY(number): Y coordinate relative to viewportpageX(number): X coordinate relative to documentpageY(number): Y coordinate relative to documentscreenX(number): X coordinate relative to screenscreenY(number): Y coordinate relative to screenradiusX(number): X radius of contact arearadiusY(number): Y radius of contact arearotationAngle(number): Rotation angle of contact areaforce(number): Pressure of touch (0-1, if supported)
Example:
pwa--touch:moved
pwa--touch:movedDispatched when a touch contact moves across the screen.
Payload: Native Touch object (same properties as started)
Example:
pwa--touch:ended
pwa--touch:endedDispatched when a touch contact ends (finger lifts from screen).
Payload: Native Touch object (same properties as started)
Example:
pwa--touch:cancelled
pwa--touch:cancelledDispatched 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
pwa--touch:updatedDispatched 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 identifierclientX(number): X coordinate relative to viewportclientY(number): Y coordinate relative to viewportpageX(number): X coordinate relative to documentpageY(number): Y coordinate relative to documentscreenX(number): X coordinate relative to screenscreenY(number): Y coordinate relative to screenradiusX(number): X radius of contact arearadiusY(number): Y radius of contact areaforce(number): Pressure of touchrotationAngle(number): Rotation angle of contact areatop(number): Y coordinate (alias for clientY)left(number): X coordinate (alias for clientX)
Example:
Best Practices
Prevent default scrolling: Use
touch-action: noneCSS on touch-interactive elementsHandle cancellation: Always handle
cancelledevents to clean up stateTrack touch identifiers: Use
identifierto track individual touches in multi-touch scenariosOptimize performance: Minimize work in touch event handlers, especially for
movedProvide visual feedback: Show immediate visual response to touch interactions
Consider touch size: Design touch targets at least 44x44 pixels
Test on devices: Touch behavior varies between devices - test on real hardware
Avoid hover-based UI: Touch devices don't have hover - design accordingly
Handle orientation changes: Account for device rotation
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:
Element doesn't have touch-action CSS property set
Event listeners on wrong element
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:
Throttle touch event processing
Use
requestAnimationFramefor renderingMinimize work in event handlers
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:
Only prevent default when necessary, as it disables scrolling and other default behaviors.
Last updated
Was this helpful?