-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
601 lines (487 loc) · 18.8 KB
/
script.js
File metadata and controls
601 lines (487 loc) · 18.8 KB
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/*===========================================================================*/
/* Global variables & constant */
/*=======================================================================*/
let portfolioItems = null;
let currentFocus = 0;
/*===========================================================================*/
/* Loading */
/*=======================================================================*/
// Loading effect which alters the text of the target element
function mainLoading() {
let main = document.getElementsByTagName("main")[0];
decodeSection(main, 500, true);
}
/*===========================================================================*/
/* Menu */
/*=======================================================================*/
// Toggles the menu
function toggleMenu() {
let menuButton = document.getElementById("menu-button");
let navLinks = document.getElementById("nav-links");
menuLoading();
menuButton.classList.toggle("open");
navLinks.classList.toggle("open");
toggleMenuArtifacts();
}
// Toggles the menu artifacts
function toggleMenuArtifacts() {
let menuFrame = document.getElementById("menu-frame");
let overlay = document.getElementById("overlay");
$(menuFrame).fadeToggle(250);
$(overlay).fadeToggle(250);
}
createOverlay = () => {
let overlay = document.createElement("div");
overlay.id = "overlay";
document.body.appendChild(overlay);
}
// Loading effect which alters the text of the target element
function menuLoading() {
let menu = document.getElementsByTagName("menu")[0];
menu.classList.toggle("show");
if (menu.classList.contains("show")) {
decodeSection(menu, 100);
}
}
/*===========================================================================*/
/* Main Frame */
/*=======================================================================*/
// Draws the back frame
function drawBackFrame() {
let windowHeight = getWindowHeight();
let windowWidth = getWindowWidth();
// Setting the width and height of the canvas to the width and height of the
// window
frame = document.getElementById("back-frame");
frame.width = windowWidth;
frame.height = windowHeight;
// The offset is the distance between the edge of the frame and the edge of
// the canvas
let offset = 25;
// The trimming is the vertical and horizontal lengths of the edge
// trimming of the frame
let trimming = 0.05 * windowWidth;
// The bridgeOffset is the horizontal length from the top right corner of
// the frame to the start of the bridge
let bridgeOffset = 0.15 * windowWidth + offset;
// The bridgeLength is the horizontal length of the dip in the top of the
// frame
let bridgeLength = 0.3 * windowWidth;
let ctx = frame.getContext("2d");
ctx.strokeStyle = "white";
ctx.lineWidth = 0.5;
// Outlining the back frame through drawing lines
ctx.moveTo(offset, offset + trimming);
ctx.lineTo(offset, windowHeight - offset - trimming);
ctx.lineTo(offset + trimming, windowHeight - offset);
ctx.lineTo(windowWidth - offset - trimming, windowHeight - offset);
ctx.lineTo(windowWidth - offset, windowHeight - offset - trimming);
ctx.lineTo(windowWidth - offset, offset);
ctx.lineTo(windowWidth - bridgeOffset, offset);
ctx.lineTo(windowWidth - bridgeOffset - trimming, offset + trimming);
ctx.lineTo(windowWidth - bridgeOffset - bridgeLength, offset + trimming);
ctx.lineTo(windowWidth - bridgeOffset - bridgeLength - trimming, offset);
ctx.lineTo(offset + trimming, offset);
ctx.closePath();
ctx.stroke();
}
/*===========================================================================*/
/* Menu Frame */
/*=======================================================================*/
// Draws the menu frame
function drawMenuFrame() {
let windowHeight = getWindowHeight();
let windowWidth = getWindowWidth();
// Setting the width and height of the canvas to the width and height of the
// window
frame = document.getElementById("menu-frame");
frame.width = windowWidth;
frame.height = windowHeight;
// The offset is the distance between the edge of the frame and the edge of
// the canvas
let offset = 25;
// The trimming is the vertical and horizontal lengths of the edge
// trimming of the frame
let trimming = 0.05 * windowWidth;
// The bridgeOffset is the horizontal length from the top right corner of
// the frame to the start of the bridge
let bridgeOffset = 0.15 * windowWidth + offset;
// The bridgeLength is the horizontal length of the dip in the top of the
// frame
let bridgeLength = 0.3 * windowWidth;
let ctx = frame.getContext("2d");
ctx.fillStyle = "#151515";
// Outlining the back frame through drawing lines
ctx.moveTo(windowWidth - bridgeOffset - bridgeLength, windowHeight - offset);
ctx.lineTo(windowWidth - offset - trimming, windowHeight - offset);
ctx.lineTo(windowWidth - offset, windowHeight - offset - trimming);
ctx.lineTo(windowWidth - offset, offset);
ctx.lineTo(windowWidth - bridgeOffset, offset);
ctx.lineTo(windowWidth - bridgeOffset - trimming, offset + trimming);
ctx.lineTo(windowWidth - bridgeOffset - bridgeLength + trimming, offset + trimming);
ctx.lineTo(windowWidth - bridgeOffset - bridgeLength, offset + 2 * trimming);
ctx.closePath();
ctx.fill();
}
/*===========================================================================*/
/* Decode Effect */
/*=======================================================================*/
// Decode effect which alters the text of the target element
function decode(target) {
// If the target element is already being decoded, then return
if (target.classList.contains("decoded")
|| target.classList.contains("in-progress")) {
return;
}
// Add the in-progress class to the target element
target.classList.add("in-progress");
// Sets up decoding effect
let text = target.innerHTML;
target.innerHTML = text.substring(0, 1);
let maxCount = 3;
let delay = 50;
let speed = 1 / (2 * text.length);
// Decodes the text
for (let i = 1; i <= text.length; i++) {
for (let count = 1; count <= maxCount; count++) {
// The character delay is the delay of each character's decoding
const characterDelay = delay + 200 * speed * count;
setTimeout(() => {
let addedChar = randomChar();
if (i == text.length) {
addedChar = "";
}
target.innerHTML = text.substring(0, i) + addedChar;
}, characterDelay);
}
// The delay is the delay between each character being added to the
// target element
delay += 200 * speed * maxCount;
}
// Removes the in-progress class from the target element after the decoding
setTimeout(() => {
target.classList.remove("in-progress");
}, delay + 200 * speed * maxCount * text.length);
}
// Returns a random character
function randomChar() {
let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!.,?";
return chars.charAt(Math.floor(Math.random() * chars.length));
}
/*===========================================================================*/
/* About */
/*=======================================================================*/
// Image Effect
function setupAboutImageEffect() {
let imageEffect = document.getElementById("image-effect");
const maxCharacters = 1000;
let characters = "";
for (let i = 0; i < maxCharacters; i++) {
characters += randomChar();
}
imageEffect.innerHTML = characters;
setInterval(() => {
cycleText(imageEffect);
}, 100);
}
function cycleText(target) {
let text = target.innerHTML;
let length = text.length;
let newText = text.substring(1, length) + text.substring(0, 1);
target.innerHTML = newText;
}
// Radial Circular Slider
async function drawRadialSliders() {
let statsData = await parseJSON("files\\assets\\json\\stats.json");
let stats = document.getElementById("stats");
stats.innerHTML = "";
statsData["items"].forEach((element) => {
let name = element.name;
let percentage = element.percentage;
let data = document.createElement("div");
let dataName = document.createElement("h2");
data.classList.add("data");
dataName.innerHTML = name;
data.appendChild(createRadialSlider(percentage));
data.appendChild(dataName);
data.title = name + ": " + Math.round(percentage * 100) + "%";
stats.appendChild(data);
});
}
// Creates a radial slider given a percentage
function createRadialSlider(percentage) {
let canvas = document.createElement("canvas");
let ctx = canvas.getContext("2d");
let windowWidth = getWindowWidth();
let windowHeight = getWindowHeight();
let width = 0.15 * windowWidth;
let height = 0.15 * windowHeight;
let thickness = Math.min(0.1 * width, 0.1 * height);
let radius = Math.min(0.5 * width, 0.5 * height) - thickness;
canvas.width = width;
canvas.height = height;
let x = 0.5 * width;
let y = 0.5 * height;
ctx.lineWidth = thickness + 10;
ctx.strokeStyle = "#11111155";
ctx.beginPath();
ctx.arc(x, y, radius, 0, 2 * Math.PI);
ctx.stroke();
ctx.lineWidth = thickness;
ctx.strokeStyle = "#ccf";
ctx.beginPath();
ctx.arc(x, y, radius, 1.5 * Math.PI, 2 * Math.PI * percentage - 0.5 * Math.PI);
ctx.stroke();
return canvas;
}
// Toggle Buttons
function toggleButton(button) {
if (!button.classList.contains("active")) {
$("#bio-button").toggleClass("active");
$("#stats-button").toggleClass("active");
toggleView();
}
}
// Toggle View
function toggleView() {
$("#bio").toggle();
$("#stats").toggle();
}
/*===========================================================================*/
/* Portfolio */
/*=======================================================================*/
// Sets up the portfolio items
async function setupPortfolio() {
let parsedPortfolioItems = await parseJSON("files\\assets\\json\\portfolio-items.json");
portfolioItems = [];
parsedPortfolioItems["items"].forEach(item => {
portfolioItems.push(new PortfolioItem(item));
});
}
// Displays the portfolio items
async function displayPortfolio() {
if (portfolioItems == null) {
await setupPortfolio();
}
let portfolioList = document.getElementById("portfolio-list");
let items = getPortfolioDisplayItems();
for (let i = 0; i < items.length; i++) {
button = createPortfolioButton(items[i]);
portfolioList.appendChild(button);
}
recalibrateOnClickEvents();
updateFocusItemDisplay();
}
// Creates a portfolio button
function createPortfolioButton(item) {
let button = document.createElement("button");
button.classList.add("portfolio-item");
button.classList.add("clear-default");
let h2 = document.createElement("h2");
h2.innerText = item.name;
button.appendChild(h2);
return button;
}
// Returns the portfolio items to be displayed in a specific order
function getPortfolioDisplayItems() {
let twoPreviousItem = portfolioItems[(portfolioItems.length - currentFocus - 2) % portfolioItems.length];
let previousItem = portfolioItems[(portfolioItems.length - currentFocus - 1) % portfolioItems.length];
let focusedItem = portfolioItems[currentFocus];
let nextItem = portfolioItems[(currentFocus + 1) % portfolioItems.length];
let twoNextItem = portfolioItems[(currentFocus + 2) % portfolioItems.length];
return [twoPreviousItem, previousItem, focusedItem, nextItem, twoNextItem];
}
// Cycles the portfolio items
function cycleNext() {
let portfolioList = document.getElementById("portfolio-list");
currentFocus = (currentFocus + 1) % portfolioItems.length;
let newNext = portfolioItems[(currentFocus + 2) % portfolioItems.length];
let button = createPortfolioButton(newNext);
let firstChild = portfolioList.firstChild;
firstChild.remove();
portfolioList.appendChild(button);
recalibrateOnClickEvents();
updateFocusItemDisplay();
decode(portfolioList.children[2].firstChild);
}
// Cycles the portfolio items
function cyclePrevious() {
let portfolioList = document.getElementById("portfolio-list");
currentFocus = (portfolioItems.length + currentFocus - 1) % portfolioItems.length;
let newPrevious = portfolioItems[(portfolioItems.length + currentFocus - 2) % portfolioItems.length];
let button = createPortfolioButton(newPrevious);
let lastChild = portfolioList.lastChild;
lastChild.remove();
portfolioList.insertBefore(button, portfolioList.firstChild);
recalibrateOnClickEvents();
updateFocusItemDisplay();
decode(portfolioList.children[2].firstChild);
}
// Recalibrates the onclick events of the portfolio items
function recalibrateOnClickEvents() {
let portfolioList = document.getElementById("portfolio-list");
let portfolioItems = portfolioList.children;
for (let i = 0; i < portfolioItems.length; i++) {
portfolioItems[i].onclick = function() {
if (i < 2) {
cyclePrevious();
} else if (i == 2) {
openPortfolioItem();
} else if (i >= 3) {
cycleNext();
}
}
}
}
// Updates the focus item display
function updateFocusItemDisplay() {
let backgroundImageHolder = document.getElementById("background-image-holder");
console.log(portfolioItems[currentFocus].displayImage);
let image = document.createElement("img");
image.classList.add("portfolio-display-image");
image.src = portfolioItems[currentFocus].displayImage;
image.alt = portfolioItems[currentFocus].displayImageAlt;
image.onload = function() {
backgroundImageHolder.innerHTML = "";
backgroundImageHolder.appendChild(image);
setTimeout( () => {
image.style.opacity = "1";
}, 150);
}
}
// Portfolio Item Class
class PortfolioItem {
constructor(item) {
this.name = item["name"];
this.displayImage = item["display-image"];
this.displayImageAlt = item["display-image-alt"];
this.bannerImage = item["banner-image"];
this.bannerImageAlt = item["banner-image-alt"];
this.description = item["description"];
}
}
// Pop-up
// Opens the portfolio item
function openPortfolioItem() {
let portfolioItem = portfolioItems[currentFocus];
let popUp = document.getElementById("portfolio-item-display");
let banner = document.getElementById("banner");
let name = document.getElementById("name");
let description = document.getElementById("description");
let descriptionLocation = portfolioItem.description;
fetch(descriptionLocation)
.then(response => response.text())
.then(text => description.innerHTML = text)
let image = document.createElement("img");
image.src = portfolioItem.bannerImage;
image.alt = portfolioItem.bannerImageAlt;
banner.innerHTML = "";
banner.appendChild(image);
name.innerText = portfolioItem.name;
$(popUp).fadeIn(500);
decodeSection(popUp, 100, stop = true);
}
// Closes the portfolio item
function closePortfolioItem() {
console.log("close");
let popUp = document.getElementById("portfolio-item-display");
$(popUp).fadeOut(500);
$("#name").removeClass("decoded");
}
/*===========================================================================*/
/* General Functions */
/*=======================================================================*/
// Returns the width of the window
function getWindowWidth() {
return window.innerWidth;
}
// Returns the height of the window
function getWindowHeight() {
return window.innerHeight;
}
// Parses a JSON file given a location
async function parseJSON(location) {
let response = await fetch(location);
let data = await response.json();
return data;
}
// Returns the difference between two strings
function stringDifference(string1, string2){
let difference = "";
string2.split('').forEach(function(value, i) {
if (value != string1.charAt(i))
difference += value;
}
);
return difference;
}
// Decodes a section
function decodeSection(section, delay, stop = false) {
let decodingElements = Array.from(section.getElementsByClassName("decode"));
decodingElements.forEach(element => {
element.classList.add("hidden");
});
for (let i = 0; i < decodingElements.length; i++) {
let element = decodingElements[i];
setTimeout(() => {
element.classList.remove("hidden");
decode(element);
if (stop) {
element.classList.add("decoded");
}
}, delay * i);
}
}
/*===========================================================================*/
/* Event Listeners */
/*=======================================================================*/
function handleOnHoverAnimations(event) {
if (event.target.classList.contains("decode")) {
decode(event.target);
}
}
let page;
// Draws the back frame when the window loads and when the window is resized
window.onload = async () => {
page = document.body.classList[0];
switch (page) {
case "home":
mainLoading();
break;
case "about":
setupAboutImageEffect();
await drawRadialSliders();
break;
case "portfolio":
await displayPortfolio();
break;
}
drawBackFrame();
drawMenuFrame();
createOverlay();
}
window.onresize = () => {
switch (page) {
case "about":
drawRadialSliders();
break;
}
drawBackFrame();
drawMenuFrame();
}
document.addEventListener("mouseover", handleOnHoverAnimations);
switch (document.body.classList[0]) {
case "portfolio":
document.onkeydown = function(event) {
console.log(event.key);
switch (event.key) {
case "ArrowUp":
cyclePrevious();
break;
case "ArrowDown":
cycleNext();
break;
}
}
break;
}