|
1 | 1 | var data, svg, bandScale, text; |
2 | 2 | data = []; |
| 3 | +var time = 700; |
3 | 4 | for (var i = 0; i < 15; i++) { |
4 | 5 | data.push(Math.floor(Math.random() * 100) + 1); |
5 | 6 | } |
| 7 | +var h = 100, |
| 8 | + w = 800; |
| 9 | +var heightScale = d3 |
| 10 | + .scaleLinear() |
| 11 | + .domain([0, d3.max(data)]) |
| 12 | + .range([0, h]); |
| 13 | + |
6 | 14 | createChart(); |
7 | 15 |
|
8 | 16 | function createChart() { |
9 | 17 | svg = d3.select("#chart").append("svg"); |
10 | 18 |
|
11 | | - var h = 100, |
12 | | - w = 800; |
13 | | - |
14 | 19 | //var bandWidth = w / data.length - 1; |
15 | 20 |
|
16 | 21 | 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]); |
21 | 22 |
|
22 | 23 | svg.attr("width", w).attr("height", h); |
23 | 24 |
|
@@ -78,33 +79,40 @@ function selectionSort() { |
78 | 79 | const timer = (ms) => new Promise((res) => setTimeout(res, ms)); |
79 | 80 | async function sort() { |
80 | 81 | // 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"; |
81 | 85 | for (var i = 0; i < data.length; i++) { |
82 | 86 | smallest = data[i]; |
83 | 87 | pos = i; |
| 88 | + forColorAnimation(smallest, smallestColor); |
| 89 | + await timer(time); |
84 | 90 | for (var j = i + 1; j < data.length; j++) { |
| 91 | + forColorAnimation(data[j], traverseColor); |
85 | 92 | if (smallest > data[j]) { |
| 93 | + await timer(time); |
| 94 | + forColorAnimation(smallest, unsortedColor); |
86 | 95 | smallest = data[j]; |
87 | 96 | pos = j; |
88 | 97 | } |
| 98 | + |
| 99 | + forColorAnimation(smallest, smallestColor); |
| 100 | + await timer(time); |
| 101 | + forColorAnimation(data[j], unsortedColor); |
89 | 102 | } |
90 | 103 | if (data[i] != smallest) { |
91 | 104 | temp = data[i]; |
92 | 105 | data[i] = smallest; |
93 | 106 | 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 | + |
101 | 108 | var swooshAudio = new Audio( |
102 | 109 | "/algorithm-visualizer/sound-effects/swoosh.mp3" |
103 | 110 | ); |
104 | 111 | swooshAudio.play(); |
105 | 112 | } |
| 113 | + forColorAnimation(smallest, "#56b4d3"); |
106 | 114 | sortAnimate(data); |
107 | | - await timer(1000); // then the created Promise can be awaited |
| 115 | + await timer(time); // then the created Promise can be awaited |
108 | 116 | } |
109 | 117 | svg.selectAll("rect").style("fill", "green"); |
110 | 118 | var completeAudio = new Audio( |
@@ -139,3 +147,12 @@ function sortAnimate(data) { |
139 | 147 | return bandScale(d) + 5; |
140 | 148 | }); |
141 | 149 | } |
| 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 | +} |
0 commit comments