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:
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