We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi-
Please have a look at this sketch. http://codepen.io/edhebert/pen/GraYLZ?editors=0010
I'm drawing the stem (line) prior to the petals (which are drawn with a rotation). Yet the line appears atop the petals (but beneath the yellow flower center, which is drawn at the end of sketch.)
Note the noLoop() call that's commented out in setup(). If I uncomment that, the sketch draws as expected, with the stem behind the petals.
Here's the code, for those who don't feel like looking at the codepen:
var petal_angle = 0;
function setup() {
createCanvas(windowWidth, windowHeight);
background(0);
// noLoop();
}
function draw() {
//make center of canvas the 0, 0 origin
translate(width / 2, height / 2);
var petal_color = color(128, 212, 255);
//define one petal as an object
var petal = {
x: 120,
y: 0,
petal_width: 150,
petal_height: 75
}
//draw stem
stroke(0,128,0);
strokeWeight(20);
line(0, 0, 0, height / 2 - 50);
fill(petal_color);
noStroke();
push();
for (petal_angle; petal_angle < 360; petal_angle += 45) {
rotate(radians(petal_angle));
ellipse(petal.x, petal.y, petal.petal_width, petal.petal_height);
}
pop();
//draw center of flower on top of petals
fill(255, 255, 0);
ellipse(0, 0, 130, 130);
}
Answers
Most likely the problem is you've made iterator petal_angle a global
var
.And you don't reset it back to
0
before eachfor ( ; ; ) {}
loop! #-oAlso you should move
background(0);
to the top of draw()! L-)Anyways, I did some tweaks to your sketch. Visit the link below for it: :bz
http://Bl.ocks.org/GoSubRoutine/367742ecc1a78c303dc100cefc3e20cd http://CodePen.io/GoSubRoutine/pen/BpevPE/right?editors=001
Thanks, Goto...the obvious answer was the global for petal angle in the for loop, which I embarrassingly didn't notice.
Interesting code rework as well. Why did you choose to bitshift width and height instead of just dividing by two? Elegance? Speed?
Umm... I find it interesting. Although it can also be slightly faster. ;))
Another extra bonus is that a division can result a fractional value, while bitshifting is always whole.