Update object color depending on mouse position
in
Programming Questions
•
2 years ago
I have a bezier with rectangles draw along its points, and what I would like to do is change the color of these rectangles as the dial points to them. This piece of code I have wrote changes the color of the rectangle the dial points to however I would like it to also keep the color the same for all of the rectangles that the dial has past, and then reset if the dial goes back down. The dials rotation is determined to the mouseX position.
Thank you
I realise that is a bit of an odd explanation so please ask questions if you don't quite understand what I am trying to do.
- void setup() {
- size(500, 500);
- smooth();
- background(0);
- }
- void draw() {
- noFill();
- noStroke();
- bezier(85, 20, 50, 50, 50, 20, 20, 20);
- fill(0);
- stroke(57, 181, 74);
- int steps = 90;
- rectArray();
- int mrot = round(map(mouseX, 0, 500, 0, 180));
- float marot = round(mrot/2);
- if (marot <= steps) {
- //
- float t = marot / float(steps);
- float x = bezierPoint(10, 10, 490, 490, t);
- float y = bezierPoint(485, 200, 200, 485, t);
- pushMatrix();
- translate(x,y);
- int rot = round(map(x, 10, 490, 0, 180));
- float arot = radians(rot);
- rotate(arot);
- fill(57, 181, 74);
- rect(0, 0, 25, 2);
- popMatrix();
- noStroke();
- fill(0,0,0,25);
- rect(0,0,500,500);
- }
- dial();
- }
- void dial() {
- pushMatrix();
- translate(250,485);
- fill(0);
- noStroke();
- ellipse(0, 15, 400, 400);
- fill(255,255,255);
- int mrot = round(map(mouseX, 0, 500, 0, 180));
- float marot = radians(mrot);
- rotate(marot);
- quad(0, 10, -192, 0, 0, -10, 10, 0);
- popMatrix();
- }
- void rectArray() {
- int steps = 90;
- for (int i = 0; i <= steps; i++) {
- float t = i / float(steps);
- float x = bezierPoint(10, 10, 490, 490, t);
- float y = bezierPoint(485, 200, 200, 485, t);
- pushMatrix();
- translate(x,y);
- int rot = round(map(x, 10, 490, 0, 180));
- float arot = radians(rot);
- rotate(arot);
- fill(0);
- rect(0, 0, 25, 2);
- popMatrix();
- }
- }
I don't know if the best way to achieve the outcome I want would be through the use of arrays or another method so I am open to all suggestions.
Liam
1