-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtouch.js
More file actions
159 lines (138 loc) · 3.59 KB
/
Copy pathtouch.js
File metadata and controls
159 lines (138 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/************************************************
TOUCH.JS
This hotness brought to you by:
Michael Meyer
mikemeyer@gmail.com
twitter.com/mikemeyer
Steal it, improve it, then let me know.
************************************************/
var touch;
var swiping = false;
var device = {
// Read-only meta info
type: false,
orientation: false,
}
var event = {
// Some code I snarfed from here and modified : http://bit.ly/6s0rhu
fire: function(eventName,payload) {
var e = document.createEvent("Events");
e.initEvent(eventName, true, true);
document.dispatchEvent(e);
},
listen: function(eventName, functionToCall) {
window.addEventListener( eventName, functionToCall, false);
}
}
// Number crunching for single-finger swipes
function Touch(touchObject){
// Initial X & Y
this.x1 = touchObject.pageX;
this.y1 = touchObject.pageY;
// Final X & Y
this.x2 = this.x1;
this.y2 = this.y1;
this.dx = 0;
this.dy = 0;
this.id = touchObject.identifier; // this may come in handy
this.update = function(){
this.x2 = touchObject.pageX;
this.y2 = touchObject.pageY;
this.dx = this.x2 - this.x1;
this.dy = this.y2 - this.y1;
}
}
// Detect orientation
function updateOrientation(){
switch( window.orientation ){
case 0:
x$("body").removeClass("landscape").addClass("portrait");
device.orientation = "portrait";
break;
case 90: // Home button is on the right
case -90: // Home button is on the left
x$("body").addClass("landscape").removeClass("portrait");
device.orientation = "landscape";
break;
case 180:
// What a goofy browser you must be using.
break;
}
if( window.navigator.standalone ){ // Launched from the home screen
setTimeout(function() { window.scrollTo(0, 0) }, 100); // Scroll to top
} else {
setTimeout(function() { window.scrollTo(0, 1) }, 100); // Scroll past the address bar & debug menu
}
}
x$(window).load(function(e){
// TODO: Android support (?)
if( navigator.userAgent.match(/iPhone/i) ){
device.type = "iphone";
} else if( navigator.userAgent.match(/iPod/i) ){
device.type = "ipod";
} else {
device.type = "unknown"; // Desktop computer or other browser/device
}
if( window.navigator.standalone )
x$("body").addClass("standalone");
updateOrientation();
x$(window).orientationchange(updateOrientation);
// The Magic
x$(document)
.touchstart( function(e){
e.preventDefault();
if( e.touches.length == 1 ){
// Aw, that's touching. Ba dum, tsh.
touch = new Touch(e.touches[0]);
event.fire("swipeStart");
}
})
.touchmove(function(e){
e.preventDefault();
// Gesture events take over from here
if( e.touches.length > 1 ){
event.fire("swipeEnd");
touch = false;
}
if( touch ){ // if we started with one finger
touch.update();
event.fire("swipeMove");
}
})
.touchend(function(e){
if( touch ){
event.fire("swipeEnd");
touch = false;
}
})
// 2-finger gestures
.gesturestart(function(e){
})
.gesturechange(function(e){
})
.gestureend(function(e){
})
});
function debug(){
var debugHTML = [];
for( i in this.touch ){
switch( typeof(this.touch[i]) ){
case "function":
debugHTML.push( "function: " + i + "();" );
break;
case "object": // probably an array
debugHTML.push( i + ": " + this.touch[i][this.touch[i].length-1] );
break;
case "number":
debugHTML.push( i + ": " + this.touch[i].toFixed(2) );
break;
default:
debugHTML.push( i + ": " + this.touch[i] );
break;
}
}
x$("#content").html((debugHTML.join("<br>"))+"");
}
//event.listen("swipeStart",debug);
//event.listen("swipeMove",debug);
//event.listen("swipeEnd",function(e){x$(".point").remove();});