This repository has been archived by the owner on Apr 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.js
93 lines (75 loc) · 2.06 KB
/
client.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
var shoe = require('shoe')
var through = require('through')
// specify instrument
var socketUrl = '/sockets' + window.location.pathname
// server socket
var ws = shoe(socketUrl)
var lastMessage = null
var output = through(function(data){
var message = JSON.stringify(data)
if (message != lastMessage){
this.queue(message)
lastMessage = message
}
})
output.pipe(ws)
var disconnectTimer = null
ws.on('data', function(data){
var data = JSON.parse(data)
if (data.color){
document.body.style.backgroundColor = data.color
}
clearTimeout(disconnectTimer)
disconnectTimer = setTimeout(disconnected, 1000)
})
function disconnected(){
document.body.style.backgroundColor = '#C00'
}
var scrollCallback = function(event){
if (document.body.clientHeight > document.body.clientWidth){
current.y = event.pageX / document.body.clientHeight
current.x = event.pageY / document.body.clientWidth
} else {
current.y = event.pageY / document.body.clientHeight
current.x = event.pageX / document.body.clientWidth
}
output.write(current)
event.preventDefault()
};
var startCallback = function(event){
document.documentElement.style.backgroundColor = '#FFF'
window.current.active = true
scrollCallback(event)
event.preventDefault()
}
var endCallback = function(event){
document.documentElement.style.backgroundColor = '#000'
window.current.active = false
output.write(window.current)
event.preventDefault()
};
function prepareDegrees(val) {
//var ret = 0;
//if(Math.min(val, -90) < -90){
// ret = -90;
//} else if (Math.max(val, 90) > 90) {
// ret = 90;
//} else {
// ret = val;
//}
return ((val+360+90) % 360) / 360;
}
// mobile
document.addEventListener('touchstart', startCallback);
document.addEventListener('touchmove', scrollCallback);
document.addEventListener('touchend', endCallback);
// pc
document.addEventListener('mousedown', startCallback);
document.addEventListener('mousemove', scrollCallback);
document.addEventListener('mouseup', endCallback);
window.current = {
x: 0,
y: 0,
z: 0,
active: false
}