Skip to content

Commit 6f70ec1

Browse files
committed
handles mouseButton for multi touches
1 parent 9cf3c8c commit 6f70ec1

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

src/events/pointer.js

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ function pointer(p5, fn){
851851
* </div>
852852
*/
853853
fn.touches = [];
854-
fn._activeTouches = new Map();
854+
fn._activePointers = new Map();
855855

856856
/**
857857
* A `Boolean` system variable that's `true` if the mouse is pressed and
@@ -920,7 +920,7 @@ function pointer(p5, fn){
920920

921921
if (e.pointerType == 'touch') {
922922
const touches = [];
923-
for (const touch of this._activeTouches.values()) {
923+
for (const touch of this._activePointers.values()) {
924924
touches.push(getTouchInfo(canvas, sx, sy, touch));
925925
}
926926
this.touches = touches;
@@ -969,11 +969,19 @@ function pointer(p5, fn){
969969
id: touch.pointerId,
970970
};
971971
}
972-
fn._setMouseButton = function(e) {
973-
this.mouseButton.left = (e.buttons & 1) !== 0;
974-
this.mouseButton.center = (e.buttons & 4) !== 0;
975-
this.mouseButton.right = (e.buttons & 2) !== 0;
976-
};
972+
973+
fn._setMouseButton = function(e) {
974+
// Check all active touches to determine button states
975+
this.mouseButton.left = Array.from(this._activePointers.values()).some(touch =>
976+
(touch.buttons & 1) !== 0
977+
);
978+
this.mouseButton.center = Array.from(this._activePointers.values()).some(touch =>
979+
(touch.buttons & 4) !== 0
980+
);
981+
this.mouseButton.right = Array.from(this._activePointers.values()).some(touch =>
982+
(touch.buttons & 2) !== 0
983+
);
984+
};
977985

978986
/**
979987
* A function that's called when the mouse moves.
@@ -1152,10 +1160,9 @@ function pointer(p5, fn){
11521160
const context = this._isGlobal ? window : this;
11531161
let executeDefault;
11541162
this._updatePointerCoords(e);
1155-
1156-
if(e.pointerType === 'touch') {
1157-
this._activeTouches.set(e.pointerId, e);
1158-
}
1163+
this._activePointers.set(e.pointerId, e);
1164+
this._setMouseButton(e);
1165+
11591166

11601167
if (!this.mouseIsPressed && typeof context.mouseMoved === 'function') {
11611168
executeDefault = context.mouseMoved(e);
@@ -1167,8 +1174,6 @@ function pointer(p5, fn){
11671174
if (executeDefault === false) {
11681175
e.preventDefault();
11691176
}
1170-
} else {
1171-
this._setMouseButton(e);
11721177
}
11731178
};
11741179

@@ -1319,13 +1324,9 @@ function pointer(p5, fn){
13191324
let executeDefault;
13201325
this.mouseIsPressed = true;
13211326

1322-
if (e.pointerType === 'touch') {
1323-
this._activeTouches.set(e.pointerId, e);
1324-
} else {
1325-
this._setMouseButton(e);
1326-
}
1327-
1328-
this._updatePointerCoords(e);
1327+
this._activePointers.set(e.pointerId, e);
1328+
this._setMouseButton(e);
1329+
this._updatePointerCoords(e);
13291330

13301331
if (typeof context.mousePressed === 'function') {
13311332
executeDefault = context.mousePressed(e);
@@ -1483,11 +1484,8 @@ function pointer(p5, fn){
14831484
let executeDefault;
14841485
this.mouseIsPressed = false;
14851486

1486-
if(e.pointerType == 'touch'){
1487-
this._activeTouches.delete(e.pointerId);
1488-
} else {
1489-
this._setMouseButton(e);
1490-
}
1487+
this._activePointers.delete(e.pointerId);
1488+
this._setMouseButton(e);
14911489

14921490
this._updatePointerCoords(e);
14931491

0 commit comments

Comments
 (0)