xxxxxxxxxx
var dataExamples = []; //array of data
function setup() {
createCanvas(windowWidth, windowHeight);
background(0);
}
function draw() {
background(0);
if (dataExamples.length < 2000) {
//We can still make new points
spawnPoint(2000, mouseX); //index doesn't matter
}
for (var i = dataExamples.length - 1; i >= 0; i--) {
var d = dataExamples[i];
d.render();
if (d.getLife() <= 0) {
//It died, replace it
spawnPoint(i, mouseX);
}
}
}
//Add a new data point
function spawnPoint(index, x) {
var data = new datafield(x, -100 * sin(frameCount), windowWidth + 100 * sin(frameCount));
var d = new dataPoint(data);
if (index >= dataExamples.length) {
//New
dataExamples.push(d);
}
else {
//Reborn
dataExamples[index] = d;
}
}
//Mapping data to ranges
function map (num, in_min, in_max, out_min, out_max) {
//from https://stackoverflow.com/a/23202637
return (num - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
//The class for holding data values / updating them
function datafield(input, min, max) {
this.data = input;
this.min = min;
this.max = max;
this.setInput = function(input) {
this.data = input;
}
this.setMin = function(min) {
this.min = min;
}
this.setMax = function(max) {
this.max = max;
}
this.getInput = function() {
return this.data;
}
this.getMin = function() {
return this.min;
}
this.getMax = function() {
return this.max;
}
this.getValue = function(outmin, outmax) {
return map(this.data, this.min, this.max, outmin, outmax);
}
}
//An example of using the data
function dataPoint(data) {
this.data = data;
this.c = color(random(205.0) + 50.0, random(205.0) + 50.0, random(205.0) + 50.0);
this.life = 500; //500 frames of life
this.y = mouseY;
this.x = 0;
this.render = function() {
this.x = this.data.getValue(10.0, windowWidth - 10.0);
this.data.setInput(this.data.getInput() + random(1.0) - random(1.0)); //Modify the data slightly to simulate changes
this.y += random(0.5);
fill(this.c);
noStroke();
ellipse(this.x, this.y, 40, 40);
this.life -= 1;
}
this.getLife = function() {
return this.life;
}
}