-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcourses.js
324 lines (291 loc) · 8.39 KB
/
courses.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
var divs = [];
var center;
var scl;
var curr;
function setup() {
createCanvas(window.innerWidth, window.innerHeight)
background(29);
scl = 1;
// for (var i = 0; i < 10; i++) {
// divs[i] = new Course(random(window.innerWidth), random(window.innerHeight))
// }
/*
addNode(classes["/comp/330"], "/comp/330")
addNode(classes["/comp/172"], "/comp/172")
addNode(classes["/comp/241"], "/comp/241")
addNode(classes["/comp/142"], "/comp/142")
addNode(classes["/comp/141"], "/comp/141")
addNode(classes["/comp/231"], "/comp/231")
divs[0].link(divs[1])
divs[1].link(divs[4])
divs[5].link(divs[4])
divs[2].link(divs[3])
divs[3].link(divs[4])
divs[5].link(divs[0])
divs[0].link(divs[3])*/
center = createVector(window.innerWidth / 2, window.innerHeight / 2);
// for (let i = 0; i < divs.length - 1; i++) {
// divs[i].link(divs[i + 1])
// }
}
function draw() {
background(52)
//scale(scl);
for (let d of divs) {
//d.attract(); //attracts divs to the center of mass
d.chain(); //moves divs based on links
d.avoid(); //avoid each other
}
for (let d of divs) {
d.update(); //update in separate loop to finish calculations before updating
d.show(); //display elements
}
if(divs.length > 0){
centerOfGravity() //calculate average position of all, giving center of mass
}
}
function windowResized(){
resizeCanvas(windowWidth, windowHeight);
}
class Course {
constructor(x, y, data, name) {
this.data = data; //object that holds class information
this.i = divs.length; //current index ##### needs testing
this.name = name;
this.div = createDiv(name.replaceAll("/", " ")); //creates div with p5
this.div.class("course"); //gives it class course for css
this.div.id(this.i)
this.div.mouseClicked(description);
//this.div.mouseOut(hide);
//init physics
this.pos = createVector(x, y);
this.vel = createVector(0, 0)
this.acc = createVector(0, 0);
this.friction = 0.7
this.radius = 42;
//if mouse is over and dragging it
this.drag = false;
//link lengths
this.length = 200;
//if connected to another div
this.isConnected = false;
//array of connected divs
this.connected = [];
this.prerequisites = [];
this.taken = false;
if(this.taken){
this.div.elt.style.background = "var(--blue)"
this.div.elt.style.color = "var(--light)"
}else if(this.data.description.includes("offered")){
this.div.elt.style.background = "var(--rare)"
this.div.elt.style.color = "var(--light)"
}else if(this.data.semester.includes(",")){
this.div.elt.style.background = "var(--both)"
this.div.elt.style.color = "var(--dark)"
}else if(this.data.semester.includes("Spring")){
this.div.elt.style.background = "var(--spring)"
this.div.elt.style.color = "var(--dark)"
}else if(this.data.semester.includes("Fall")){
this.div.elt.style.background = "var(--fall)"
this.div.elt.style.color = "var(--light)"
}else if(this.data.semester.includes("Summer")){
this.div.elt.style.background = "var(--summer)"
this.div.elt.style.color = "var(--dark)"
}
}
update() {
//physics loop
this.vel.mult(this.friction)
this.acc.mult(this.friction);
if (this.drag) {
this.pos = createVector(mouseX, mouseY)
}
this.vel.add(this.acc);
//dampen small motion, reduce jitter
if (this.vel.mag() > 0.3) {
this.pos.add(this.vel);
}
//reset acceleration
this.acc.mult(0)
}
show() {
//sets css position of div
this.div.position((this.pos.x - this.radius) * scl, (this.pos.y - this.radius) * scl);
if(this.isConnected){
for(let p of this.prerequisites){
drawArrow(p.pos, p5.Vector.sub(this.pos, p.pos), color(87, 115, 153))
}
}
}
avoid() {
//loop through all divs
for (let d of divs) {
//get distance from each other
let dist = this.pos.dist(d.pos);
//if meets threshhold force in opposite direction
if (dist < this.length && dist > 0.0001) {
let force = p5.Vector.sub(this.pos, d.pos)
force.setMag(this.radius / dist);
force.mult(1);
this.acc.add(force);
}
}
}
attract() {
//generate force towards center of mass
let force = p5.Vector.sub(center, this.pos)
if (this.pos.dist(center) < 0) {
force.setMag(0);
}
force.mult(this.pos.dist(center))
force.mult(0.00001)
this.acc.add(force)
}
chain() {
//check if connected
if (this.isConnected) {
//for all that its connected to
for (let d of this.connected) {
//calculate target
let target = p5.Vector.sub(this.pos, d.pos);
target.setMag(this.length);
target.add(d.pos)
//spring force
let force = p5.Vector.sub(target, this.pos);
force.mult(0.005);
this.acc.add(force);
//draw lines
/*
drawArrow(this.pos, d.pos, color(255,255,0))
line(d.pos.x * scl, d.pos.y * scl, this.pos.x * scl, this.pos.y * scl);
ellipse(target.x * scl, target.y * scl, 20 * scl, 20 * scl);
*/
}
}
}
link(d) {
//sets connected to true, adds other div to array of connected divs
d.isConnected = true;
this.isConnected = true;
this.connected.push(d);
d.connected.push(this);
this.prerequisites.push(d);
}
populate(){
//adds all prerequisite nodes
for(let p of this.data.prerequisites){
for(let d of divs){
if(p != d.name){
let n = addNode(classes[p], p)
this.link(n)
}else{
this.link(d)
}
}
}
}
checkTaken(){
if(this.taken){
this.div.elt.style.background = "var(--blue)"
this.div.elt.style.color = "var(--light)"
}else if(this.data.description.includes("offered")){
this.div.elt.style.background = "var(--rare)"
this.div.elt.style.color = "var(--light)"
}else if(this.data.semester.includes(",")){
this.div.elt.style.background = "var(--both)"
this.div.elt.style.color = "var(--dark)"
}else if(this.data.semester.includes("Spring")){
this.div.elt.style.background = "var(--spring)"
this.div.elt.style.color = "var(--light)"
}else if(this.data.semester.includes("Fall")){
this.div.elt.style.background = "var(--fall)"
this.div.elt.style.color = "var(--light)"
}else if(this.data.semester.includes("Summer")){
this.div.elt.style.background = "var(--summer)"
this.div.elt.style.color = "var(--dark)"
}
}
}
function centerOfGravity() {
//get average position
var average = createVector();
for (let d of divs) {
average.add(d.pos);
}
average.mult(1 / divs.length);
center = average;
}
//get mouse factored in scale, sets drag to true and position matches mouse
function mousePressed() {
let m = createVector(mouseX * scl, mouseY * scl);
for (let d of divs) {
if (d.pos.dist(m) < d.radius) {
d.drag = true;
}
}
}
function mouseReleased() {
let m = createVector(mouseX * scl, mouseY * scl);
for (let d of divs) {
if (d.pos.dist(m) < d.radius) {
d.drag = false;
}
}
}
//scale
/*
function mouseWheel(event) {
scl += 0.02 * event.delta;
}*/
function description(event){
let d = document.getElementById('description');
d.style.display = "block";
let selection = event.target.innerText;
let output = "/" + selection.replace(/\s/gi, "/");
d.innerHTML = classes[output].course +"<br><br>" + classes[output].description + "<br><br> Semester: <br>" + classes[output].semester.toString() + "<br><br>";
var temp = document.getElementsByTagName("template")[0];
var clon = temp.content.cloneNode(true);
curr = event.target.id;
d.appendChild(clon);
document.querySelector("#description > label > input[type=checkbox]").checked = divs[curr].taken;
}
function hide(){
let d = document.getElementById('description');
d.style.display = "none";
}
function addNode(data, name){
let added = false;
let existingNode;
for(let d of divs){
if(d.name == name){
added = true;
existingNode = d;
}
}
if(!added){
let c = new Course(random(200) + window.innerWidth/2 - 100, random(200) + window.innerHeight/2 - 100, data, name)
divs.push(c)
c.populate()
return c;
}else{
return existingNode;
}
}
function drawArrow(base, vec, myColor) {
push();
stroke(myColor);
strokeWeight(3);
fill(myColor);
translate(base.x, base.y);
vec.mult(0.7);
line(0, 0, vec.x, vec.y);
rotate(vec.heading());
let arrowSize = 7;
translate(vec.mag() - arrowSize, 0);
triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
pop();
}
function toggle(event){
divs[curr].taken = event.target.checked
divs[curr].checkTaken();
}