Skip to content

Commit e5405bc

Browse files
committed
fixing mouse/touch coordinates update issue(312)
1 parent 9e95299 commit e5405bc

File tree

4 files changed

+74
-40
lines changed

4 files changed

+74
-40
lines changed

lib/p5.js

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! p5.js v0.3.2 August 15, 2014 */
1+
/*! p5.js v0.3.2 August 16, 2014 */
22
var shim = function (require) {
33
window.requestDraw = function () {
44
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback, element) {
@@ -2953,8 +2953,13 @@ var inputmouse = function (require, core, constants) {
29532953
var mousePos = getMousePos(this._curElement.elt, e);
29542954
this._setProperty('pmouseX', this.mouseX);
29552955
this._setProperty('pmouseY', this.mouseY);
2956-
this._setProperty('mouseX', mousePos.x);
2957-
this._setProperty('mouseY', mousePos.y);
2956+
if (e.type === 'touchstart' || e.type === 'touchmove') {
2957+
this._setProperty('mouseX', this.touchX);
2958+
this._setProperty('mouseY', this.touchY);
2959+
} else {
2960+
this._setProperty('mouseX', mousePos.x);
2961+
this._setProperty('mouseY', mousePos.y);
2962+
}
29582963
this._setProperty('pwinMouseX', this.winMouseX);
29592964
this._setProperty('pwinMouseY', this.winMouseY);
29602965
this._setProperty('winMouseX', e.pageX);
@@ -2974,6 +2979,10 @@ var inputmouse = function (require, core, constants) {
29742979
this._setProperty('mouseButton', constants.RIGHT);
29752980
} else {
29762981
this._setProperty('mouseButton', constants.LEFT);
2982+
if (e.type === 'touchstart' || e.type === 'touchmove') {
2983+
this._setProperty('mouseX', this.touchX);
2984+
this._setProperty('mouseY', this.touchY);
2985+
}
29772986
}
29782987
};
29792988
p5.prototype.onmousemove = function (e) {
@@ -2984,13 +2993,15 @@ var inputmouse = function (require, core, constants) {
29842993
context.mouseMoved(e);
29852994
} else if (typeof context.touchMoved === 'function') {
29862995
e.preventDefault();
2996+
this.setTouchPoints(e);
29872997
context.touchMoved(e);
29882998
}
29892999
} else {
29903000
if (typeof context.mouseDragged === 'function') {
29913001
context.mouseDragged(e);
29923002
} else if (typeof context.touchMoved === 'function') {
29933003
e.preventDefault();
3004+
this.setTouchPoints(e);
29943005
context.touchMoved(e);
29953006
}
29963007
}
@@ -3004,6 +3015,7 @@ var inputmouse = function (require, core, constants) {
30043015
context.mousePressed(e);
30053016
} else if (typeof context.touchStarted === 'function') {
30063017
e.preventDefault();
3018+
this.setTouchPoints(e);
30073019
context.touchStarted(e);
30083020
}
30093021
};
@@ -3015,6 +3027,7 @@ var inputmouse = function (require, core, constants) {
30153027
context.mouseReleased(e);
30163028
} else if (typeof context.touchEnded === 'function') {
30173029
e.preventDefault();
3030+
this.setTouchPoints(e);
30183031
context.touchEnded(e);
30193032
}
30203033
};
@@ -3067,48 +3080,56 @@ var inputtouch = function (require, core) {
30673080
p5.prototype.touches = [];
30683081
p5.prototype.setTouchPoints = function (e) {
30693082
var context = this._isGlobal ? window : this;
3070-
context._setProperty('touchX', e.changedTouches[0].pageX);
3071-
context._setProperty('touchY', e.changedTouches[0].pageY);
3072-
var touches = [];
3073-
for (var i = 0; i < e.changedTouches.length; i++) {
3074-
var ct = e.changedTouches[i];
3075-
touches[i] = {
3076-
x: ct.pageX,
3077-
y: ct.pageY
3078-
};
3083+
if (e.type === 'mousedown' || e.type === 'mousemove') {
3084+
context._setProperty('touchX', context.mouseX);
3085+
context._setProperty('touchY', context.mouseY);
3086+
} else {
3087+
context._setProperty('touchX', e.changedTouches[0].pageX);
3088+
context._setProperty('touchY', e.changedTouches[0].pageY);
3089+
var touches = [];
3090+
for (var i = 0; i < e.changedTouches.length; i++) {
3091+
var ct = e.changedTouches[i];
3092+
touches[i] = {
3093+
x: ct.pageX,
3094+
y: ct.pageY
3095+
};
3096+
}
3097+
context._setProperty('touches', touches);
30793098
}
3080-
context._setProperty('touches', touches);
30813099
};
30823100
p5.prototype.ontouchstart = function (e) {
30833101
var context = this._isGlobal ? window : this;
3084-
context.setTouchPoints(e);
3102+
this.setTouchPoints(e);
30853103
if (typeof context.touchStarted === 'function') {
30863104
e.preventDefault();
30873105
context.touchStarted(e);
30883106
} else if (typeof context.mousePressed === 'function') {
30893107
e.preventDefault();
3108+
this.setMouseButton(e);
30903109
context.mousePressed(e);
30913110
}
30923111
};
30933112
p5.prototype.ontouchmove = function (e) {
30943113
var context = this._isGlobal ? window : this;
3095-
context.setTouchPoints(e);
3114+
this.setTouchPoints(e);
30963115
if (typeof context.touchMoved === 'function') {
30973116
e.preventDefault();
30983117
context.touchMoved(e);
30993118
} else if (typeof context.mouseDragged === 'function') {
31003119
e.preventDefault();
3120+
this.updateMouseCoords(e);
31013121
context.mouseDragged(e);
31023122
}
31033123
};
31043124
p5.prototype.ontouchend = function (e) {
31053125
var context = this._isGlobal ? window : this;
3106-
context.setTouchPoints(e);
3126+
this.setTouchPoints(e);
31073127
if (typeof context.touchEnded === 'function') {
31083128
e.preventDefault();
31093129
context.touchEnded(e);
31103130
} else if (typeof context.mouseReleased === 'function') {
31113131
e.preventDefault();
3132+
this.updateMouseCoords(e);
31123133
context.mouseReleased(e);
31133134
}
31143135
};

lib/p5.min.js

Lines changed: 4 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/input/mouse.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,13 @@ define(function (require) {
105105

106106
this._setProperty('pmouseX', this.mouseX);
107107
this._setProperty('pmouseY', this.mouseY);
108-
this._setProperty('mouseX', mousePos.x);
109-
this._setProperty('mouseY', mousePos.y);
108+
if(e.type === 'touchstart' || e.type === 'touchmove') {
109+
this._setProperty('mouseX', this.touchX);
110+
this._setProperty('mouseY', this.touchY);
111+
} else {
112+
this._setProperty('mouseX', mousePos.x);
113+
this._setProperty('mouseY', mousePos.y);
114+
}
110115
this._setProperty('pwinMouseX', this.winMouseX);
111116
this._setProperty('pwinMouseY', this.winMouseY);
112117
this._setProperty('winMouseX', e.pageX);
@@ -128,6 +133,10 @@ define(function (require) {
128133
this._setProperty('mouseButton', constants.RIGHT);
129134
} else {
130135
this._setProperty('mouseButton', constants.LEFT);
136+
if(e.type === 'touchstart' || e.type === 'touchmove') {
137+
this._setProperty('mouseX', this.touchX);
138+
this._setProperty('mouseY', this.touchY);
139+
}
131140
}
132141
};
133142

@@ -192,6 +201,7 @@ define(function (require) {
192201
context.mouseMoved(e);
193202
} else if (typeof context.touchMoved === 'function') {
194203
e.preventDefault();
204+
this.setTouchPoints(e);
195205
context.touchMoved(e);
196206
}
197207
}
@@ -201,6 +211,7 @@ define(function (require) {
201211
context.mouseDragged(e);
202212
} else if (typeof context.touchMoved === 'function') {
203213
e.preventDefault();
214+
this.setTouchPoints(e);
204215
context.touchMoved(e);
205216
}
206217
}
@@ -246,6 +257,7 @@ define(function (require) {
246257
context.mousePressed(e);
247258
} else if (typeof context.touchStarted === 'function') {
248259
e.preventDefault();
260+
this.setTouchPoints(e);
249261
context.touchStarted(e);
250262
}
251263
};
@@ -287,6 +299,7 @@ define(function (require) {
287299
context.mouseReleased(e);
288300
} else if (typeof context.touchEnded === 'function') {
289301
e.preventDefault();
302+
this.setTouchPoints(e);
290303
context.touchEnded(e);
291304
}
292305
};

src/input/touch.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,20 @@ define(function (require) {
4242
p5.prototype.setTouchPoints = function(e) {
4343
var context = this._isGlobal ? window : this;
4444

45-
context._setProperty('touchX', e.changedTouches[0].pageX);
46-
context._setProperty('touchY', e.changedTouches[0].pageY);
45+
if(e.type === 'mousedown' || e.type === 'mousemove'){
46+
context._setProperty('touchX', context.mouseX);
47+
context._setProperty('touchY', context.mouseY);
48+
} else {
49+
context._setProperty('touchX', e.changedTouches[0].pageX);
50+
context._setProperty('touchY', e.changedTouches[0].pageY);
4751

48-
var touches = [];
49-
for(var i = 0; i < e.changedTouches.length; i++){
50-
var ct = e.changedTouches[i];
51-
touches[i] = {x: ct.pageX, y: ct.pageY};
52+
var touches = [];
53+
for(var i = 0; i < e.changedTouches.length; i++){
54+
var ct = e.changedTouches[i];
55+
touches[i] = {x: ct.pageX, y: ct.pageY};
56+
}
57+
context._setProperty('touches', touches);
5258
}
53-
context._setProperty('touches', touches);
5459
};
5560

5661
/**
@@ -62,12 +67,13 @@ define(function (require) {
6267
*/
6368
p5.prototype.ontouchstart = function(e) {
6469
var context = this._isGlobal ? window : this;
65-
context.setTouchPoints(e);
70+
this.setTouchPoints(e);
6671
if(typeof context.touchStarted === 'function') {
6772
e.preventDefault();
6873
context.touchStarted(e);
6974
} else if (typeof context.mousePressed === 'function') {
7075
e.preventDefault();
76+
this.setMouseButton(e);
7177
context.mousePressed(e);
7278
}
7379
};
@@ -81,12 +87,13 @@ define(function (require) {
8187
*/
8288
p5.prototype.ontouchmove = function(e) {
8389
var context = this._isGlobal ? window : this;
84-
context.setTouchPoints(e);
90+
this.setTouchPoints(e);
8591
if (typeof context.touchMoved === 'function') {
8692
e.preventDefault();
8793
context.touchMoved(e);
8894
} else if (typeof context.mouseDragged === 'function') {
8995
e.preventDefault();
96+
this.updateMouseCoords(e);
9097
context.mouseDragged(e);
9198
}
9299
};
@@ -100,12 +107,13 @@ define(function (require) {
100107
*/
101108
p5.prototype.ontouchend = function(e) {
102109
var context = this._isGlobal ? window : this;
103-
context.setTouchPoints(e);
110+
this.setTouchPoints(e);
104111
if (typeof context.touchEnded === 'function') {
105112
e.preventDefault();
106113
context.touchEnded(e);
107114
} else if (typeof context.mouseReleased === 'function') {
108115
e.preventDefault();
116+
this.updateMouseCoords(e);
109117
context.mouseReleased(e);
110118
}
111119
};

0 commit comments

Comments
 (0)