Skip to content
Related Articles

Related Articles

Improve Article

p5.js | Mouse | mouseY

  • Last Updated : 16 Apr, 2019

The mouseY variable in p5.js is used to store the current vertical position of the mouse, relative to (0, 0) of the canvas. If touch is used instead of mouse input, mouseX will hold the y value of the most recent touch point.

Syntax:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

mouseY

Below programs illustrate the mouseY variable in p5.js:

Example 1: This example uses mouseY variable to display vertical position ofmouse.






function setup() {
  
    // Create canvas
    createCanvas(1000, 400);
      
    // Set the font size
    textSize(20);
}
   
function draw() {
      
    // Set background color
    background(200);
      
    // Create rectangle
    rect(height/2, mouseY, 30, 30);
      
    // Display position of mouse on Y-axis
    text("Position of mouseY relative to canvas is: "
            + mouseY, 30, 40);
}

Output:

Example 2: This example uses mouseY variable to display vertical position ofmouse.




function setup() {
  
    // Create canvas
    createCanvas(1000, 400);
      
    // Set the font size
    textSize(20);
}
   
function draw() {
      
    // Set background color
    background(200);
      
    // Create circle
    circle(mouseX, mouseY, 30);
      
    // Display position of mouse on Y-axis
    text("Position of mouseY relative to canvas is: "
            + mouseY, 30, 40);
}

Output:

Reference: https://p5js.org/reference/#/p5/mouseY




My Personal Notes arrow_drop_up
Recommended Articles
Page :