Wednesday, January 19, 2011

BlackBerry webkit javascript touch events


var lastX=0;
var lastY=0;
document.ontouchstart = function(event) {
var touchEvent = event.changedTouches[0];
lastX = touchEvent.pageX;
lastY = touchEvent.pageY;
}

document.ontouchmove = function(event) {
event.preventDefault();
}

document.ontouchend = function(event) {
var touchEvent = event.changedTouches[0];
var deltaX = lastX - touchEvent.pageX;
var deltaY = lastY - touchEvent.pageY;
alert("deltaX: " + deltaX + " deltaY: " + deltaY);
}


For android and iPhone, it can be

startPoint.x = e.touches[0].pageX;
startPoint.y = e.touches[0].pageY;

General codes:

if(!e['touches']) e['touches'] = e.changedTouches;

startPoint.x = e.touches[0].pageX;
startPoint.y = e.touches[0].pageY;

No comments: