menu

JavaScript/Typescript - 16 Topics

DEEP DIVE INTO

JavaScript/Typescript

Topic:touchcancel

menu

The touchcancel event in JavaScript is triggered when a touch point is interrupted or canceled due to an unexpected event, such as the user moving their touch point outside the browser window or the system terminating the touch interaction. This event allows you to handle situations where a touch interaction is abruptly terminated. Here's a deep explanation of the touchcancel event:

Basic Usage:

The touchcancel event is used with the addEventListener method to specify a function that should be executed when a touch point is canceled. The basic syntax looks like this:

javascriptelement.addEventListener('touchcancel', function(event) {
  // Code to be executed when a touch is canceled
});
  • element: The HTML element to which you want to attach the event listener.

  • touchcancel: The event type to listen for.

  • event: The event object containing information about the touch interaction.

Event Object Properties:

The event object passed to the touchcancel event handler contains properties that provide information about the touch interaction. These properties are similar to those in other touch events, including:

  • touches: A list of all touch points currently in contact with the screen.

  • targetTouches: A list of touch points that are currently in contact with the target element that triggered the event.

  • changedTouches: A list of touch points that have changed since the last touch event.

  • clientX and clientY: The coordinates of the touch point relative to the viewport.

  • pageX and pageY: The coordinates of the touch point relative to the entire document.

Example:

Here's a basic example of how to use the touchcancel event to change the background color of an element when a touch interaction is canceled:

html<!DOCTYPE html>
<html>
<head>
  <title>Touchcancel Example</title>
</head>
<body>
  <div id="touchDiv" style="width: 200px; height: 200px; background-color: lightgray;"></div>

  <script>
    const touchDiv = document.getElementById('touchDiv');

    touchDiv.addEventListener('touchcancel', function(event) {
      touchDiv.style.backgroundColor = 'lightcoral';
    });
  </script>
</body>
</html>

In this example, when a touch interaction is canceled on the touchDiv element, the background color changes to light coral.

Common Use Cases:

  1. Building Touch-Friendly Interfaces: The touchcancel event is important for creating robust touch-friendly web applications. It allows you to handle scenarios where a touch interaction is unexpectedly terminated.

  2. Error Handling: You can use touchcancel to clean up resources or handle errors when a touch interaction is canceled, ensuring that your application remains responsive and user-friendly.

  3. Interactive Games and Apps: Games and interactive applications often rely on touch events, including touchcancel, to manage user input and handle unexpected interruptions.

Event Precautions:

When working with the touchcancel event, be aware that touch interactions can be canceled for various reasons, and it's essential to handle such situations gracefully. Additionally, consider cross-browser compatibility and potential differences in touch event handling on different devices and platforms.

warning

In summary, the touchcancel event in JavaScript is a crucial event for handling the cancellation of touch interactions on touch-enabled devices. It allows you to respond to unexpected interruptions in touch-based interactions, ensuring that your web applications remain user-friendly and responsive even when touch events are canceled.

1280 x 720 px