xxxxxxxxxx
66
let eyeX = 200; // X position of the eye
let eyeY = 200; // Y position of the eye
let eyeSize = 100; // diameter of the eye
let irisSize = 50; // diameter of the iris
let pupilSize = 25; // diameter of the pupil
function setup() {
createCanvas(400, 400);
background("#FF2E3F");
angleMode(DEGREES);
}
function draw() {
background("#FF2E3F");
drawEye(eyeX, eyeY, eyeSize, irisSize, pupilSize);
}
function drawEye(x, y, eyeSize, irisSize, pupilSize) {
// draw the sclera
noStroke();
fill("#F2F2F2");
ellipse(x, y, eyeSize, eyeSize);
// calculate the angle between the mouse position and the eye center
let angle = atan2(mouseY - y, mouseX - x);
// set a maximum distance the iris and pupil center can move (within the edge of sclera)
let maxDist = (eyeSize / 2) - (pupilSize / 2);
// calculate the distance between the mouse position and the eye center
let distanceFromCenter = dist(mouseX, mouseY, x, y);
// restrain the distance iris and pupil move so they stay inside the eye boundary
let moveDist = min(distanceFromCenter, maxDist);
// calculate the new iris and pupil positions along the angle
let irisX = x + cos(angle) * moveDist;
let irisY = y + sin(angle) * moveDist;
// determine if the mouse is near the edge or outside the eye
let edgeThreshold = (eyeSize / 2) - irisSize;
let stretch;
// apply stretch factor when near the edge or outside the eye
if (distanceFromCenter >= edgeThreshold) {
stretch = map(distanceFromCenter, edgeThreshold, eyeSize / 2, 1, 0.5);
} else {
// no stretch inside the inner part of the eye
stretch = 1;
}
// constrain stretch between 0.5 and 1
stretch = constrain(stretch, 0.5, 1);
// draw the blue iris with dynamic stretch
push();
translate(irisX, irisY); // move to iris position
rotate(angle); // rotate based on the angle to the mouse
fill("#275AF2");
ellipse(0, 0, irisSize * stretch, irisSize);
// draw the black pupil with dynamic stretch
fill(0);
ellipse(0, 0, pupilSize * stretch, pupilSize);
pop();
}