Skip to content

Commit b14f25e

Browse files
authored
Merge pull request #6740 from RandomGamingDev/touchstarted-fix
Fix for touchStarted triggers twice among other issues with touchStarted/mousePressed on mobile
2 parents 9ef898f + 05adbdc commit b14f25e

File tree

3 files changed

+10
-14
lines changed

3 files changed

+10
-14
lines changed

src/core/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ class p5 {
205205
};
206206
this._millisStart = -1;
207207
this._recording = false;
208+
this.touchstart = false;
208209
this.touchend = false;
209210

210211
// States used in the custom random generators

src/events/mouse.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -657,21 +657,24 @@ p5.prototype._onmousedown = function(e) {
657657
this._setMouseButton(e);
658658
this._updateNextMouseCoords(e);
659659

660+
// _ontouchstart triggers first and sets this.touchstart
661+
if (this.touchstart) {
662+
return;
663+
}
664+
660665
if (typeof context.mousePressed === 'function') {
661666
executeDefault = context.mousePressed(e);
662667
if (executeDefault === false) {
663668
e.preventDefault();
664669
}
665-
// only safari needs this manual fallback for consistency
666-
} else if (
667-
navigator.userAgent.toLowerCase().includes('safari') &&
668-
typeof context.touchStarted === 'function'
669-
) {
670+
} else if (typeof context.touchStarted === 'function') {
670671
executeDefault = context.touchStarted(e);
671672
if (executeDefault === false) {
672673
e.preventDefault();
673674
}
674675
}
676+
677+
this.touchstart = false;
675678
};
676679

677680
/**

src/events/touch.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,7 @@ p5.prototype._ontouchstart = function(e) {
134134
if (executeDefault === false) {
135135
e.preventDefault();
136136
}
137-
// only safari needs this manual fallback for consistency
138-
} else if (
139-
navigator.userAgent.toLowerCase().includes('safari') &&
140-
typeof context.mousePressed === 'function'
141-
) {
142-
executeDefault = context.mousePressed(e);
143-
if (executeDefault === false) {
144-
e.preventDefault();
145-
}
137+
this.touchstart = true;
146138
}
147139
};
148140

0 commit comments

Comments
 (0)