-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixlracrController.js
205 lines (183 loc) · 4.33 KB
/
pixlracrController.js
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* Controller with Input Controls */
/* Creates window.onload and attaches all functions */
var p;
var debug = true;
window.onload = function() {
// debug
var log = document.querySelector('#debug p');
if(debug) {
document.querySelector('#debug').style.display = 'block';
}
// create one pixl
p = new pixl();
// +++++ KEYBOARD CONTROL +++++
window.onkeydown = function(e) {
//console.log("KeyDown: " + e);
switch (e.keyCode) {
// case 32: // SPACE
case 37: // LEFT
p.left(true);
break;
case 38: // UP
p.up(true);
break;
case 39: // RIGHT
p.right(true);
break;
case 40: // DOWN
p.down(true);
break;
case 187: // +
p.increaseSize();
break;
case 189: // -
p.decreaseSize();
break;
case 190: // .
p.increaseSpeed();
break;
case 188: // ,
p.decreaseSpeed();
break;
};
// suppress default key behaviour
e.preventDefault();
};
window.onkeyup = function(e) {
switch (e.keyCode) {
case 37: // LEFT
p.left(false);
break;
case 38: // UP
p.up(false);
break;
case 39: // RIGHT
p.right(false);
break;
case 40: // DOWN
p.down(false);
break;
}
// suppress default key behaviour
e.preventDefault();
};
// +++++ MOUSE CONTROL +++++
var mouseX = 0;
var mouseY = 0;
window.onmousedown = function(e) {
mouseX = e.clientX;
mouseY = e.clientY;
}
window.onmousemove = function(e) {
// log.textContent = "Mouse move x:" + e.clientX + ", y:" + e.clientY;
// delete all previous directions
p.stop();
// now get the new direction
if(mouseX > e.clientX) {
p.left(true);
} else if(mouseX < e.clientX) {
p.right(true);
}
if(mouseY > e.clientY) {
p.up(true);
} else if(mouseY < e.clientY) {
p.down(true);
}
mouseX = e.clientX;
mouseY = e.clientY;
log.textContent = "Mouse " + p.isMoving();
};
window.onmouseup = function(e) {
// log.textContent = "Touch End";
p.stop();
}
window.onscroll = function(e) {
// Not really well supported in Safari
log.textContent = "Scrolling...";
}
// +++++ TOUCH CONTROL +++++
var touchX = 0;
var touchY = 0;
var touchThreshold = 0; // min px diff to change direction
window.ontouchstart = function(e) {
// log.textContent = "Touch Start";
if(e.targetTouches.length) {
var touch = e.targetTouches[0];
touchX = touch.pageX;
touchY = touch.pageY;
}
}
window.ontouchmove = function(e) {
if(e.targetTouches.length) {
var touch = e.targetTouches[0];
// delete all previous directions
p.stop();
// now get the new direction
if(touchX > (touch.pageX + touchThreshold)) {
p.left(true);
} else if(touchX < (touch.pageX - touchThreshold)) {
p.right(true);
}
if(touchY > (touch.pageY + touchThreshold)) {
p.up(true);
} else if(touchY < (touch.pageY - touchThreshold)) {
p.down(true);
}
touchX = touch.pageX;
touchY = touch.pageY;
}
log.textContent = "Touch " + p.isMoving();
e.preventDefault();
};
window.ontouchend = function(e) {
// log.textContent = "Touch End";
p.stop();
}
// window.ongesturestart = function(e) {
window.ongesturechange = function(e) {
// log.textContent = "Gesture: e.scale = " + e.scale + " e.rotation = " + e.rotation;
// pinch to set size
if(e.scale != 1.0) {
if(e.scale < 1.0) {
p.decreaseSize();
} else {
p.increaseSize();
}
}
/*// rotate to set speed
if(e.rotation < 0) {
p.increaseSpeed();
} else {
p.decreaseSpeed();
}*/
}
// +++++ TILT CONTROL +++++
window.onorientationchange = function(e) {
var orientation;
switch(window.orientation) {
case 0:
orientation += "Portrait";
break;
case -90:
orientation += "Landscape (right, screen turned clockwise)";
break;
case 90:
orientation += "Landscape (left, screen turned counterclockwise)";
break;
case 180:
orientation += "Portrait (upside-down portrait)";
break;
}
log.textContent = "New orientation: " + orientation;
}
// +++++ ANIMATION +++++
// setup animation by creating a timer
var timer = setInterval( function() {
p.move();
// log.textContent += ".";
// log.textContent = ( "x: "+document.querySelector('#pixl').style.left);
// log.textContent+= (" y: "+document.querySelector('#pixl').style.top);
}, 100);
// set the focus on the window to receive all key events
window.focus();
}