Friday, July 8, 2011

iPhone/iPad/android touch event

I have been looking for ways of detecting touch screen events on iPhone comparable to mouse events on a regular computer. I found this today and think it's wonderful. My game now can interact with user on iPhone.

On iPhone/iPad/android, can use the follow replacements for mouse events:
mousedown -> touchstart
mousemove -> touchmove
mouseup -> touchend

In mouse/touch events, obtain page location by:
me.vSelectCanvas.addEventListener('mousedown', function(e) {
if (e.which == 3) { return; } // do nothing for right mouse button.
var mouse_x = e.pageX - this.offsetLeft,
mouse_y = e.pageY - this.offsetTop;
// ...
}, false);

me.vSelectCanvas.addEventListener('touchstart', function(e) {
if (e.touches.length == 1) {
var mouse_x = e.touches[0].pageX - this.offsetLeft,
mouse_y = e.touches[0].pageY - this.offsetTop;
// ...
}
}, false);

To detect browser agent type (regular browser or smart phone) and reset window size:
  var D=navigator.userAgent.toLowerCase();
var x=(D.indexOf("android")!=-1)||(D.indexOf("iphone")!=-1)||(D.indexOf("ipad")!=-1);
var d=x?window.innerWidth:900;
var B=x?window.innerHeight:550;
(See code here)

And here's information on How to get iPad/iPhone screen dimensions in javascript.

1 comment:

salenayoungs said...

That’s a great site you folks have been carrying out there.

ipad 32gb

Blog Archive

Followers