xxxxxxxxxx
var values = [];
var curpos = 0;
var count = 69; //change this to adjust the number of bars
var first = 0;
var last = count-1;
var lf = first;
var ll = last;
var dir = 1;
var done = false;
var fin = false;
var swapped = false;
var sind = 0;
var osc;
function setup() {
createCanvas(windowWidth, windowHeight);
background(0);
//frameRate(15);
for (var i = 0; i < count; i++) {
values[i] = i+1;
}
for (var i = 0; i < count; i++) {
var nwps = floor(random(0, count-1));
var inter = values[nwps];
values[nwps] = values[i];
values[i] = inter;
}
osc = new p5.Oscillator('sine');
osc.start();
osc.amp(0.25)
}
function draw() {
if (done == false) {
clear();
noStroke();
//fill(127);
for (var j = 0; j < values.length; j++) {
fill(((values[j]-1)/(count-1))*224+29, ((count-values[j])/(count-1))*131+105, ((count-values[j])/(count-1))*65+179);
rect(1+((width-count-1)/count+1)*j, height, (width-count-1)/count, -values[j]*((height-(height/(count+1)))/(count+1)));
}
fill(255, 0, 0);
if (fin == true) fill(0, 255, 0);
rect(1+((width-count-1)/count+1)*curpos, height, (width-count-1)/count, -values[curpos]*((height-(height/(count+1)))/(count+1)));
if (dir == 1) {
if (values[curpos] > values[curpos+1]) {
inter = values[curpos+1];
values[curpos+1] = values[curpos];
values[curpos] = inter;
swapped = true;
sind = curpos;
}
} else {
if (values[curpos] < values[curpos-1]) {
inter = values[curpos-1];
values[curpos-1] = values[curpos];
values[curpos] = inter;
swapped = true;
sind = curpos;
}
}
osc.freq(((values[curpos]-1)*(330/(count-1))+110)*2);
curpos = curpos + dir;
if ((curpos == lf || curpos == ll) && swapped == false && fin == false) {
curpos = 0;
dir = 1;
fin = true;
}
if (curpos == last) {
if (swapped) {
curpos--;
dir = -1;
if (last <= first - 1) {
done = true;
}
ll = last;
last = sind;
swapped = false;
}
}
if (curpos == first) {
if (swapped) {
curpos++;
dir = 1;
if (last <= first - 1) {
done = true;
}
lf = first;
first = sind;
swapped = false;
}
}
}
if (curpos > values.length) {
osc.stop(0.2);
}
}