Created
July 18, 2014 17:48
-
-
Save hansent/f9b409087856a1eadd1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var $ = require('jquery'); | |
//global variable to keep track of ongoing touches | |
var ongoingTouches = {}; | |
$(function() { | |
var el = document.body; | |
el.addEventListener("touchstart", handleTouchStart, false); | |
el.addEventListener("touchend", handleTouchEnd, false); | |
el.addEventListener("touchcancel", handleTouchCancel, false); | |
el.addEventListener("touchleave", handleTouchEnd, false); | |
el.addEventListener("touchmove", handleTouchMove, false); | |
log("initialized."); | |
}); | |
function handleTouchStart(evt) { | |
var touches = evt.changedTouches; | |
for (var i = 0; i < touches.length; i++) { | |
var touchId = touches[i].identifier; | |
var touchData = { | |
identifier: touches[i].identifier, | |
clientX: touches[i].clientX, | |
clientY: touches[i].clientY | |
}; | |
ongoingTouches[touchId] = touchData; | |
console.log("touchstart:" + i + "...", touchId, touchData); | |
evt.preventDefault(); | |
} | |
} | |
function handleTouchMove(evt) { | |
var touches = evt.changedTouches; | |
for (var i = 0; i < touches.length; i++) { | |
var touchId = touches[i].identifier; | |
var touchData = ongoingTouches[touchId]; | |
if (touchData) { | |
touchData.clientX = touches[i].clientX; | |
touchData.clientY = touches[i].clientY; | |
console.log("continuing touch " + touchId, touchData); | |
} else { | |
console.log("can't figure out which touch to continue"); | |
} | |
evt.preventDefault(); | |
} | |
} | |
function handleTouchEnd(evt) { | |
var touches = evt.changedTouches; | |
for (var i = 0; i < touches.length; i++) { | |
var touchId = touches[i].identifier; | |
var touchData = ongoingTouches[touchId]; | |
if (touchData) { | |
ongoingTouches[touchId] = false; | |
console.log("ended touch", touchId); | |
} else { | |
console.log("can't figure out which touch to end"); | |
} | |
evt.preventDefault(); | |
} | |
} | |
function handleTouchCancel(evt) { | |
var touches = evt.changedTouches; | |
for (var i = 0; i < touches.length; i++) { | |
var touchId = touches[i].identifier; | |
ongoingTouches[touchId] = false; | |
console.log("touchcancel.", touchId); | |
} | |
evt.preventDefault(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment