Open In App

p5.js | Mouse | winmouseX

The winmouseX variable in p5.js is used to store the current horizontal position of the mouse, relative to (0, 0) of the window.

Syntax:



mouseX

Below programs illustrate the mouseX variable in p5.js:

Example 1: This example uses winmouseX variable to display its position.






function setup() {
      
    // Create canvas of given size
    createCanvas(1000, 400);
      
    // Set the text size
    textSize(20); 
}
   
function draw() {
      
    // Set the background color
    background(200);
      
    // Create rectangle
    rect(winMouseX, winMouseY, 10, 10);
      
    // Display position of winmouseX
    text("Position of winmouseX is " 
        + winMouseX, 30, 40);
}

Output:

Example 2: This example uses winmouseX variable to display some content if condition is satisfied.




function setup() {
  
    // Create canvas of given size 
    createCanvas(500, 500);
      
    // Set the text size
    textSize(20); 
}
   
function draw() {
      
    // Set the background color
    background(200);
      
    // Create circle
    circle(winMouseX, winMouseY, winMouseX-winMouseY);
      
    // Create line
    line(0, 0, windowWidth, windowHeight);
      
    // Check condition and execute statement
    if(winMouseX!=winMouseY ) {
        text("You Lose", 12, 34);
    }
}

Output:

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


Article Tags :