xxxxxxxxxx
let particleA, particleB;
function setup() {
createCanvas(windowWidth, windowHeight);
background(0);
particleA = createParticle();
particleB = createParticle();
}
function draw() {
background(0, 10);
drawLine()
updateLine()
}
function drawLine() {
stroke(255)
strokeWeight(5)
line(particleA.position.x, particleA.position.y,
particleB.position.x, particleB.position.y)
}
function createParticle() {
return {
position: randomPointOnScreen(), // create a vector at a random co-ordinate
velocity: randomVelocity()
}
}
function updateParticle(particle) {
particle.position.add(particle.velocity);
if (particle.position.x > width || particle.position.x < 0) {
particle.velocity.x *= -1
}
if (particle.position.y > height || particle.position.y < 0) {
particle.velocity.y *= -1
}
}
function mousePressed(){
particleA.velocity = randomVelocity()
particleB.velocity = randomVelocity()
}
function updateLine() {
updateParticle(particleA);
updateParticle(particleB);
}
function randomPointOnScreen(){
return createVector(random(width), random(height))
}
function randomVelocity(){
return p5.Vector.random2D().mult(10)
}