p5.js mouseMoved() Function

The mouseMoved() function in p5.js is called every time the mouse moves and a mouse button is not pressed.
Syntax:
mouseMoved(Event)
Parameters: This function accepts single parameter Event which is optional.
Below programs illustrate the mouseMoved() function in p5.js:
Example 1: This example uses mouseMoved() function change the rectangle color when mouse move over.
| functionsetup() {  Â    // Create Canvas of size 500*500     createCanvas(500, 500); }   Âlet value = 0; functiondraw() {      Â    // SEt background color     background(200);      Â    // Set the filled color     fill(value);      Â    // Create rectangle of given size     rect(25, 25, 460, 440);      Â    // Set the text color     fill('lightgreen');      Â    // Set font size     textSize(15);      Â    // Display the text     text('Move Mouse Across the page to change its value.',               windowHeight/6, windowWidth/4); }  ÂfunctionmouseMoved() {      Â    value = value + 5;      Â    if(value > 255) {         value = 0;     } }  | 
Output:
Example 2: This example uses mouseMoved() function change the color of ellipse.
| // Declare a variable let value;  Âfunctionsetup() {      Â    // Create Canvas of size 500*500     createCanvas(500, 500); }   Âfunctiondraw() {      Â    // Set background color     background(200);       Â    // fill color according to      // mouseMoved() function      Â    // Set the color     fill(value, value, value);      Â    // Draw ellipse       ellipse(mouseX, mouseY, 115, 115); }  ÂfunctionmouseMoved() {     value = mouseX%255; }  | 
Output:
Reference: https://p5js.org/reference/#/p5/mouseMoved
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
 
				 
					



