Skip to content

Commit 4f99be7

Browse files
committed
Fixed jacomyal#161: Multi-touch wrong positions
Had to add the sigma.utils.getOffset function that recursively retrieves the global offset of a DOM element. With this function and the pageX / pageY of the Touch objects, the positions of the points are now good, and the multiu-touch is now fully working.
1 parent 34ade16 commit 4f99be7

File tree

2 files changed

+53
-13
lines changed

2 files changed

+53
-13
lines changed

src/captors/sigma.captors.touch.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@
6262
_target.addEventListener('touchleave', _handleLeave);
6363
_target.addEventListener('touchmove', _handleMove);
6464

65+
function position(e) {
66+
var offset = sigma.utils.getOffset(_target);
67+
68+
return {
69+
x: e.pageX - offset.left,
70+
y: e.pageY - offset.top
71+
};
72+
}
6573

6674

6775

@@ -78,7 +86,9 @@
7886
var x0,
7987
x1,
8088
y0,
81-
y1;
89+
y1,
90+
pos0,
91+
pos1;
8292

8393
_downTouches = e.touches;
8494

@@ -93,18 +103,21 @@
93103
_lastCameraX = _camera.x;
94104
_lastCameraY = _camera.y;
95105

96-
_startTouchX0 = sigma.utils.getX(_downTouches[0]);
97-
_startTouchY0 = sigma.utils.getY(_downTouches[0]);
106+
pos0 = position(_downTouches[0]);
107+
_startTouchX0 = pos0.x;
108+
_startTouchY0 = pos0.y;
98109

99110
break;
100111
case 2:
101112
_camera.isMoving = true;
102113
_touchMode = 2;
103114

104-
x0 = sigma.utils.getX(_downTouches[0]);
105-
y0 = sigma.utils.getY(_downTouches[0]);
106-
x1 = sigma.utils.getX(_downTouches[1]);
107-
y1 = sigma.utils.getY(_downTouches[1]);
115+
pos0 = position(_downTouches[0]);
116+
pos1 = position(_downTouches[1]);
117+
x0 = pos0.x;
118+
y0 = pos0.y;
119+
x1 = pos1.x;
120+
y1 = pos1.y;
108121

109122
_lastCameraX = _camera.x;
110123
_lastCameraY = _camera.y;
@@ -209,6 +222,8 @@
209222
cos,
210223
sin,
211224
end,
225+
pos0,
226+
pos1,
212227
diff,
213228
start,
214229
dAngle,
@@ -230,8 +245,9 @@
230245

231246
switch (_touchMode) {
232247
case 1:
233-
x0 = sigma.utils.getX(_downTouches[0]);
234-
y0 = sigma.utils.getY(_downTouches[0]);
248+
pos0 = position(_downTouches[0]);
249+
x0 = pos0.x;
250+
y0 = pos0.y;
235251

236252
diff = _camera.cameraPosition(
237253
x0 - _startTouchX0,
@@ -255,10 +271,12 @@
255271
}
256272
break;
257273
case 2:
258-
x0 = sigma.utils.getX(_downTouches[0]);
259-
y0 = sigma.utils.getY(_downTouches[0]);
260-
x1 = sigma.utils.getX(_downTouches[1]);
261-
y1 = sigma.utils.getY(_downTouches[1]);
274+
pos0 = position(_downTouches[0]);
275+
pos1 = position(_downTouches[1]);
276+
x0 = pos0.x;
277+
y0 = pos0.y;
278+
x1 = pos1.x;
279+
y1 = pos1.y;
262280

263281
start = _camera.cameraPosition(
264282
(_startTouchX0 + _startTouchX1) / 2 - e.target.width / 2,

src/utils/sigma.utils.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,28 @@
210210
);
211211
};
212212

213+
/**
214+
* Returns the offset of a DOM element.
215+
*
216+
* @param {DOMElement} dom The element to retrieve the position.
217+
* @return {object} The offset of the DOM element (top, left).
218+
*/
219+
sigma.utils.getOffset = function(dom) {
220+
var left = 0,
221+
top = 0;
222+
223+
while (dom) {
224+
top = top + parseInt(dom.offsetTop);
225+
left = left + parseInt(dom.offsetLeft);
226+
dom = dom.offsetParent;
227+
}
228+
229+
return {
230+
top: top,
231+
left: left
232+
};
233+
};
234+
213235
/**
214236
* Simulates a "double click" event.
215237
*

0 commit comments

Comments
 (0)