Mouse and Keyboard Input
index.html
You can make your P5.js sketches interactive by responding to mouse and keyboard events. Two built-in variables, mouseX and mouseY, always contain the current position of the mouse cursor within the canvas. These variables update automatically as the mouse moves, allowing you to draw shapes that follow the mouse or react to its position.
To respond to keyboard input, P5.js provides special functions like keyPressed(). This function is called automatically whenever you press any key. By placing code inside keyPressed(), you can change colors, move objects, or trigger any behavior you want in response to a key press.
This style of programming, where your code reacts to user actions such as moving the mouse or pressing a key, is called event-driven programming. Instead of following a fixed sequence, your sketch waits for events and responds to them, making your art interactive and engaging.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 6.67
Mouse and Keyboard Input
Swipe to show menu
index.html
You can make your P5.js sketches interactive by responding to mouse and keyboard events. Two built-in variables, mouseX and mouseY, always contain the current position of the mouse cursor within the canvas. These variables update automatically as the mouse moves, allowing you to draw shapes that follow the mouse or react to its position.
To respond to keyboard input, P5.js provides special functions like keyPressed(). This function is called automatically whenever you press any key. By placing code inside keyPressed(), you can change colors, move objects, or trigger any behavior you want in response to a key press.
This style of programming, where your code reacts to user actions such as moving the mouse or pressing a key, is called event-driven programming. Instead of following a fixed sequence, your sketch waits for events and responds to them, making your art interactive and engaging.
Thanks for your feedback!