forked from caspervonb/toneofsorting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
246 lines (199 loc) · 7.79 KB
/
index.html
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
<html>
<head>
<link rel="stylesheet" href="screen.css">
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-107714021-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-107714021-1');
</script>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>
(adsbygoogle = window.adsbygoogle || []).push({
google_ad_client: "ca-pub-3538270718430385",
enable_page_level_ads: true
});
</script>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<main id="content" class="content">
<section id="configuration" class="">
<p>
<label for='#algorithm'>Algorithm</label>
<select id='algorithm'>
<option value='bubbleSort'>Bubble sort</option>
<option value='cocktailSort'>Cocktail sort</option>
<option value='insertionSort'>Insertion sort</option>
<option value='oddEvenSort'>Odd-even sort</option>
<option value='selectionSort'>Selection sort</option>
</select>
</p>
<p>
<label for='#size'>Size</label>
<input id='size' type='number' value="20"/>
</p>
<p>
<label for='#shuffle'>Shuffle</label>
<select id='shuffle'>
<option value='random'>Random</option>
<option value='ascending'>Ascending</option>
<option value='descending'>Descending</option>
</select>
</p>
<p>
<label for='#delay'>Delay</label>
<input id="delay" type="range" value="200" min="1" max="1000">
</p>
<p>
<label for='#volume'>Volume</label>
<input id="volume" type="range" value="25" min="0" max="100">
</p>
<p>
<input id="sort" type="submit" value="Sort">
</p>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
data-ad-client="ca-pub-3538270718430385"
data-ad-slot="8210302024"
data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</section>
<section>
<output id="visualization">
</output>
</section>
</main>
<script>
var queue = null;
var worker = null;
if (typeof AudioContext == 'undefined') {
AudioContext = webkitAudioContext;
}
var audio = new AudioContext();
var master = audio.createGain();
master.gain.setValueAtTime(0.20, audio.currentTime);
master.connect(audio.destination);
var volume = document.querySelector('#volume');
volume.onchange = function() {
master.gain.setValueAtTime(Number(this.value) / 100, audio.currentTime);
};
var track = audio.createGain();
track.gain.setValueAtTime(0, audio.currentTime);
track.connect(master);
var tone = audio.createOscillator();
tone.type = 'triangle';
tone.frequency.value = 440;
tone.connect(track);
tone.start();
var sort = document.querySelector('#sort');
sort.onclick = function click(event) {
if (worker) {
worker.terminate();
}
queue = new Array();
worker = new Worker('sort.js');
worker.onmessage = function message(event) {
queue.push(event);
};
var algorithm = document.querySelector('#algorithm');
var shuffle = document.querySelector('#shuffle');
var size = document.querySelector('#size');
var length = Number(size.value);
var array = new Array(length);
for (var i = 0; i < array.length; i++) {
array[i] = i + 1;
}
array.sort(function(a, b) {
switch (shuffle.value) {
case 'random':
return Math.random() > 0.5 ? -1 : 1;
case 'ascending':
return a - b;
case 'descending':
return b - a;
}
});
var visualization = document.querySelector('#visualization');
visualization.innerHTML = '';
for (var i = 0; i < array.length; i++) {
var element = document.createElement('span');
element.dataset.value = array[i];
if (array.length <= 10) {
element.className = 'ball';
element.innerText = array[i];
} else {
element.className = 'bar';
element.style.height = Math.round((array[i] / array.length) * 100) + '%';
}
visualization.appendChild(element);
}
worker.postMessage([algorithm.value, array]);
};
var then = performance.now();
window.requestAnimationFrame(function tick(now) {
var container = document.querySelector('#visualization');
var elements = container.querySelectorAll('span');
var delay = Number(document.querySelector('#delay').value);
for (var i = 0; i < elements.length; i++) {
if (elements[i].style.translate != '0px') {
elements[i].style.transition = 'all ' + (delay / 1000) + 's';
elements[i].style.transform = 'translate(0px)';
//elements[i].style.translate = '0px';
}
}
if (now - then > delay) {
for (var i = 0; i < elements.length; i++) {
elements[i].classList.remove('test');
elements[i].classList.remove('swap');
}
var event = (queue || []).shift();
if (event) {
var element1 = elements[event.data[1]];
var element2 = elements[event.data[2]];
var value1 = Number(element1.dataset.value);
var value2 = Number(element2.dataset.value);
var distance = Math.floor(element1.offsetLeft - element2.offsetLeft);
if (event.data[0] == 'test') {
element1.classList.add('test');
element2.classList.add('test');
var factor = ((value1 / elements.length) + (value2 / elements.length) / 2);
var frequency = 440 + (factor * 440);
tone.frequency.linearRampToValueAtTime(frequency, audio.currentTime);
track.gain.cancelScheduledValues(audio.currentTime);
track.gain.linearRampToValueAtTime(0.75, audio.currentTime);
track.gain.linearRampToValueAtTime(0, audio.currentTime + delay);
}
if (event.data[0] == 'swap') {
var factor = ((value1 / elements.length) + (value2 / elements.length) / 2);
var frequency = 440 - (factor * 440);
tone.frequency.linearRampToValueAtTime(frequency, audio.currentTime);
track.gain.cancelScheduledValues(audio.currentTime);
track.gain.linearRampToValueAtTime(1, audio.currentTime);
track.gain.linearRampToValueAtTime(0, audio.currentTime + delay);
var temp = document.createElement('span');
element1.parentNode.insertBefore(temp, element1);
element1.classList.add('swap');
element2.parentNode.insertBefore(element1, element2);
element2.classList.add('swap');
temp.parentNode.insertBefore(element2, temp);
temp.parentNode.removeChild(temp);
element1.style.transition = '';
element1.style.transform = 'translate(' + (distance * 1) + 'px)';
element2.style.transition = '';
element2.style.transform = 'translate(' + (distance * -1) + 'px)';
}
} else {
track.gain.cancelScheduledValues(0);
track.gain.linearRampToValueAtTime(0, audio.currentTime);
}
then = now;
}
window.requestAnimationFrame(tick);
});
</script>
</body>
</html>