-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathapp.js
147 lines (122 loc) · 3.84 KB
/
app.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
const INITIAL_NB_ELEMENTS = 10;
const INCREMENT = 10;
const SPEED = 3;
const canvas = document.querySelector('.canvas');
const moreButton = document.querySelector('.more');
const lessButton = document.querySelector('.less');
const stopButton = document.querySelector('.stop');
const optimizedButton = document.querySelector('input[value="optimized"]');
const slowButton = document.querySelector('input[value="slow"]');
let nbOfElements = INITIAL_NB_ELEMENTS;
let isOptimized = false;
let isRunning = true;
let currentElements = [];
let canvasHeight = canvas.offsetHeight;
stopButton.addEventListener('click', () => {
isRunning = !isRunning;
stopButton.textContent = isRunning ? 'Stop' : 'Start';
if (isRunning) {
update();
}
});
moreButton.addEventListener('click', () => {
nbOfElements += INCREMENT;
init();
});
lessButton.addEventListener('click', () => {
nbOfElements -= INCREMENT;
if (nbOfElements < INITIAL_NB_ELEMENTS) {
nbOfElements = INITIAL_NB_ELEMENTS;
}
init();
});
optimizedButton.addEventListener('click', () => {
isOptimized = true;
init();
});
slowButton.addEventListener('click', () => {
isOptimized = false;
init();
});
function updateOptimized() {
for (let i = 0; i < currentElements.length; i++) {
const element = currentElements[i];
// Initialize the position of the element if it hasn't been done yet.
if (!element.style.transform) {
element.style.transform = `translateY(${Math.random() * 100}vh)`;
}
// Read the current position.
const currentPos = parseFloat(element.style.transform.match(/[0-9.]+/)[0]);
// Read the direction.
const direction = element.dataset.dir;
// Calculate the next position.
let nextPos = direction === 'up' ? currentPos - (SPEED * 100 / canvasHeight) : currentPos + (SPEED * 100 / canvasHeight);
// If the new position is out of bounds, reset it, and switch the direction.
if (nextPos < 0) {
nextPos = 0;
element.dataset.dir = 'down';
} else if (nextPos > 100) {
nextPos = 100;
element.dataset.dir = 'up';
}
// Set the new position on the element.
element.style.transform = `translateY(${nextPos}vh)`;
}
}
function updateSlow() {
for (let i = 0; i < currentElements.length; i++) {
const element = currentElements[i];
// Initialize the position of the element if it hasn't been done yet.
if (!element.style.top) {
element.style.top = `${Math.random() * 100}%`;
}
// Read the current position.
const currentPos = element.offsetTop;
// Read the direction.
const direction = element.dataset.dir;
// Calculate the next position.
let nextPos = direction === 'up' ? currentPos - SPEED : currentPos + SPEED;
// If the new position is out of bounds, reset it.
if (nextPos < 0) {
nextPos = 0;
} else if (nextPos > canvas.offsetHeight) {
nextPos = canvas.offsetHeight;
}
// Set the new position on the element.
element.style.top = `${nextPos}px`;
// Switch the direction if needed.
if (element.offsetTop === 0) {
element.dataset.dir = 'down';
} else if (element.offsetTop === canvas.offsetHeight) {
element.dataset.dir = 'up';
}
}
}
let rAF = null;
function update() {
if (!isRunning) {
return;
}
if (isOptimized) {
updateOptimized();
} else {
updateSlow();
}
rAF = requestAnimationFrame(update);
}
function init() {
canvas.innerHTML = '';
currentElements = [];
cancelAnimationFrame(rAF);
canvasHeight = canvas.offsetHeight;
for (let i = 0; i < nbOfElements; i++) {
const element = document.createElement('div');
element.classList.add('element');
element.style.left = `${i / (nbOfElements / 100)}%`;
element.setAttribute('data-dir', Math.random() > 0.5 ? 'up' : 'down');
canvas.appendChild(element);
currentElements.push(element);
}
update();
}
onload = init;