JavaScript
x
36
36
1
function setup() {
2
createCanvas(400, 400);
3
}
4
5
function draw() {
6
background(220);
7
petals();
8
noStroke(); //the center of flower = white circle
9
fill(255);
10
ellipse(200, 200, 130, 130);
11
12
function petals() { //flower leaves -> I wanna fill them with rainbow colors each in shortly
13
push()
14
translate(200, 200);
15
rotate(radians(90));
16
noStroke();
17
rotate(radians(45));
18
ellipse(100, 0, 250, 60)
19
rotate(radians(45));
20
ellipse(100, 0, 250, 60)
21
rotate(radians(45));
22
ellipse(100, 0, 250, 60)
23
rotate(radians(45));
24
ellipse(100, 0, 250, 60)
25
rotate(radians(45));
26
ellipse(100, 0, 250, 60)
27
rotate(radians(45));
28
ellipse(100, 0, 250, 60)
29
rotate(radians(45));
30
ellipse(100, 0, 250, 60)
31
rotate(radians(45));
32
ellipse(100, 0, 250, 60)
33
pop()
34
35
}
36
}
JavaScript
1
1
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.js"></script>
How can I color those leaves?
I added fill('red')
, fill('blue')
in front of each ellipse and somehow only one color dominates all of the petals.
Plus, I want it to rotate at a constant speed.
Advertisement
Answer
Fill seems to work fine. (and added a loop to be more concise)…
JavaScript
1
26
26
1
function setup() {
2
createCanvas(400, 400);
3
}
4
5
function draw() {
6
background(220);
7
petals();
8
noStroke(); //the center of flower = white circle
9
fill(255);
10
ellipse(200, 200, 130, 130);
11
12
function petals() { //flower leaves -> I wanna fill them with rainbow colors each in shortly
13
push()
14
translate(200, 200);
15
noStroke();
16
const colors = ['red', 'yellow', 'blue', 'green'];
17
for (let i=0; i<8; i++) {
18
let color = colors[i%4];
19
fill(color)
20
ellipse(100, 0, 250, 60)
21
rotate(radians(45));
22
}
23
pop()
24
25
}
26
}
JavaScript
1
1
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.js"></script>