I would like to obtain the coordinates of a touch event on the ipad in javascript. How would I do this?
I believe this ought to do the trick:
var x = event.targetTouches[0].pageX,
y = event.targetTouches[0].pageY;
Update:
Here is an example:
<!doctype html>
<html lang="en" class="no-js">
<head>
<meta charset="utf-8">
<title>Touch event test</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
</head>
<body>
<script>
$(function() {
var $log = $("#log");
function updateLog(x, y) {
$log.html('X: '+x+'; Y: '+y);
}
document.addEventListener('touchstart', function(e) {
updateLog(e.changedTouches[0].pageX, e.changedTouches[0].pageY);
}, false);
document.addEventListener('touchmove', function(e) {
e.preventDefault();
updateLog(e.targetTouches[0].pageX, e.targetTouches[0].pageY);
}, false);
});
</script>
<div id="log"></div>
</body>
</html>
Try to use:
x = event.originalEvent.pageX;
y = event.originalEvent.pageY;
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