-
Notifications
You must be signed in to change notification settings - Fork 1
/
phone.html
192 lines (154 loc) · 7.19 KB
/
phone.html
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
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Splash</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
(function (window, document) {
function Shake() {
//feature detect
this.hasDeviceMotion = 'ondevicemotion' in window;
//default velocity threshold for shake to register
this.threshold = 5;
//use date to prevent multiple shakes firing
this.lastTime = new Date();
//accelerometer values
this.lastX = null;
this.lastY = null;
this.lastZ = null;
//create custom event
this.event = document.createEvent('Event');
this.event.initEvent('shake', true, true);
}
//reset timer values
Shake.prototype.reset = function () {
this.lastTime = new Date();
this.lastX = null;
this.lastY = null;
this.lastZ = null;
};
console.log(this.hasDeviceMotion);
$('body').append("blah:" + this.hasDeviceMotion);
//start listening for devicemotion
Shake.prototype.start = function () {
this.reset();
if (this.hasDeviceMotion) { window.addEventListener('devicemotion', this, false); }
};
//stop listening for devicemotion
Shake.prototype.stop = function () {
if (this.hasDeviceMotion) { window.removeEventListener('devicemotion', this, false); }
this.reset();
};
var sumx = 0.0, sumy = 0.0, sumz = 0.0, count = 0;
//calculates if shake did occur
Shake.prototype.devicemotion = function (e) {
var current = e.accelerationIncludingGravity,
currentTime,
timeDifference,
deltaX = 0,
deltaY = 0,
deltaZ = 0;
if ((this.lastX === null) && (this.lastY === null) && (this.lastZ === null)) {
this.lastX = current.x;
this.lastY = current.y;
this.lastZ = current.z;
return;
}
deltaX = Math.abs(this.lastX - current.x);
deltaY = Math.abs(this.lastY - current.y);
deltaZ = Math.abs(this.lastZ - current.z);
if (((deltaX > this.threshold) && (deltaY > this.threshold)) || ((deltaX > this.threshold) && (deltaZ > this.threshold)) || ((deltaY > this.threshold) && (deltaZ > this.threshold))) {
//calculate time in milliseconds since last shake registered
currentTime = new Date();
timeDifference = currentTime.getTime() - this.lastTime.getTime();
if (timeDifference > 1000) {
var text2;
sumx += deltaX;
sumy += deltaY;
sumz += deltaZ;
count++;
if (current.z > 4) {
text2 = "LEFT SLASH!";
}
else if (current.z < -3) {
text2 = "RIGHT SLASH!";
}
else {
text2 = "FORWARD SLASH!";
}
$(".text").text("" + (sumx/count) + "\n" + (sumy/count) + "\n" + (sumz/count) + "\n\n" + current.x + "\n" + current.y + "\n" + current.z + "\n" + text2 + "\n");
window.dispatchEvent(this.event);
this.lastTime = new Date();
}
}
};
//event handler
Shake.prototype.handleEvent = function (e) {
if (typeof (this[e.type]) === 'function') {
return this[e.type](e);
}
};
//create a new instance of shake.js.
var myShakeEvent = new Shake();
myShakeEvent.start();
}(window, document));
</script>
<script>
$(document).ready(function(){
$displayCanvas = $('#displayCanvas');
var canvas = document.getElementById('screen');
var $canvas = $(canvas);
var ctx = canvas.getContext('2d');
var xPos;
var yPos;
var prevX;
var prexY;
var crosshair;
var col;
var createCrosshair = function(xPos, yPos){
}
var deleteCrosshair = function(prevX, prevY){
}
var socket = io.connect('/')
socket.on('userName', function(color){
col = color;
ctx.fillStyle = color;
ctx.fillRect (0, 0, canvas.width, canvas.height);
console.log(color)
});
$canvas.click(function(e){
var offset = $(this).offset();
var xPos = e.clientX;
var yPos = e.clientY;
crosshair = createCrosshair(xPos, yPos);
//deleteCrosshair(prevX, prevY);
prevX = xPos;
prevY = yPos;
socket.emit('data', {'xPos': xPos/($(window).width()), 'yPos': yPos($(window).height()), 'color': col});
var color;
console.log("x: " + xPos + "y: " + yPos);
});
// shake listener
window.addEventListener('shake', shakeEventDidOccur, false);
function shakeEventDidOccur (e) {
console.log("Shaken!");
socket.emit('paint', {'xPos': xPos, 'yPos': yPos, 'color': color});
$('body').empty();
}
});
</script>
<script src="/socket.io/socket.io.js"></script>
<style> #displayCanvas{
position:absolute;
}
</style>
</head>
<body>
<canvas id="screen" style="position: absolute;
width: 100%;
height: 100%"></canvas>
<div id="displayCanvas" class="displayCanvas">
</div>
</body>
</html>