xxxxxxxxxx
67
let springs;
let x;
let y;
let vx;
let vy;
function makeSpring() {
return {
bx: random(0, width),
by: random(0, height),
rl: random(100, 400),
s: random(0.001, 0.01),
};
}
function initSystem() {
springs = [];
const ns = int(random(2, 5));
for (let idx = 0; idx < ns; ++idx) {
springs.push(makeSpring());
}
x = random(0, width);
y = random(0, height);
vx = 0;
vy = 0;
}
function setup() {
createCanvas(480, 480);
background("#3468C0");
initSystem();
}
function draw() {
let ax = 0;
let ay = 0;
for (let s of springs) {
const d = dist(s.bx, s.by, x, y);
const dx = (s.bx - x) / d;
const dy = (s.by - y) / d;
ax += (d - s.rl) * dx;
ay += (d - s.rl) * dy;
}
vx += ax * 0.004;
vy += ay * 0.004;
const nx = x + vx + 0.01;
const ny = y + vy + 0.01;
noFill();
stroke("#FF9843");
strokeWeight(3);
line(x, y, nx, ny);
x = nx;
y = ny;
}
function keyPressed() {
if (key === " ") {
initSystem();
background("#3468C0");
} else if (key === "s") {
save( 'jan8.png' );
}
}