-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
130 lines (103 loc) · 3.43 KB
/
Copy pathindex.js
File metadata and controls
130 lines (103 loc) · 3.43 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
// get serach params from url
const urlSearchParams = new URLSearchParams(window.location.search);
const searchParams = [...urlSearchParams].reduce((params, [key, value]) => {
params[key] = value;
return params;
}, {});
const resetButton = document.querySelector('.reset');
resetButton.addEventListener('click', () => {
urlSearchParams.delete('seed');
window.history.replaceState(
{},
'',
`${location.pathname}?${decodeURIComponent(urlSearchParams)}`
);
window.location.reload(false);
});
let {items, seed = Date.now()} = searchParams;
items = items.split(',');
urlSearchParams.set('seed', seed);
window.history.replaceState(
{},
'',
`${location.pathname}?${decodeURIComponent(urlSearchParams)}`
);
const seedRandom = new Math.seedrandom(String(seed));
const container = document.querySelector('.container');
// shuffle items
for (let i = items.length - 1; i > 0; i--) {
const j = Math.floor(seedRandom() * (i + 1));
[items[i], items[j]] = [items[j], items[i]];
}
// create rows
items = items.map(item => {
const colorRandom = new Math.seedrandom(item + String(seed));
const $item = document.createElement('div');
$item.style.transform = 'translateX(-100%)';
$item.className = 'item';
const $logoContainer = document.createElement('div');
$logoContainer.className = 'logo-container logo-container--hidden';
const $logo = document.createElement('img');
$logo.src = 'twix.png';
$logo.className = 'logo';
const $itemText = document.createElement('div');
$itemText.className = 'item-text';
const color = tinycolor(
`rgb(${colorRandom() * 255},${colorRandom() * 255},${colorRandom() * 255})`
);
$item.style.backgroundColor = color.getOriginalInput();
if (color.isDark()) {
$itemText.style.color = 'white';
} else {
$itemText.style.color = 'black';
}
const $itemTextValue = document.createElement('span');
$itemTextValue.className = 'item-text-value';
$itemTextValue.textContent = item;
$logoContainer.appendChild($logo);
$itemText.appendChild($logoContainer);
$itemText.appendChild($itemTextValue);
$item.appendChild($itemText);
container.appendChild($item);
return {$item, progress: 0, name: item};
});
let finished = false;
// loop
const intervalId = setInterval(() => {
if (finished) {
clearInterval(intervalId);
}
items = items.map(item => {
const {$item, progress, name} = item;
const percentToAdd = seedRandom();
const newProgress = finished
? progress
: Math.min(progress + percentToAdd, 100);
$item
.querySelector('.logo-container')
.classList.add('logo-container--hidden');
$item.style.transform = `translate3d(${-100 + newProgress}%, 0, 0)`;
$item.style.zIndex = 0;
if (newProgress === 100 && !finished) {
finished = true;
$item.classList.add('item--winner');
const winnerElement = document.createElement('div');
winnerElement.className = 'winner';
winnerElement.textContent = `${name} is the winner!`;
const gif = document.createElement('img');
gif.className = 'winner-gif';
gif.src = 'yes.gif';
winnerElement.appendChild(gif);
container.appendChild(winnerElement);
}
return {
...item,
progress: newProgress
};
});
const leader = items.slice().sort((a, b) => b.progress - a.progress)[0];
leader.$item
.querySelector('.logo-container')
.classList.remove('logo-container--hidden');
leader.$item.style.zIndex = 1;
}, 300);