I am programming a little page using jquery.hammer.js and I have having problems getting the x and y coordinates of a touch event.
my code is as follows:
var x1, y1;
var element = document.getElementById("myDIV")
$(element).hammer().on("touch", function(e){
x1 = e.clientX
y1 = e.clientY
document.getElementById("resultDIV").innerHTML = x1 + " " + y1
}
but all I get is "undefined undefined" as my result.
i have everything i can think of, any help is much appreciated.
e.gesture
is deprecated now.
With the latest version of Hammer.js, you should use
var tapX, tapY;
tapX = e.center.x;
tapY = e.center.y;
);
at the end.;
) in the end of each statement
.You can find the information you want from the documentation on GitHub.
Update:
I've analyzed the event
object and found the gesture
property of this object. The gesture
contains another property, which is called center
. And finally you can get the X and Y coordinates from this property by using pageX
and pageY
.
var x1, y1;
x1 = e.gesture.center.pageX;
y1 = e.gesture.center.pageY;
Here is the working demo of your example in JSFiddle.
And also if you use multi-touch
, you can get the coordinates of each touch by using the touches
array of gesture
property. The other property names remain the same (pageX, pageY
).
var x1, y1;
x1 = e.gesture.touches[0].pageX;
y1 = e.gesture.touches[0].pageY;
Here is the second demo of your example using touches
array to get coordinates for only the first touch.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With