Translate with mouse?
in
Programming Questions
•
2 years ago
Hello everybody! Hope you can help me out :)
I've made this little program were if you press the mouse all these balls execute from a while loop. If I click on the 0, 0 coordinates of the canvas I get all the balls going to the center of the screen which is fine but I want this to happen when you click in the center of the screen instead on the 0, 0 coordinates of the canvas.
As a second "improvement" I would like to make the 0, 0 coordinates follow the mouse position on the canvas. I guess this must be done with the translate function but I'm not able to do it.
I'm slowly learning how to code so apologize if my code looks terrible... hehe
Thank you!
I've made this little program were if you press the mouse all these balls execute from a while loop. If I click on the 0, 0 coordinates of the canvas I get all the balls going to the center of the screen which is fine but I want this to happen when you click in the center of the screen instead on the 0, 0 coordinates of the canvas.
As a second "improvement" I would like to make the 0, 0 coordinates follow the mouse position on the canvas. I guess this must be done with the translate function but I'm not able to do it.
I'm slowly learning how to code so apologize if my code looks terrible... hehe
Thank you!
- void setup() {
- size(screen.width, screen.height);
- ellipseMode(CENTER);
- colorMode(HSB);
- background(174, 25, 0);
- smooth();
- }
- void draw() {
- // Background Fade Out Effect
- frameRate(8);
- fill(174, 25, 0, 30);
- noStroke();
- rect(0, 0, width, height);
- // Pelotitas Function Calling
- if(mousePressed) {
- pelotitas(mouseX/2, mouseY/2);
- }
- }
- void pelotitas(int x, int y) {
- // Variables declaration
- float i = random(50);
- float u = random(50);
- int j = 15;
- // Translation to make circle in the middle of the screen, not in 0,0
- translate(width/2, height/2);
- // While Loop
- while(i < width & u < height) {
- noStroke();
- fill(174, random(0, 99), random(0, 99));
- rotate(random(5, 30));
- ellipse(random(x, i), random(y, i), random(j), j);
- i++;
- u++;
- }
- }
1