-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle-animation.js
194 lines (159 loc) · 5.44 KB
/
handle-animation.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
// This file is responsible for the changes to animation of the sorting
let animationSpeed = 5;
let audioSound = null;
let audioEnabled = true;
const MIN_FREQ = 200;
const MAX_FREQ = 600;
function animate(moves) {
if (moves.length === 0) {
displayBars();
isSorting = false;
enableSliders();
enableDropdown();
enableButtons();
return;
}
const move = moves.shift();
const [i, j] = move.indices;
if (move.moveType === "change") {
[arr[i], arr[j]] = [arr[j], arr[i]];
}
playSound(MIN_FREQ+ arr[i] * MAX_FREQ);
playSound(MIN_FREQ+ arr[j] * MAX_FREQ);
displayBars(move);
setTimeout(function () {
animate(moves);
}, getAnimationSpeed());
}
// ANIMATE INSERTION SORT AND SHELL SORT
function animateShellInsert(moves) {
if (moves.length === 0) {
displayBars();
isSorting = false;
enableSliders();
enableDropdown();
enableButtons();
return;
}
const move = moves.shift();
const [i, j] = move.indices;
if (move.moveType === "change") {
if(i > j){
[arr[i],arr[j]] = [arr[j],arr[i]];
}
}
playSound(MIN_FREQ+ arr[i] * MAX_FREQ);
playSound(MIN_FREQ+ arr[j] * MAX_FREQ);
displayBars(move);
setTimeout(function () {
animateShellInsert(moves);
}, getAnimationSpeed());
}
function enableSliders(){
const sizeInput = document.getElementById("change-size-bars");
const speedInput = document.getElementById("change-speed-bars");
sizeInput.disabled = false;
speedInput.disabled = false;
}
function disableSliders(){
const sizeInput = document.getElementById("change-size-bars");
const speedInput = document.getElementById("change-speed-bars");
sizeInput.disabled = true;
speedInput.disabled = true;
}
function enableButtons() {
const buttons = document.querySelectorAll("button");
buttons.forEach((button) => {
button.disabled = false;
});
}
function disableButtons() {
const buttons = document.querySelectorAll("button");
buttons.forEach((button) => {
button.disabled = true;
});
}
function enableDropdown() {
const sortDropdown = document.getElementById("sort-dropdown");
sortDropdown.disabled = false;
sortDropdown.style.display = "block";
}
function disableAndHideDropdown(){
const sortDropdown = document.getElementById("sort-dropdown");
sortDropdown.disabled = true;
sortDropdown.style.display = "none";
}
function handleSizeChange() {
const barSizeInput = document.getElementById("change-size-bars");
const newSize = parseInt(barSizeInput.value);
if (newSize < 4) {
barSizeInput.value = 4; // 4 is the minimum number of bars
return; // Exit the function to prevent further execution
}
if (newSize < n) {
arr = arr.slice(0, newSize);
} else {
for (let i = n; i < newSize; i++) {
arr[i] = Math.random();
}
}
n = newSize;
reset();
const container = document.getElementById("container");
const barElements = container.getElementsByClassName("bar");
const barWidth = 500 / newSize; // Adjust the width of the bars based on the new size
for (let i = 0; i < barElements.length; i++) {
barElements[i].style.width = barWidth + "px"; // Set the updated width of the bars
}
}
document.getElementById("change-size-bars").addEventListener("input", handleSizeChange);
// Get the animation speed based on the position of the speed cursor
function getAnimationSpeed() {
const speedInput = document.getElementById("change-speed-bars");
return 100 - parseInt(speedInput.value); // Invert the value to make higher values correspond to faster speed
}
// Update the animation speed when the speed cursor value changes
document.getElementById("change-speed-bars").addEventListener("input", function () {
animationSpeed = getAnimationSpeed();
});
const toggleModeButton = document.getElementById("toggle-mode-btn");
toggleModeButton.addEventListener("click", toggleMode);
// This customises the background to either light mode or dark mode
function toggleMode() {
var element = document.body;
element.classList.toggle("dark-mode");
const currentMode = toggleModeButton.textContent;
if(currentMode == "Dark Mode"){
toggleModeButton.textContent = "Light Mode";
}else{
toggleModeButton.textContent = "Dark Mode"
}
}
// This is to allow the user to set if the sound off or on
const toggleSoundButton = document.getElementById("toggle-sound");
toggleSoundButton.addEventListener("click", toggleSound);
function toggleSound() {
audioEnabled = !audioEnabled;
toggleSoundButton.textContent = audioEnabled ? "Sound On" : "Sound Off";
}
// adding sound to the bars if user would like that using webaudio api
// (adding sound could help people with vision loss using the sound)
function playSound(music){
if(!audioEnabled){
return;
}
if(audioSound == null){
audioSound = new(AudioContext || webAudioContext
|| window.webkitAudioContext)();
}
const music_duration = 0.1;
const oscilator = audioSound.createOscillator();
oscilator.frequency.value = music;
oscilator.start();
oscilator.stop(audioSound.currentTime + music_duration);
const node = audioSound.createGain();
node.gain.value = 0.1;
node.gain.linearRampToValueAtTime(0, audioSound.currentTime +music_duration);
oscilator.connect(node);
node.connect(audioSound.destination);
}