xxxxxxxxxx
// Follow 2 refactored version
// Original code: https://p5js.org/examples/interaction-follow-2.html
let pos = [];
const segNum = 16;
const segLength = 50;
function setup() {
createCanvas(windowWidth, windowHeight);
stroke(0, 100);
strokeWeight(20);
for (let i = 0; i < segNum; i++) {
pos.push({ x: 0, y: 0 });
}
}
function draw() {
background(255);
let parent = { x: mouseX, y: mouseY };
for (let child of pos) {
let angle = atan2(child.y - parent.y, child.x - parent.x);
child.x = parent.x + cos(angle) * segLength;
child.y = parent.y + sin(angle) * segLength;
line(parent.x, parent.y, child.x, child.y);
parent = child;
}
}