Skip to content

Commit c83e62f

Browse files
committed
added traversing and smallest element color animation
1 parent b450eed commit c83e62f

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

function.js

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
var data, svg, bandScale, text;
22
data = [];
3+
var time = 700;
34
for (var i = 0; i < 15; i++) {
45
data.push(Math.floor(Math.random() * 100) + 1);
56
}
7+
var h = 100,
8+
w = 800;
9+
var heightScale = d3
10+
.scaleLinear()
11+
.domain([0, d3.max(data)])
12+
.range([0, h]);
13+
614
createChart();
715

816
function createChart() {
917
svg = d3.select("#chart").append("svg");
1018

11-
var h = 100,
12-
w = 800;
13-
1419
//var bandWidth = w / data.length - 1;
1520

1621
bandScale = d3.scaleBand().domain(data).range([0, w]).padding(0.1);
17-
var heightScale = d3
18-
.scaleLinear()
19-
.domain([0, d3.max(data)])
20-
.range([0, h]);
2122

2223
svg.attr("width", w).attr("height", h);
2324

@@ -78,33 +79,40 @@ function selectionSort() {
7879
const timer = (ms) => new Promise((res) => setTimeout(res, ms));
7980
async function sort() {
8081
// We need to wrap the loop into an async function for this to work
82+
var traverseColor = "#ffcaa1";
83+
var smallestColor = "#ab87ff";
84+
var unsortedColor = "#add8e6";
8185
for (var i = 0; i < data.length; i++) {
8286
smallest = data[i];
8387
pos = i;
88+
forColorAnimation(smallest, smallestColor);
89+
await timer(time);
8490
for (var j = i + 1; j < data.length; j++) {
91+
forColorAnimation(data[j], traverseColor);
8592
if (smallest > data[j]) {
93+
await timer(time);
94+
forColorAnimation(smallest, unsortedColor);
8695
smallest = data[j];
8796
pos = j;
8897
}
98+
99+
forColorAnimation(smallest, smallestColor);
100+
await timer(time);
101+
forColorAnimation(data[j], unsortedColor);
89102
}
90103
if (data[i] != smallest) {
91104
temp = data[i];
92105
data[i] = smallest;
93106
data[pos] = temp;
94-
var smi = bandScale(smallest);
95-
svg.selectAll("rect").each(function (d, i) {
96-
console.log(d3.select(this).attr("height"));
97-
if (smi == d3.select(this).attr("height")) {
98-
console.log(smi);
99-
}
100-
});
107+
101108
var swooshAudio = new Audio(
102109
"/algorithm-visualizer/sound-effects/swoosh.mp3"
103110
);
104111
swooshAudio.play();
105112
}
113+
forColorAnimation(smallest, "#56b4d3");
106114
sortAnimate(data);
107-
await timer(1000); // then the created Promise can be awaited
115+
await timer(time); // then the created Promise can be awaited
108116
}
109117
svg.selectAll("rect").style("fill", "green");
110118
var completeAudio = new Audio(
@@ -139,3 +147,12 @@ function sortAnimate(data) {
139147
return bandScale(d) + 5;
140148
});
141149
}
150+
151+
function forColorAnimation(d, color) {
152+
var smi = heightScale(d);
153+
svg.selectAll("rect").each(function (d, i) {
154+
if (smi == d3.select(this).attr("height")) {
155+
d3.select(this).style("fill", color);
156+
}
157+
});
158+
}

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ <h1>Sorting Visualizer</h1>
1717
<button id="selection-sort">Sort</button>
1818
</div>
1919
</div>
20-
<script src="https://cdn.jsdelivr.net/npm/d3@7"></script>
20+
<script src="https://d3js.org/d3.v4.min.js"></script>
2121
<script src="function.js"></script>
2222
</body>
2323
</html>

0 commit comments

Comments
 (0)