-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_js.js
252 lines (197 loc) · 6.52 KB
/
index_js.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
// Hide all ongoing feature demos to prevent lag
const hideEveryting = (callback) => {
$("#timekeep-gif").slideUp(() => {
$("#timekeep-animation").slideUp("fast", () => {
$("#yepchat-gif").slideUp("fast", () => {
$("#yepchat-animation").slideUp("fast", () => {
$("#mst-gif").slideUp("fast", () => {
$("#mst-animation").slideUp("fast",
callback
)
})
}
)
})
})
})
}
let width = 0
let height = 0
// Canvas animation
const setupCanvas = () => {
// set up canvas
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
const distanceNeededToConnect = 200;
width = canvas.width = window.innerWidth;
height = canvas.height = document.body.scrollHeight;
// function to generate random number
function random(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Gets two speeds and returns either a number between the two speeds or a number between the negative of the two speeds
function randomSpeed(minSpeed, maxSpeed) {
const isPositive = Math.random() > 0.5 ? 1 : -1
return isPositive * (Math.floor(Math.random() * (maxSpeed - minSpeed + 1)) + minSpeed);
}
class Ball {
constructor(x, y, velX, velY, color, size) {
this.x = x;
this.y = y;
this.velX = velX;
this.velY = velY;
this.color = color;
this.size = size;
}
draw() {
ctx.beginPath();
ctx.fillStyle = this.color;
ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI);
ctx.fill();
}
update() {
if ((this.x + this.size) >= width) {
this.velX = -(this.velX);
}
if ((this.x - this.size) <= 0) {
this.velX = -(this.velX);
}
if ((this.y + this.size) >= height) {
this.velY = -(this.velY);
}
if ((this.y - this.size) <= 0) {
this.velY = -(this.velY);
}
this.x += this.velX;
this.y += this.velY;
}
distance(x, y) {
return Math.sqrt(x * x + y * y);
}
// Sees if balls in range and if so, connects them
connectBalls() {
for (const ball of balls) {
const dx = this.x - ball.x;
const dy = this.y - ball.y;
const distance = this.distance(dx, dy)
if (distance <= this.size + ball.size + distanceNeededToConnect) {
ctx.beginPath();
ctx.strokeStyle = this.color;
ctx.moveTo(this.x, this.y);
ctx.lineTo(ball.x, ball.y);
ctx.stroke();
}
}
}
}
const balls = [];
while (balls.length < 25) {
const size = 2;
const ball = new Ball(
// ball position always drawn at least one ball width
// away from the edge of the canvas, to avoid drawing errors
random(0 + size, width - size),
random(0 + size, height - size),
randomSpeed(2, 4),
randomSpeed(2, 4),
'rgb(255, 255, 255)',
size
);
balls.push(ball);
}
function loop() {
ctx.fillStyle = '#191919';
ctx.fillRect(0, 0, width, height);
for (const ball of balls) {
ball.draw();
ball.update();
ball.connectBalls();
}
requestAnimationFrame(loop);
}
loop();
return canvas
}
// Updating height and width to support window resizing
const updateHeight = (canvas) => {
const newHeight = document.body.scrollHeight
console.log(document.body.scrollHeight)
canvas.height = newHeight
height = newHeight
}
const updateWidth = (canvas) => {
const newWidth = window.innerWidth
console.log("changed")
canvas.width = newWidth
width = newWidth
}
// Animation displaying each one of the about-me items one at a time
const descriptionAnimation = (aboutMeArray, currentIndex, speed, canvas) => {
updateHeight(canvas)
if (currentIndex < aboutMeArray.length) {
$(aboutMeArray[currentIndex]).slideDown(speed, () => { descriptionAnimation(aboutMeArray, currentIndex + 1, speed, canvas) });
}
}
$(document).ready(() => {
const canvas = setupCanvas();
descriptionAnimation($("li.about-me"), 0, 500, canvas);
// On resize - update width
$(window).resize(() => { updateWidth(canvas) })
// Timekeep click animation
$("#timekeep").click(() => {
if (!$("#timekeep-animation").is(":visible")) {
hideEveryting(() => {
$("#timekeep-animation").slideDown("fast", () => {
$("#timekeep-gif").slideDown("fast", () => {
updateHeight(canvas)
})
})
});
}
else {
$("#timekeep-gif").slideUp(() => {
$("#timekeep-animation").slideUp("fast", () => {
updateHeight(canvas)
})
})
}
});
// MST click animation
$("#mst").click(() => {
if (!$("#mst-animation").is(":visible")) {
hideEveryting(() => {
$("#mst-animation").slideDown("fast", () => {
$("#mst-gif").slideDown("fast", () => {
updateHeight(canvas)
})
})
})
}
else {
$("#mst-gif").slideUp("fast", () => {
$("#mst-animation").slideUp("fast", () => {
updateHeight(canvas)
})
})
}
});
// Yep Chat click animation
$("#yep-chat").click(() => {
if (!$("#yepchat-animation").is(":visible")) {
hideEveryting(() => {
$("#yepchat-animation").slideDown("fast", () => {
$("#yepchat-gif").slideDown("fast", () => {
updateHeight(canvas)
})
})
})
}
else {
$("#yepchat-gif").slideUp("fast", () => {
$("#yepchat-animation").slideUp("fast", () => {
updateHeight(canvas)
})
})
}
});
});