p5.js | Mouse | pmouseX
The pmouseX variable in p5.js is used to store the horizontal position of the mouse cursor in the frame previous to the currently active frame, relative to the origin of the canvas.
Syntax:
mouseX
Below programs illustrate the pmouseX variable in p5.js:
Example 1: This example uses pmouseX variable to draw mouse pointer as circle.
function setup() { // Create canvas createCanvas(1000, 400);} function draw() { // Set the background color background(244, 248, 252); // Fill color relative pmouseX, // pmouseY and mouseY fill(pmouseX%255, pmouseY%255, mouseY%255); // Draw circle circle(pmouseX, mouseX, pmouseX%100);} |
Output:
Example 2: This example uses pmouseX variable to display the position of mouse pointer.
function setup() { // Create canvas createCanvas(1000, 400); // Set font size textSize(20);} function draw() { // Set background color background(200); // Display result text("Position of pmouseX is " + pmouseX, 30, 40);} |
Output:
Reference: https://p5js.org/reference/#/p5/pmouseX
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!



