Created
October 4, 2012 16:12
-
-
Save kejyun/3834662 to your computer and use it in GitHub Desktop.
Pad Touch Event
This file contains hidden or 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 Touch = { | |
startPos: null, | |
endPos: null, | |
init: function (elementID) { | |
document.getElementById(elementID) | |
.addEventListener('touchstart', | |
function (event) { | |
Touch.start(event); | |
}, false); | |
document.getElementById(elementID) | |
.addEventListener('touchmove', | |
function (event) { | |
Touch.update(event); | |
}, false); | |
document.getElementById(elementID) | |
.addEventListener('touchend', | |
function (event) { | |
Touch.stop(event); | |
}, false); | |
}, | |
start: function (event) { | |
var touch = event.targetTouches[0]; | |
this.startPos = { | |
x: touch.pageX, | |
y: touch.pageY | |
}; | |
}, | |
update: function (event) { | |
var touch = event.targetTouches[0]; | |
// TODO: record gesture | |
}, | |
stop: function (event) { | |
var touch = event.changedTouches[0]; | |
this.endPos = { | |
x: touch.pageX, | |
y: touch.pageY | |
}; | |
var gesture = this.analyseGesture(); | |
alert("The gesture is: " + gesture); | |
}, | |
analyseGesture: function () { | |
if (this.startPos && this.endPos) { | |
if (this.endPos.x > this.startPos.x + 200) return "swipe right"; | |
else if (this.endPos.x < this.startPos.x - 200) return "swipe left"; | |
else return "unknown"; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment