Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining Ipad touchstart coordinates

I would like to obtain the coordinates of a touch event on the ipad in javascript. How would I do this?

like image 735
Mason Avatar asked Jun 03 '11 15:06

Mason


2 Answers

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>
like image 88
simshaun Avatar answered Sep 22 '22 02:09

simshaun


Try to use:

x = event.originalEvent.pageX;
y = event.originalEvent.pageY;
like image 33
Luca Borrione Avatar answered Sep 21 '22 02:09

Luca Borrione