Submodule 17.1: Events

Site: ΕΛ/ΛΑΚ Moodle
Course: Study / Web Design and Web Development
Book: Submodule 17.1: Events
Printed by: Guest user
Date: Thursday, 21 November 2024, 5:19 PM

Description

  • Introduction to p5.js events
  • mouseX and mouseY
  • mouseClicked()
  • mouseIsPressed

Introduction to p5.js events

P5.js events have built-in events that enable you to apply interactivity in your canvas.

Using these functions you can apply changes to your sketch/animation when the user does something.

For example, you can change the color of a shape when the user clicks on it.

Let's see some of these events

mouseX and mouseY

Two of the p5.js events are the mouseX and mouseY.

mouseX holds the horizontal position at which the user points its mouse. It changes everytime the user moves its mouse horizontally. 

Similar to mouseX, mouseY holds the vertical position at which the user holds its mouse. 

Example 

Here is an example code which creates a simple drawing tool: 

function setup() {
  createCanvas(windowWidth, windowHeight);
  // set background color in the setup so sketches will leave traces
  background('#D66761');
}
function draw() {
  noStroke();
  fill(241, 222, 152, 30);
  // ellipse will change according to the mouseX and mouseY position
  ellipse(mouseX, mouseY, 30, 30);
}

You can see the result of the above code, click here.

Exercise

  1. Open your Visual Studio editor and the p5yourName folder.
  2. Open the file ex812.js in your editor and save it as ex912.js
  3. Open the file ex812.html in your editor and save it as ex912.html
  4. In the ex912.html file, update the link to ex912.js  from exersice812.js
  5. Go to the index.html file and create, under Module 9, a link to the ex912.html  file with the title "mouseX and mouseY".

Modify the ex912.jsfile to create an ellipse that will change color according to the mouseX and mouseY position. You can see here an example.

Answer:

function setup() {
  createCanvas(800, 500);
  // set background color 
  background('#D66761');
}
function draw() {
  // we use mouseX in order to control the green value of the color
  // we divide it by 4 so that the value will not exceed 256
  // for the blue value we use mouseY divided by 2
  fill(241, mouseX/4, mouseY/2);
  // ellipse will change color according to mouseX and mouseY position
  ellipse(400, 250, 200);
}

Do a Git commit with the message "mouseX and mouseY".

  • See more about the mouseX event
  • See more about the mouseY event

mouseClicked()

The mouseClicked() function is called everytime the left mouse button is clicked. 

In our code, we place this function outside the draw().

The syntax is:

 function mouseClicked(){
//your code
}
Example 

Here is an example code which will be drawn an ellipse in the position where we point our mouse, everytime we click the left mouse button:

let xPos, yPos;
function setup() {
  createCanvas(800, 500);
  // set background color
  background('#D66761');
}
function draw() {
  fill(241, 213, 145);
  // an ellipse will be drawn everytime the xPos, yPos are defined
  ellipse(xPos, yPos, 50);
}
// everytime the mouse is clicked
function mouseClicked(){
  // the x position will take the value of the horizontal mouse position
  xPos=mouseX;
  // the y position will take the value of the vertical mouse position
  yPos = mouseY;
}

You can see the result of the above code, click here.

Exercise

  1. Open your Visual Studio editor and the p5yourName folder.
  2. Open the file ex812.js in your editor and save it as ex913.js
  3. Open the file ex812.html in your editor and save it as ex913.html
  4. In the ex913.html file, update the link to ex913.js  from exersice812.js
  5. Go to the index.html file and create, under Module 9, a link to the ex913.html  file with the title "mouseClicked()".

Modify the ex913.jsfile to create an ellipse which will change its color from white to red, everytime the user clicks inside it.If the user clicks outside the ellipse, change its color back to white. You can see here an example.

Answer:

let i = 'white';
function setup() {
  createCanvas(800, 500);
  // set background color
  background('#D66761');
}
function draw() {
  fill(i);
  // create an ellipse
  ellipse(400, 250, 50);
}
// everytime the mouse is clicked
function mouseClicked(){
  // if the mouse x and y position is inside the ellipse
  if (mouseX >=350 && mouseX <=450 && mouseY >=200 && mouseY <=300){
    //change its color to red
    i = 'red';
  }
  // else change the color to white
  else {
    i = 'white';
  }
}

Do a Git commit with the message "mouseClicked() ".

mouseIsPressed

mouseIsPressed is a boolean variable which changes from false to true when the mouse is pressed. 

This variable stays true as long as the mouse is pressed, and when it is released it changes back to false.

Example 

Here is an example code which draws an ellipse in the position where the mouse is pointing, as long as the mouse is pressed: 

function setup() {
  createCanvas(800, 500);
}
function draw() {
  // set background inside draw so that the ellipse will not leave traces
  background('#D66761');
  fill(241, 213, 145);
  // if the mouse is pressed
  if (mouseIsPressed){
    // an ellipse will be drawn in the position where the mouse is pointing
    ellipse(mouseX, mouseY, 50);
  }
}

You can see the result of the above code, click here.

Exercise

  1. Open your Visual Studio editor and the p5yourName folder.
  2. Open the file ex812.js in your editor and save it as ex914.js
  3. Open the file ex812.html in your editor and save it as ex914.html
  4. In the ex914.html file, update the link to ex914.js  from exersice812.js
  5. Go to the index.html file and create, under Module 9, a link to the ex914.html  file with the title "mouseIsPressed".

Modify the ex914.jsfile to create a drawing tool which will be drawing ellipses as long as the mouse is pressed. Also, include two other ellipses, a red and a green one. Make the drawing tool change the color of the ellipses it is drawing to red, if the user clicks inside the red ellipse and to green if the user clicks inside the green ellipse. You can see here an example.

Answer:

let mycolor = 'white';
function setup() {
  createCanvas(800, 500);
  background('#D66761');
}
function draw() {
  // this color will change every time the mouse clikcs the red or green ellipse
  fill(mycolor);
  // if the mouse is pressed
  if (mouseIsPressed){
    // an ellipse will be drawn in the position where the mouse is pointing
    ellipse(mouseX, mouseY, 20);
  }
  fill ('red');
  ellipse (600,250,50);
  fill('green');
  ellipse (600,350,50);
}
// everytime the mouse is clicked
function mouseClicked(){
  // if the mouse x and y position is inside the red ellipse
  if (mouseX >=550 && mouseX <=650 && mouseY >=200 && mouseY <=300){
    //change its color to red
    mycolor = 'red';
  }
  // if the mouse x and y position is inside the green ellipse
  if (mouseX >=550 && mouseX <=650 && mouseY >=300 && mouseY <=400){
    //change its color to green
    mycolor = 'green';
  }
}

Do a Git commit with the message "mouseIsPressed".