Skip to content

Commit

Permalink
Merge pull request #6738 from RandomGamingDev/touchended-fix
Browse files Browse the repository at this point in the history
Fix for touchEnded triggers twice among other issues with touchEnded/mouseReleased on mobile
  • Loading branch information
davepagurek authored Jan 23, 2024
2 parents 9f778a8 + 87d5fe5 commit 9ef898f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/core/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ class p5 {
};
this._millisStart = -1;
this._recording = false;
this.touchend = false;

// States used in the custom random generators
this._lcg_random_state = null;
Expand Down
7 changes: 7 additions & 0 deletions src/events/mouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,12 @@ p5.prototype._onmouseup = function(e) {
const context = this._isGlobal ? window : this;
let executeDefault;
this._setProperty('mouseIsPressed', false);

// _ontouchend triggers first and sets this.touchend
if (this.touchend) {
return;
}

if (typeof context.mouseReleased === 'function') {
executeDefault = context.mouseReleased(e);
if (executeDefault === false) {
Expand All @@ -742,6 +748,7 @@ p5.prototype._onmouseup = function(e) {
e.preventDefault();
}
}
this.touchend = false;
};

p5.prototype._ondragend = p5.prototype._onmouseup;
Expand Down
6 changes: 1 addition & 5 deletions src/events/touch.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,7 @@ p5.prototype._ontouchend = function(e) {
if (executeDefault === false) {
e.preventDefault();
}
} else if (typeof context.mouseReleased === 'function') {
executeDefault = context.mouseReleased(e);
if (executeDefault === false) {
e.preventDefault();
}
this.touchend = true;
}
};

Expand Down

0 comments on commit 9ef898f

Please sign in to comment.