Repository navigation
Expand file tree
/
Copy pathScripts.js
More file actions
158 lines (100 loc) · 2.96 KB
/
Copy pathScripts.js
File metadata and controls
158 lines (100 loc) · 2.96 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
const body = document.body;
const button = document.getElementById("glitchButton");
const background = document.querySelector(".background-glitch");
const BACKGROUND_DURATION = 10_000;
const SEVERE_GLITCH_DURATION = 650;
let vhsGlitchTimeout;
let backgroundPhaseTimeout;
let backgroundFlashTimeout;
// =========================================
// RANDOM VHS GLITCH
// =========================================
function triggerGlitch(duration = 150) {
body.classList.add("vhs-glitching");
clearTimeout(vhsGlitchTimeout);
vhsGlitchTimeout = setTimeout(() => {
body.classList.remove("vhs-glitching");
}, duration);
}
function scheduleBackgroundFlash() {
if (!background?.classList.contains("phase-one")) {
return;
}
const delay = 1400 + Math.random() * 2200;
backgroundFlashTimeout = setTimeout(() => {
if (!background.classList.contains("phase-one")) {
return;
}
const flash = Math.random() < 0.5 ? "flash-1" : "flash-2";
background.classList.add(flash);
setTimeout(() => {
background.classList.remove(flash);
scheduleBackgroundFlash();
}, 900);
}, delay);
}
function startBackgroundPhase(phase) {
if (!background) {
return;
}
clearTimeout(backgroundPhaseTimeout);
clearTimeout(backgroundFlashTimeout);
background.classList.remove(
"phase-main",
"phase-one",
"severe-main",
"severe-one",
"flash-1",
"flash-2"
);
background.classList.add(phase);
if (phase === "phase-one") {
scheduleBackgroundFlash();
}
backgroundPhaseTimeout = setTimeout(() => {
const severeClass = phase === "phase-main" ? "severe-main" : "severe-one";
background.classList.add(severeClass);
setTimeout(() => {
startBackgroundPhase(phase === "phase-main" ? "phase-one" : "phase-main");
}, SEVERE_GLITCH_DURATION);
}, BACKGROUND_DURATION);
}
if (background) {
startBackgroundPhase("phase-main");
}
// =========================================
// RANDOM GLITCH LOOP
// =========================================
function randomGlitch() {
// Random chance
const chance = Math.random();
// Only glitch sometimes
if (chance > 0.65) {
const duration =
80 + Math.random() * 250;
triggerGlitch(duration);
}
// Wait a random amount of time
const nextGlitch =
500 + Math.random() * 2500;
setTimeout(randomGlitch, nextGlitch);
}
// Start the VHS
randomGlitch();
// MOUSE HOVER
if (button) {
button.addEventListener("mouseenter", () => {
triggerGlitch(180);
});
}
// CLICK GLITCH
if (button) {
button.addEventListener("click", () => {
// Stronger glitch
triggerGlitch(500);
// You can put your page transition here
setTimeout(() => {
console.log("ENTER CLICKED");
}, 500);
});
}