p5.js | Mouse | mouseX
Last Updated :
16 Apr, 2019
The mouseX variable in p5.js is used to store the current horizontal position of the mouse, relative to (0, 0) of the canvas. If touch is used instead of mouse input, mouseX will hold the x value of the most recent touch point.
Syntax:
mouseX
Below programs illustrate the mouseX variable in p5.js:
Example 1: This example uses mouseX variable to display position.
function setup() {
createCanvas(1000, 400);
textSize(20);
}
function draw() {
background(200);
rect(mouseX, height/2, 30, 30);
text( "Position of mouse relative to canvas is "
+ mouseX, 30, 40);
}
|
Output:
Example 2: This example uses mouseX variable to display position.
function setup() {
createCanvas(1000, 400);
textSize(20);
}
function draw() {
background(200);
circle(mouseX, mouseY, 30);
text( "Position of mouseX relative to canvas: "
+ mouseX, 30, 40);
}
|
Output:
Reference: https://p5js.org/reference/#/p5/mouseX
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...