Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Mouse and Keyboard Input | Transformations and Interactivity
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Generative Art with JavaScript and P5.js

bookMouse and Keyboard Input

index.html

index.html

copy

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.

question mark

Which variables in P5.js provide the current position of the mouse?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you show an example of using mouseX and mouseY in a sketch?

How do I use keyPressed() to change something in my sketch?

What other events can I respond to in P5.js?

bookMouse and Keyboard Input

Sveip for å vise menyen

index.html

index.html

copy

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.

question mark

Which variables in P5.js provide the current position of the mouse?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2
some-alt