Assignment 1 – Self Portrait
For this weeks assignment, I wanted to create a simple image of myself but have the mood / expression and weather change depending on the user’s interactivity. As a hijabi, I like to change the colour of my hijab and so I wanted it be changeable by the user pressing the mouse. I also enjoy the city life so I wanted to replicate some sort of simple skyline.
Emotions
I decided to go with 4 different emotions inspired from the following emojis.
cheesing. I wanted this one to be me at my happiest with a simple rainbow in the background.
sad. I wanted it to rain in the background and have my facial expression be a simple upside down arc.
neutral. I wanted this one to be me just meh, with nothing interesting in the background.
happy. I wanted this one to be me just normally happy with the sun in the background.
Previous coding experience with p5.js
I took Decoding Nature with Aya so I have had exposure to replicating natural patterns in p5.js so I wanted to replicate the rain pouring down with my old skill set learnt from her class.
For the rain pouring down effect, I wanted to have simple white lines
// Draw rain stroke(255, 255, 255); // 'white' rain strokeWeight(2); for (let i = 0; i < 20; i++) { let x = random(0, 600); let y = random(0, 400); line(x, y, x + 1, y + 5); // rain drops }
Here I am generating lines and having their starting (x,y) coordinates randomly generated from any x coordinate of the screen and y coordinates of 0 to 400. I then have the line stretched out a little to the x by one pixel to create a more angled look.
For the eyes, I wanted them to also be interactive so I made them follow the cursor of the user.
else { // normal eyes - white background and black pupils that follow cursor noStroke(); // white eye fill(255); ellipse(270, 180, 25, 25); ellipse(330, 180, 25, 25); // black pupils that follow mouse fill(0); // calculate pupil position based on mouse location let leftEyeX = 270; let leftEyeY = 180; let rightEyeX = 330; let rightEyeY = 180; // calculte teh angle from eye to mouse let leftAngle = atan2(mouseY - leftEyeY, mouseX - leftEyeX); let rightAngle = atan2(mouseY - rightEyeY, mouseX - rightEyeX); // LIMIT pupil movement within eye bounds let pupilDistance = 4; let leftPupilX = leftEyeX + cos(leftAngle) * pupilDistance; let leftPupilY = leftEyeY + sin(leftAngle) * pupilDistance; let rightPupilX = rightEyeX + cos(rightAngle) * pupilDistance; let rightPupilY = rightEyeY + sin(rightAngle) * pupilDistance; // drawing the pupils ellipse(leftPupilX, leftPupilY, 8, 8); ellipse(rightPupilX, rightPupilY, 8, 8);
From decoding nature, we learnt how to have particles follow our cursor by making use of the mouseX, mouseY variables. I had the pupils initial X,Y coordinates be the same as the white circles and then limit their distance to 4 so that they don’t go out of bound of the white circles.
The use of atan2() is used to calculate the angle from each eye center to the mouse position and returns an angle in radians. It is then used in the sin and cos formulas with basic trigonometry to create a new X,Y coordinate. Because the value of sin and cos will always be between -1 and 1, when multiplied by our pupilDistance (constraint we implemented earlier) it will never go out of bound of the white part of the eye.