-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
169 lines (151 loc) · 5.06 KB
/
main.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
import {Heap} from "./heap.js";
const arrayContainer = document.querySelector(".array-visual");
const insertInput = document.getElementById("insert-input");
const insertButton = document.getElementById("insert-button");
const insertError = document.getElementById("insert-error");
const removeButton = document.getElementById("remove-button");
const removeError = document.getElementById("remove-error");
var audioCtx = new (window.AudioContext || window.webkitAudioContext);
var canvas = document.getElementById('heap-canvas');
var canvasContainer = document.getElementById('canvas-container');
canvas.width = canvasContainer.clientWidth;
canvas.height = canvasContainer.clientHeight - 100;
var canvasCtx = canvas.getContext('2d');
canvasCtx.font = '14px sans-serif';
canvasCtx.textAlign = 'center';
canvasCtx.textBaseline = 'middle';
canvasCtx.lineWidth = 2;
var heap = new Heap();
const setupArrayVisual = () => {
arrayContainer.innerHTML = '';
for (var i = 1; i <= heap.size(); i++) {
var arrayCell = document.createElement("div");
arrayCell.classList.add("array-cell");
arrayCell.id = `cell-${i}`;
arrayCell.innerHTML = `${heap.get(i)}`;
arrayContainer.appendChild(arrayCell);
}
}
const validateInsertAction = () => {
const MIN_VAL = -100, MAX_VAL = 100;
if (insertInput.value === "") {
return "Please input a value.";
}
else {
const insertValue = parseInt(insertInput.value);
if (MIN_VAL <= insertValue && insertValue <= MAX_VAL) {
return "";
}
else {
return `Value must be between ${MIN_VAL} and ${MAX_VAL}.`
}
}
}
const setupInsertButton = () => {
insertButton.addEventListener('click', (e) => {
const validation = validateInsertAction();
if (validation === "") {
heap.insert(parseInt(insertInput.value), animateArrayStep);
insertError.innerHTML = "";
insertInput.value = "";
}
else {
insertError.innerHTML = validation;
}
});
}
const validateRemoveAction = () => {
if (heap.size() > 0) {
return "";
}
else {
return "No elements in heap."
}
}
const setupRemoveButton = () => {
removeButton.addEventListener('click', (e) => {
const validation = validateRemoveAction();
if (validation === "") {
heap.remove(animateArrayStep);
removeError.innerHTML = "";
}
else {
removeError.innerHTML = validation;
}
});
}
const setup = () => {
setupArrayVisual();
setupInsertButton();
setupRemoveButton();
}
const playNote = (frequency) => {
var oscillator = audioCtx.createOscillator();
oscillator.type = "sine";
oscillator.frequency.value = frequency;
var gain = audioCtx.createGain();
oscillator.connect(gain);
gain.connect(audioCtx.destination);
var now = audioCtx.currentTime;
gain.gain.setValueAtTime(1, now);
gain.gain.exponentialRampToValueAtTime(0.001, now + 1.5);
oscillator.start(now);
oscillator.stop(now + 1.5);
}
const drawEdge = (x1, y1, x2, y2) => {
const angle = Math.atan2(y1 - y2, x2- x1);
const xDel = 25 * Math.cos(angle), yDel = 25 * Math.sin(angle);
canvasCtx.beginPath();
canvasCtx.moveTo(x1 + xDel, y1 - yDel);
canvasCtx.lineTo(x2 - xDel, y2 + yDel);
canvasCtx.stroke();
}
const drawNode = (index, indicatorIndex, x, y, xSpacing, ySpacing=80) => {
if (index === indicatorIndex) {
canvasCtx.fillStyle = 'coral';
canvasCtx.beginPath();
canvasCtx.arc(x, y, 25, 0, 2 * Math.PI);
canvasCtx.fill();
canvasCtx.fillStyle = 'black';
}
canvasCtx.beginPath();
canvasCtx.arc(x, y, 25, 0, 2 * Math.PI);
canvasCtx.stroke();
canvasCtx.fillText(`${heap.get(index)}`, x, y);
const leftChildIndex = 2 * index, rightChildIndex = 2 * index + 1;
if (heap.get(leftChildIndex) !== undefined) {
drawNode(leftChildIndex, indicatorIndex, x - xSpacing, y + ySpacing, xSpacing / 2);
drawEdge(x, y, x - xSpacing, y + ySpacing);
}
if (heap.get(rightChildIndex) !== undefined) {
drawNode(rightChildIndex, indicatorIndex, x + xSpacing, y + ySpacing, xSpacing / 2);
drawEdge(x, y, x + xSpacing, y + ySpacing);
}
}
const drawHeap = (indicatorIndex) => {
if (heap.size() === 0) {
return;
}
canvasCtx.clearRect(0, 0, canvas.width, canvas.height);
const numLevels = Math.floor(Math.log2(heap.size()));
const rootX = canvas.width / 2;
const rootY = canvas.height / 2 - numLevels / 2 * 80;
drawNode(1, indicatorIndex, rootX, rootY, 160);
}
const animateArrayStep = (index, callback) => {
setupArrayVisual();
drawHeap(index);
const cell = document.getElementById(`cell-${index}`);
setTimeout(() => {
if (cell) {
cell.classList.add("touched");
playNote(440 * Math.pow(2, (index - 1) / 12));
}}, 0);
setTimeout(() => {
if (cell) {
cell.classList.remove("touched");
}
}, 600);
setTimeout(callback, 1000);
}
setup();