p5.js mouseDragged() Function

The mouseDragged() function in p5.js is used to check the mouse drags (mouse moves and mouse button pressed). It is invoked each time when the mouse drags. If mouseDragged() function is not defined, then touchMoved() function will be used instead of mouseDragged() function.
Syntax:
mouseDragged(Event)
Below programs illustrate the mouseDragged() function in p5.js:
Example 1: This example uses mouseDragged() function to change the background color.
| functionsetup() {  Â    // Create Canvas     createCanvas(500, 500); }   Âlet value = 0;  Âfunctiondraw() {      Â    // Set background color     background(200);      Â    // Set filled color     fill(value);      Â    // Create rectangle     rect(25, 25, 460, 440);      Â    // Set text color     fill('lightgreen');      Â    // Set font size     textSize(15);      Â    // Display result     text('Drag Mouse Across the page to change its value.',         windowHeight/6, windowWidth/4); }  ÂfunctionmouseDragged() {     value = value + 5;      Â    if(value > 255) {         value = 0;     } }  | 
Output:
Example 2: This example uses mouseDragged() function to change the mouse cursor circle color.
| let value;  Âfunctionsetup() {      Â    // Create Canvas     createCanvas(500, 500); }   Âfunctiondraw() {      Â    // Set background color     background(200);       Â    // Set filled color     fill('green');      Â    // Set text and text size     textSize(25);      Â    text('Drag mouse to change color', 30, 30);      Â    // Fill color according to     // mouseMoved() function     fill(value, 255-value, 255-value);      Â    // Draw ellipse       ellipse(mouseX, mouseY, 115, 115); }  ÂfunctionmouseDragged() {     value = mouseX%255; }  | 
Output:
Reference: https://p5js.org/reference/#/p5/mouseDragged
 
				 
					



