-
Notifications
You must be signed in to change notification settings - Fork 3
/
visualizer.js
251 lines (213 loc) · 5.83 KB
/
visualizer.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
const screenWidth = () => window.innerWidth
const screenHeight = () => window.innerHeight
let catPos = []
let datPos = []
// state or mode
const [CATEGORY_MODE, ORGS_MODE] = ['CATEGORY_MODE', 'ORGS_MODE']
const data = []
const category = []
loadData(json => {
json.forEach(element => {
data.push(element)
if (category.find(value => value.category_name === element.category) === undefined) {
category.push({
category_name: element.category,
bubble_size: 0,
frequency: 1
})
} else { category.find(value => value.category_name === element.category).frequency++ }
})
category.sort((a, b) => b.frequency - a.frequency).forEach((e, index) => {
e.bubble_size = (screenWidth() / 3 * e.frequency / 212)
// need a better way to randomize bubble position without
// any overlaps between bubble
let newPos = [
random(-screenWidth() * 0.4, screenWidth() * 0.4),
random(-screenHeight() * 0.4, screenHeight() * 0.4)
]
if (index > 0) {
for (let j = index - 1; j >= 0; j--) {
while (dist(...newPos, ...catPos[j]) < 2 * category[j].bubble_size) {
newPos = [
random(-screenWidth() * 0.4, screenWidth() * 0.4),
random(-screenHeight() * 0.4, screenHeight() * 0.4)
]
}
}
}
catPos.push(newPos)
})
})
let rot = 0
let trig = [false, 0]
let currState = CATEGORY_MODE
let selectedCategory = category[0]
let node = []
const view = [
screenWidth() / 2,
screenHeight() / 2
]
const getcoord = (x, y) => [x - screenWidth() / 2, y - screenHeight() / 2]
const getMouse = () => getcoord(mouseX, mouseY)
function setup () {
createCanvas(screenWidth(), screenHeight())
stroke(0) // Set line drawing color to white
frameRate(30)
angleMode(DEGREES)
$('#container').hide()
}
const colorsTable = (data) => {
let colors = []
data.forEach((d, index) => {
colors.push([255 - index * 20, 0 + index * 30, 43 + index * 40])
})
return colors
}
function draw () {
background('#eee')
translate(...view)
switch (currState) {
case CATEGORY_MODE:
category.forEach((c, index) => {
push()
fill(...colorsTable(data)[index])
stroke('#333')
const size = c.bubble_size * (1 + (sin(rot) * 0.5)) + 20
ellipse(
...catPos[index],
size,
size
)
pop()
})
rot = (rot + 0.5) % 180
push()
textSize(32)
stroke(0)
fill('#a463ff')
text(
'Select a Category', -(8 * 19),
0
) // how to centering text ?
pop()
break
case ORGS_MODE:
node.forEach((n, index) => {
push()
fill(...colorsTable(data)[category.indexOf(category.find(e => e.category_name === selectedCategory.category_name))])
ellipse(...datPos[index], 10, 10)
pop()
})
break
default:
break
}
drawDesc(...trig)
$('#quit-btn').click(() => {
$('#container').slideUp()
})
}
function mouseClicked () {
let mousePos = getcoord(mouseX, mouseY)
switch (currState) {
case CATEGORY_MODE:
for (let index = 0; index < category.length; index++) {
if (dist(...mousePos, ...catPos[index]) <= category[index].bubble_size * (1 + (sin(rot) * 0.2))) {
selectedCategory = category[index]
currState = ORGS_MODE
data.forEach(e => {
if (e.category === selectedCategory.category_name) {
let tmp = [
random(-screenWidth() * 0.4, screenWidth() * 0.4),
random(-screenHeight() * 0.4, screenHeight() * 0.4)
]
datPos.push(tmp)
node.push(e)
}
})
trig = [false, 0]
break
}
}
break
case ORGS_MODE:
let changeState = true
for (let index = 0; index < datPos.length; index++) {
if (dist(...mousePos, ...datPos[index]) <= 10) {
document.getElementById('title').innerHTML = node[index].name
document.getElementById('desc').innerHTML = node[index].tagline
document.getElementById('web').setAttribute('href', `https://soc-orgs.netlify.com/orgs/${node[index].name.split(' ').join('-')}`)
$('#container').slideDown()
changeState = false
break
}
}
if (changeState) {
trig = [false, 0]
currState = CATEGORY_MODE
node = []
datPos = []
}
break
}
}
function mouseMoved () {
switch (currState) {
case CATEGORY_MODE:
for (let index = 0; index < category.length; index++) {
if (dist(...getMouse(), ...catPos[index]) <= category[index].bubble_size * (1 + (sin(rot) * 0.2))) {
trig = [true, index]
break
} else { trig = [false, 0] }
}
break
case ORGS_MODE:
for (let index = 0; index < datPos.length; index++) {
if (dist(...getMouse(), ...datPos[index]) <= 20) {
trig = [true, index]
break
} else { trig = [false, 0] }
}
break
default:
break
}
}
const drawDesc = (trigger, idx) => {
if (trigger) {
push()
let y2 = getMouse()[1] + 20
let x2 = getMouse()[0] + 20
line(x2, y2, x2 + 100, y2)
pop()
push()
stroke(0)
switch (currState) {
case CATEGORY_MODE:
line(...catPos[idx], x2, y2)
pop()
push()
textSize(12)
noStroke()
text(category[idx]
.category_name
.replace('_', ' ')
.replace('_', ' '),
x2, y2 + 10)
text(category[idx].frequency + ' organizations', x2, y2 + 24)
break
case ORGS_MODE:
line(...datPos[idx], x2, y2)
pop()
push()
textSize(12)
noStroke()
text(node[idx].name,
x2, y2 + 10)
break
default:
break
}
pop()
}
}