xxxxxxxxxx
128
var followersRightEye;
var followersLeftEye;
var num = 20;
var position;
var velocity;
var gravity;
var hasBeenClicked = false;
function setup() {
createCanvas(640, 360);
frameRate(10);
followersRightEye = [];
followersLeftEye = [];
positionRight = createVector(400, 100);
positionLeft = createVector(100, 100);
velocityRight = createVector(4, -4);
velocityLeft = createVector(-4, -4);
gravity = createVector(0, 0.5);
for (var i = 0; i < num; i++) {
followersRightEye.push(new Follower(positionRight.x, positionRight.y));
}
followersLeftEye = [];
for (var i = 0; i < num; i++) {
followersLeftEye.push(new Follower(positionLeft.x, positionLeft.y));
}
}
function mousePressed() {
hasBeenClicked = true;
}
function draw() {
background('red');
fill(7, 219, 107)
noStroke();
ellipse(150, 120, 200, 200)
ellipse(250, 120, 200, 200)
stroke(192, 54, 16)
strokeWeight(20)
line(90, 500, 160, 250);
line(340, 500, 250, 250)
noStroke();
fill('purple')
ellipse(120, 350, 50, 30)
ellipse(290, 350, 50, 30)
fill(244, 101, 40)
noStroke();
ellipse(150, 130, 150, 170)
ellipse(250, 130, 150, 170)
fill('green')
noStroke();
ellipse(200, 180, 220, 220)
if (hasBeenClicked) {
drawEyes();
}
}
function drawEyes() {
//update physics
velocityRight.add(gravity);
velocityLeft.add(gravity);
positionRight.add(velocityRight);
positionLeft.add(velocityLeft);
followersRightEye[0].setValue(positionRight.x, positionRight.y);
followersLeftEye[0].setValue(positionLeft.x, positionLeft.y);
for (var i = 1; i < num; i++) {
followersRightEye[i].update(followersRightEye[i - 1].x, followersRightEye[i - 1].y);
}
for (var i = 1; i < num; i++) {
followersLeftEye[i].update(followersLeftEye[i - 1].x, followersLeftEye[i - 1].y);
}
//DRAW THE BETTER EYE
let leftEye = followersLeftEye[0];
let rightEye = followersRightEye[0];
stroke("black");
fill("white");
strokeWeight(2);
ellipse(leftEye.x, leftEye.y, 20, 40);
ellipse(rightEye.x, rightEye.y, 20, 40);
}
class Follower {
constructor(x, y) {
this.x = x;
this.y = y;
this.velX = 0;
this.velY = 0;
}
update(fx, fy) {
let dx = fx - this.x;
let dy = fy - this.y;
if (dx * dx + dy * dy > 50) {
this.velX = 0.20 * (this.velX + fx - this.x);
this.velY = 0.20 * (this.velY + fy - this.y);
this.x += this.velX;
this.y += this.velY;
}
stroke("black");
strokeWeight(2);
noFill();
ellipse(this.x, this.y, 20, 20);
}
setValue(fx, fy) {
this.x = fx;
this.y = fy;
}
}