Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Hello Mouse
In this project, your mouse will keep shooting lines towards a point; move the mouse and you will draw a unique line of stars. Press the mouse to restart the drawing.
Sketch
int pointX = 172;
int pointY = 88;
void setup() {
size(400, 400);
stroke(255);
background(192, 16, 18);
}
void draw() {
line(pointX, pointY, mouseX, mouseY);
}
void mousePressed() {
pointX=mouseX;
pointY=mouseY;
background(192, 16, 18);
}
How it works?
The previous project was drawing a single image without any animation or interaction.
If we want to make an interactive sketch, we need to add the setup()
and draw()
functions (these are built-in functions that are called automatically) to build the frame.
setup()
: Executed only once at the start of the sketch.draw()
: Executed repeatedly, where we usually add the sketch for drawing the animation.
int pointX = 172;
int pointY = 88;
void setup() {
size(400, 400);
stroke(255);
background(192, 16, 18);
}
void draw() {
line(pointX, pointY, mouseX, mouseY);
}
This sketch above already works smoothly as an interactive sketch.
Next you can add a mouse click event. This event can be implemented with the mousePressed()
function, where we add statements to refresh the target point and clear the screen.
int pointX = 172;
int pointY = 88;
void setup() {
size(400, 400);
stroke(255);
background(192, 16, 18);
}
void draw() {
line(pointX, pointY, mouseX, mouseY);
}
void mousePressed() {
pointX=mouseX;
pointY=mouseY;
background(192, 16, 18);
}
For more please refer to Processing Reference.