Differences between JS touches variables
There are three common touches variables in JS, which is touches, targetTouches
and changedTouches when developing JS applications. Sometimes you can achieve
the same effects without regarding what they represent. However, when your design
involves massive touching interactions with users, things can be complex and
messy if you do not take care when to use each touches variables.
Here are the definations of these touches variables and hope you can get a sense of how they are differed from each other and when should each be used.
Definations
touches
A
TouchListlisting all theTouchobjects for touch points that are still in contact with the touch surface, regardless of whether or not they’ve changed or what their target element was attouchstarttime.
targetTouches
A
TouchListlisting all theTouchobjects for touch points that are still in contact with the touch surface and whosetouchstartevent occurred inside the same targetelementas the current target element.
changedTouches
A
TouchListwhoseTouchobjects include all the touch points that contributed to this touch event.
Illustration with examples
- When only one finger touches the screen, all three variables will have the same value, that is one thouch object.
- When one finger has been on the screen and the other finger touches a different element on the screen,
toucheswill have two touch objects and each object represent a touch point andtargetToucheswill have the same values astouches. If the two fingers touch the same element on the screen,targetToucheswill only have one touch variable. In both cases, whether the two fingers on the same element or not,changedToucheswill have one touch object that represent the second finger touch point since it is the canse of event. - When two fingers touch the screen at the same time,
changedToucheshave two touch objects because each finger touch point corresponds one objects. - When the finger slides the screen, all three variables will change.
- When one finger is off screen, the corresponding touch object in
touchesandtargetToucheswill be removed while the corresponding object inchangedTouchesremains and the position is the last touch point before the finger off screen. - When all fingers are off screen,
touchesandtargetToucheswill be empty whilechangedTouchesremains one object that corresponds the last touch point before the last finger off the screen.