Home > Software engineering >  What is the documented range for TouchEvent#touches?
What is the documented range for TouchEvent#touches?

Time:02-05

I'm looking at the TouchEvent#touches documentation from MDN web docs, and the code sample provided only supports up to 3 touches.

someElement.addEventListener('touchstart', (e) => {
   // Invoke the appropriate handler depending on the
   // number of touch points.
   switch (e.touches.length) {
     case 1: handle_one_touch(e); break;
     case 2: handle_two_touches(e); break;
     case 3: handle_three_touches(e); break;
     default: console.log("Not supported"); break;
   }
}, false);

That being said, I'm pretty sure that there are 3 finger touches, but I was wondering if there was a min/max range for these touches (e.g. can there be "0" touches in any scenario?)

CodePudding user response:

Practically speaking, the W3C standard for TouchEvent does not define a maximum number of touches. Instead, the maximum is going to be the maximum number of consecutive touch points supported by the device's touch screen. In a virtual environment, this could presumably be infinite. Older devices seem to have been less than 20 consecutive touch points, however this could have increased with more modern technology.

Technically speaking, the spec defines the TouchList#length property as an unsigned long, so the absolute maximum within the spec will be 4,294,967,295 consecutive touch points:

interface TouchList {
    readonly    attribute unsigned long length;
    getter Touch? item (unsigned long index);
};

As for the minimum, the spec does not say that there must be at least one item in the list of active touches. However, if you are listening to the touchstart event, then the received TouchList will always contain the touch which triggered the event:

touches of type TouchList, readonly

a list of Touches for every point of contact currently touching the surface.

and:

changedTouches of type TouchList, readonly

a list of Touches for every point of contact which contributed to the event.

For the touchstart event this must be a list of the touch points that just became active with the current event. [...]

  • Related