Skip to content

Commit 045d1af

Browse files
committed
✨ Support multiple touches, fixes elmarti#34
previously we listened for a singleton window event for mouseup/touchend. For touchend we now store the touch.identifier to allow us to identify the touchmove and touchend events, so that we can treat them diffrently per component instance
1 parent f89fa5c commit 045d1af

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

src/Joystick.tsx

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class Joystick extends React.Component<IJoystickProps, IJoystickState> {
7676
private _radius: number;
7777
private _parentRect: DOMRect;
7878
private readonly _boundMouseMove: (event: any) => void;
79+
private _touchIdentifier: number|null = null
7980

8081
constructor(props: IJoystickProps) {
8182
super(props);
@@ -102,8 +103,8 @@ class Joystick extends React.Component<IJoystickProps, IJoystickState> {
102103
};
103104
})();
104105

105-
this._boundMouseUp = () => {
106-
this._mouseUp();
106+
this._boundMouseUp = (event: any) => {
107+
this._mouseUp(event);
107108
};
108109
this._boundMouseMove = (event: any) => {
109110
this._mouseMove(event);
@@ -176,7 +177,8 @@ class Joystick extends React.Component<IJoystickProps, IJoystickState> {
176177
* @param e MouseEvent
177178
* @private
178179
*/
179-
private _mouseDown(e: MouseEvent) {
180+
private _mouseDown(e: MouseEvent| any) {
181+
e.preventDefault();
180182
if (this.props.disabled || this.props.followCursor) {
181183
return;
182184
}
@@ -190,6 +192,7 @@ class Joystick extends React.Component<IJoystickProps, IJoystickState> {
190192
window.addEventListener(InteractionEvents.MouseUp, this._boundMouseUp);
191193
window.addEventListener(InteractionEvents.MouseMove, this._boundMouseMove);
192194
} else {
195+
this._touchIdentifier = e.targetTouches[0].identifier;
193196
window.addEventListener(InteractionEvents.TouchEnd, this._boundMouseUp);
194197
window.addEventListener(InteractionEvents.TouchMove, this._boundMouseMove);
195198
}
@@ -247,16 +250,21 @@ class Joystick extends React.Component<IJoystickProps, IJoystickState> {
247250
* @param event
248251
* @private
249252
*/
250-
private _mouseMove(event: MouseEvent | TouchEvent) {
253+
private _mouseMove(event: MouseEvent | any) {
254+
event.preventDefault();
251255
if (this.state.dragging) {
256+
if(event.targetTouches && event.targetTouches[0].identifier !== this._touchIdentifier){
257+
return;
258+
}
259+
252260
let absoluteX = null;
253261
let absoluteY = null;
254262
if (event instanceof MouseEvent) {
255263
absoluteX = event.clientX;
256264
absoluteY = event.clientY;
257265
} else {
258-
absoluteX = event.touches[0].clientX;
259-
absoluteY = event.touches[0].clientY;
266+
absoluteX = event.targetTouches[0].clientX;
267+
absoluteY = event.targetTouches[0].clientY;
260268
}
261269

262270

@@ -286,7 +294,15 @@ class Joystick extends React.Component<IJoystickProps, IJoystickState> {
286294
* Handle mouse up and de-register listen events
287295
* @private
288296
*/
289-
private _mouseUp() {
297+
private _mouseUp(event: any) {
298+
if(event.touches){
299+
for(const touch of event.touches){
300+
// this touch id is still in the TouchList, so this mouse up should be ignored
301+
if(touch.identifier === this._touchIdentifier){
302+
return;
303+
}
304+
}
305+
}
290306
const stateUpdate = {
291307
dragging: false,
292308
} as any;

0 commit comments

Comments
 (0)