p5.js mouseMoved() Function
Last Updated :
04 Mar, 2021
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.
function setup() {
createCanvas(500, 500);
}
let value = 0;
function draw() {
background(200);
fill(value);
rect(25, 25, 460, 440);
fill( 'lightgreen' );
textSize(15);
text( 'Move Mouse Across the page to change its value.' ,
windowHeight/6, windowWidth/4);
}
function mouseMoved() {
value = value + 5;
if (value > 255) {
value = 0;
}
}
|
Output:
Example 2: This example uses mouseMoved() function change the color of ellipse.
let value;
function setup() {
createCanvas(500, 500);
}
function draw() {
background(200);
fill(value, value, value);
ellipse(mouseX, mouseY, 115, 115);
}
function mouseMoved() {
value = mouseX%255;
}
|
Output:
Reference: https://p5js.org/reference/#/p5/mouseMoved
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...