/
index.js
153 lines (126 loc) · 3.26 KB
/
index.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
const channel = location.pathname.slice(1) || 'public'
const apiUrl = `wss://${location.host}/_api/`
const pieceClass = 'js-piece'
const pieces = document.querySelectorAll(`.${pieceClass}`);
const ws = new WebSocket(apiUrl)
let highestZ = 0
let dragging = false
let element = null
let wsConnected = false
const radius = 40
const positions = {}
const overlay = document.getElementById('overlay')
function enable () {
overlay.style.display = 'none'
}
function disable () {
overlay.style.display = 'block'
}
ws.addEventListener('open', () => {
wsConnected = true
ws.send(JSON.stringify(['channel', channel]))
enable()
})
ws.addEventListener('close', disable)
const handlers = {
positions ({positions}) {
Object.entries(positions).forEach(([id, position]) => {
move(document.getElementById(id), position)
})
},
position ({position}) {
update(position)
},
noemi () {
toggleHomo('no homo')
}
}
ws.addEventListener('message', ({data}) => {
const message = JSON.parse(data)
const handler = handlers[message.type]
handler && handler(message)
})
function upload (positions) {
ws.send(JSON.stringify(['position', ...positions]))
}
function getCoordsFromEvent ({clientX, clientY, touches}) {
if (touches) {
const {
clientX,
clientY
} = touches[0]
console.log({clientX, clientY, radius})
return {
x: clientX - radius,
y: clientY
}
}
return {
x: clientX - radius,
y: clientY - radius
}
}
function move (element, {x, y, z}) {
element.style.position = 'absolute'
element.style.left = `${x}px`
element.style.top = `${y}px`
element.style.zIndex = z
if (z > highestZ) highestZ = z
}
function moveAndSet ({x, y}) {
const position = {
x,
y,
z: element.style.zIndex
}
move(element, position)
positions[element.id] = position
}
function update ([id, x, y, z]) {
positions[id] = {x, y, z}
pieces.forEach(piece => {
position = positions[piece.id]
position && move(piece, position)
})
}
const handleDown = event => {
if (!event.target.classList.contains(pieceClass)) return
document.body.style.overflow = 'hidden'
element = event.target
dragging = true
element.style.position = 'absolute'
moveAndSet(getCoordsFromEvent(event))
element.style.zIndex = highestZ++
}
const handleMove = event => {
if (!dragging || !element) return
moveAndSet(getCoordsFromEvent(event))
}
const handleUp = event => {
const {id} = element
const {x, y, z} = positions[id]
document.body.style.overflow = 'initial'
dragging = false
element = null
upload([id, x, y, z])
}
document.body.addEventListener('mousedown', handleDown)
document.body.addEventListener('mousemove', handleMove)
document.body.addEventListener('mouseup', handleUp)
document.body.addEventListener('touchstart', handleDown)
document.body.addEventListener('touchmove', handleMove)
document.body.addEventListener('touchend', handleUp)
function createTyper (word, fn) {
let typed = []
window.addEventListener('keydown', event => {
typed.push(event.key)
typed = typed.slice(-word.length)
if (typed.join('') === word) fn()
})
}
function toggleHomo (nohomo) {
document.body.classList.toggle('gay')
nohomo || ws.send('["noemi"]')
}
createTyper('gay', toggleHomo)
createTyper('noemi', toggleHomo)