Skip to content

Instantly share code, notes, and snippets.

@kejyun
Created October 4, 2012 16:12
Show Gist options
  • Save kejyun/3834662 to your computer and use it in GitHub Desktop.
Save kejyun/3834662 to your computer and use it in GitHub Desktop.
Pad Touch Event
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