xxxxxxxxxx
points = []; //array for vectors
connectDist = 40; //dist for connection
maxPoints = 600;
function setup() {
createCanvas(1000, 600);
background(0, 15, 30);
strokeWeight(1);
let hint = "Press right arrow key to add points, left to delete. Press shift to clear canvas";
console.log(hint);
}
function draw() {
connectDist = map(mouseX, 0, width, 20, 100); //maps dist with mouseX
for (let i = 0; i < points.length; i++) { //for every vector in array
for (let p = i+1; p < points.length; p++) { //loop through all other vectors
if (points[i].dist(points[p]) < connectDist) { //if within dist draw a line accross
//stroke(255-i*0.2, 70, 255-i*2, 3);
stroke(70, 255-i*2, 255-i*0.2, 3);
//stroke(255-i*0.2, 255-i*2, 70, 3);
line(points[i].x, points[i].y, points[p].x, points[p].y);
}
}
}
for (let p of points) {
p.add(p5.Vector.random2D().setMag(1)); //adds random motion to points
}
keyControl();
}
function keyControl() {
if (keyIsPressed && keyCode == LEFT_ARROW) {
for (let i = 0; i < 10; i++) {
if (points.length > 0) {
points.pop();
}
}
}
if (keyIsPressed && keyCode == RIGHT_ARROW) {
for (let i = 0; i < 10; i++) {
if (points.length < maxPoints) {
points.push(new createVector(random(width), random(height)));
}
}
}
if (keyIsPressed && keyCode == SHIFT) {
noStroke();
fill(0, 15, 30);
rect(0, 0, width, height);
}
}