Skip to content

Commit 78fe510

Browse files
author
Lauren McCarthy
authored
Merge pull request #1471 from mattdesl/fix-mouse-touch-handling
Change mouseX/mouseY handling
2 parents 9159a9f + 0df8649 commit 78fe510

File tree

3 files changed

+39
-33
lines changed

3 files changed

+39
-33
lines changed

src/core/core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,9 @@ var p5 = function(sketch, node, sync) {
345345
}
346346

347347
this._setProperty('frameCount', this.frameCount + 1);
348+
this.redraw();
348349
this._updateMouseCoords();
349350
this._updateTouchCoords();
350-
this.redraw();
351351
this._frameRate = 1000.0/(now - this._lastFrameTime);
352352
this._lastFrameTime = now;
353353
}

src/events/mouse.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@ var p5 = require('../core/core');
1313
var constants = require('../core/constants');
1414

1515
/*
16-
* These are helper vars that store the mouseX and mouseY vals
17-
* between the time that a mouse event happens and the next frame
18-
* of draw. This is done to deal with the asynchronicity of event
19-
* calls interacting with the draw loop. When a mouse event occurs
20-
* the _nextMouseX/Y vars are updated, then on each call of draw, mouseX/Y
21-
* and pmouseX/Y are updated using the _nextMouseX/Y vals.
16+
* This is a flag which is false until the first time
17+
* we receive a mouse event. The pmouseX and pmouseY
18+
* values will match the mouseX and mouseY values until
19+
* this interaction takes place.
2220
*/
23-
p5.prototype._nextMouseX = 0;
24-
p5.prototype._nextMouseY = 0;
21+
p5.prototype._hasMouseInteracted = false;
2522

2623
/**
2724
* The system variable mouseX always contains the current horizontal
@@ -312,27 +309,32 @@ p5.prototype.mouseIsPressed = false;
312309
p5.prototype.isMousePressed = false; // both are supported
313310

314311
p5.prototype._updateNextMouseCoords = function(e) {
312+
var x = this.mouseX;
313+
var y = this.mouseY;
315314
if(e.type === 'touchstart' ||
316315
e.type === 'touchmove' ||
317316
e.type === 'touchend' || e.touches) {
318-
this._setProperty('_nextMouseX', this._nextTouchX);
319-
this._setProperty('_nextMouseY', this._nextTouchY);
320-
} else {
321-
if(this._curElement !== null) {
322-
var mousePos = getMousePos(this._curElement.elt, e);
323-
this._setProperty('_nextMouseX', mousePos.x);
324-
this._setProperty('_nextMouseY', mousePos.y);
325-
}
317+
x = this.touchX;
318+
y = this.touchY;
319+
} else if(this._curElement !== null) {
320+
var mousePos = getMousePos(this._curElement.elt, e);
321+
x = mousePos.x;
322+
y = mousePos.y;
326323
}
324+
this._setProperty('mouseX', x);
325+
this._setProperty('mouseY', y);
327326
this._setProperty('winMouseX', e.pageX);
328327
this._setProperty('winMouseY', e.pageY);
328+
if (!this._hasMouseInteracted) {
329+
// For first draw, make previous and next equal
330+
this._updateMouseCoords();
331+
this._setProperty('_hasMouseInteracted', true);
332+
}
329333
};
330334

331335
p5.prototype._updateMouseCoords = function() {
332336
this._setProperty('pmouseX', this.mouseX);
333337
this._setProperty('pmouseY', this.mouseY);
334-
this._setProperty('mouseX', this._nextMouseX);
335-
this._setProperty('mouseY', this._nextMouseY);
336338
this._setProperty('pwinMouseX', this.winMouseX);
337339
this._setProperty('pwinMouseY', this.winMouseY);
338340
};

src/events/touch.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,12 @@
1010
var p5 = require('../core/core');
1111

1212
/*
13-
* These are helper vars that store the touchX and touchY vals
14-
* between the time that a mouse event happens and the next frame
15-
* of draw. This is done to deal with the asynchronicity of event
16-
* calls interacting with the draw loop. When a touch event occurs
17-
* the _nextTouchX/Y vars are updated, then on each call of draw, touchX/Y
18-
* and ptouchX/Y are updated using the _nextMouseX/Y vals.
13+
* This is a flag which is false until the first time
14+
* we receive a touch event. The ptouchX and ptouchY
15+
* values will match the touchX and touchY values until
16+
* this interaction takes place.
1917
*/
20-
p5.prototype._nextTouchX = 0;
21-
p5.prototype._nextTouchY = 0;
18+
p5.prototype._hasTouchInteracted = false;
2219

2320
/**
2421
* The system variable touchX always contains the horizontal position of
@@ -77,16 +74,18 @@ p5.prototype.touches = [];
7774
p5.prototype.touchIsDown = false;
7875

7976
p5.prototype._updateNextTouchCoords = function(e) {
77+
var x = this.touchX;
78+
var y = this.touchY;
8079
if(e.type === 'mousedown' ||
8180
e.type === 'mousemove' ||
8281
e.type === 'mouseup' || !e.touches) {
83-
this._setProperty('_nextTouchX', this._nextMouseX);
84-
this._setProperty('_nextTouchY', this._nextMouseY);
82+
x = this.mouseX;
83+
y = this.mouseY;
8584
} else {
8685
if(this._curElement !== null) {
8786
var touchInfo = getTouchInfo(this._curElement.elt, e, 0);
88-
this._setProperty('_nextTouchX', touchInfo.x);
89-
this._setProperty('_nextTouchY', touchInfo.y);
87+
x = touchInfo.x;
88+
y = touchInfo.y;
9089

9190
var touches = [];
9291
for(var i = 0; i < e.touches.length; i++){
@@ -95,13 +94,18 @@ p5.prototype._updateNextTouchCoords = function(e) {
9594
this._setProperty('touches', touches);
9695
}
9796
}
97+
this._setProperty('touchX', x);
98+
this._setProperty('touchY', y);
99+
if (!this._hasTouchInteracted) {
100+
// For first draw, make previous and next equal
101+
this._updateTouchCoords();
102+
this._setProperty('_hasTouchInteracted', true);
103+
}
98104
};
99105

100106
p5.prototype._updateTouchCoords = function() {
101107
this._setProperty('ptouchX', this.touchX);
102108
this._setProperty('ptouchY', this.touchY);
103-
this._setProperty('touchX', this._nextTouchX);
104-
this._setProperty('touchY', this._nextTouchY);
105109
};
106110

107111
function getTouchInfo(canvas, e, i) {

0 commit comments

Comments
 (0)