-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
234 lines (195 loc) · 5.41 KB
/
script.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
function spawn_boxes(row, column, visible = true) {
var grid = document.getElementById("grid");
// Attach to tbody if it already exists
var tbody = document.getElementById("tbody");
if (tbody == null) {
tbody = document.createElement("tbody");
tbody.id = "tbody";
}
grid.appendChild(tbody);
// Get latest box id (if any)
if (tbody.lastChild != null) {
var latest_box = tbody.lastChild.lastChild;
var latest_box_id = latest_box.id.split("-");
var starting_row = parseInt(latest_box_id[1]) + 1;
} else {
starting_row = 0;
}
let boxes = []
let rows = []
// Add new boxes
for (var i = starting_row; i < row + starting_row; i++) {
var tr = tbody.appendChild(document.createElement("tr"));
tr.className = "tr";
rows.push(tr);
for (var j = 0; j < column; j++) {
var box = document.createElement("td");
box.className = "box";
box.id = "box-" + i + "-" + j;
if (!visible) {
box.style.opacity = 0;
box.style.display = "none";
tr.style.display = "none";
}
boxes.push(box);
console.log(box)
tr.appendChild(box);
}
}
return [boxes, rows];
}
function set_box_color(row, column, color) {
var box = document.getElementById("box-" + row + "-" + column);
box.style.backgroundColor = color;
return box;
}
function create_arr(greens) {
let arr = [];
for (var key in greens) {
for (var i = 0; i < greens[key]; i++) {
arr.push(key);
}
}
return arr;
}
function get_random_green(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
function set_volume() {
var audio = document.getElementById("audio");
var vol = document.getElementById("volume");
audio.volume = vol.value;
if (audio.muted) {
audio.muted = false;
}
if (audio.volume == 0) {
vol.style.setProperty("--thumb-color", "#272b33")
} else if (audio.volume < 0.25) {
vol.style.setProperty("--thumb-color", "#0e4429")
} else if (audio.volume < 0.5) {
vol.style.setProperty("--thumb-color", "#006d32")
} else if (audio.volume < 0.75) {
vol.style.setProperty("--thumb-color", "#26a641")
} else {
vol.style.setProperty("--thumb-color", "#39d353")
}
}
function unmute_fast() {
var audio = document.getElementById("audio");
var vol = document.getElementById("volume");
audio.muted = false;
audio.volume = 0.5;
vol.value = 0.5;
vol.style.setProperty("--thumb-color", "#26a641")
document.getElementById("muted").remove();
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const anime = window.anime;
function create_muted() {
muted = document.getElementById("muted");
muted.style.display = "flex";
muted.addEventListener("click", () => {
anime({
targets: muted,
opacity: [0.7, 0.0],
duration: 100,
complete: unmute_fast,
})
});
muted.addEventListener("mouseenter", () => {
anime({
targets: muted,
opacity: [0.0, 0.5],
delay: anime.stagger(100)
});
});
muted.addEventListener("mouseleave", () => {
anime({
targets: muted,
opacity: [0.5, 0.0],
delay: anime.stagger(100)
});
});
}
const GRAY = "#272b33";
// COLOR: WEIGHT OF OCCURENCE
const ARR = create_arr({
"#0e4429": 1000,
"#006d32": 100,
"#26a641": 10,
"#39d353": 1,
});
spawn_boxes(10, 40);
fetch("https://lnus.github.io/git-apple/assets/frames.json")
.then(function (response) {
return response.json();
})
.then(async function (frames) {
// animate in container when loaded
var c = document.getElementById("container");
anime({
targets: c,
opacity: [0, 1],
delay: anime.stagger(100)
})
var audio = document.getElementById("audio");
var boxes = []
var contrib = document.getElementById("contrib");
var contributions = 0;
// Randmoize the first 10x40 boxes
for (var i = 0; i < 10; i++) {
for (var j = 0; j < 40; j++) {
// Either gray or random green
// Gray being more common than green
if (Math.random() < 0.8) {
boxes.push(set_box_color(i, j, GRAY));
} else {
boxes.push(set_box_color(i, j, get_random_green(ARR)));
contributions += 1;
}
}
}
contrib.innerHTML = contributions + " contributions in the last year";
// Spawn remaining boxes
var res = spawn_boxes(20, 40, false);
res[0].map(box => boxes.push(box));
await sleep(2000);
// Animate in all boxes
anime({
targets: boxes,
backgroundColor: GRAY,
opacity: 1.0,
scale: 1,
delay: function (el, i) {
el.closest("tr").style.display = "";
el.style.display = "";
return i * 1.5;
}
});
await sleep(boxes.length * 1.5);
// Start playing audio
audio.play();
document.getElementById("volume").value = 0.0;
audio.volume = 0.0;
// Create the muted overlay
create_muted();
frames.forEach(function (frame, i) {
setTimeout(function () {
contributions = 0;
frame.forEach(function (row, i) {
row.forEach(function (color, j) {
if (color == 0) {
contributions += 1;
color = get_random_green(ARR);
} else {
color = GRAY;
}
set_box_color(i, j, color);
});
});
contrib.innerHTML = contributions + " contributions in the last year";
}, (1000 / 30) * i);
});
});