We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Draw things on Layers
Page Index Toggle Pages: 1
Draw things on Layers (Read 493 times)
Draw things on Layers
May 29th, 2006, 7:44pm
 
Hello,

I wonder if it's possible to draw things on different layers, e.g. I draw an ellipse, then I'd like to draw another on UNDER it, so that the first one (partly) covers the second.

I'm used to this feature from flash, but I'm not sure whether it's possible at all with p5's built in functions, since drawn "things" aren't treated as objects like the Flash-MovieClips.

Can anybody help?
(please excuse the mistakes I make, I'm from Germany Wink
Re: Draw things on Layers
Reply #1 - May 29th, 2006, 8:02pm
 
Is there a reason you cannot draw them in order?

There are a few solutions, but they vary as far as being ideal.  You could mess with the z-buffer stuff (which only works in 3d modes) and draw things with different z coordinates.  You could draw to separate PImages as your layers then draw them to the screen.  Or you could simply keep track and draw stuff in the correct order...

Marcello
Re: Draw things on Layers
Reply #2 - May 29th, 2006, 8:16pm
 
hallo hannes,

as cello said, the easiest way to do this is by drawing to lowest shapes first.

another way would be to create yourself a layer-class. something like a simple drawing-manager.

maybe post some code so we can see what you are trying to do ...

F
Re: Draw things on Layers
Reply #3 - May 29th, 2006, 8:46pm
 
Hi,

it really is no problem to draw things in correct order, but it would be a bit more comfortable if it was possible... anyway, so it just helps to improve my skills and discipline Wink

I just started playing with p5 today, this is the script:

Code:



color circleColor;
color circleBGColor;

int test = 0;
float test2 = 0;

int numOfCircles = 50;
int stageWidth = 300;
int stageHeight = 300;
int circleWidth = 40;
int circleBorderWidth = 50;
int newPosRadius = 40;
int minXPos = circleWidth + 10;
int maxXPos = stageWidth - minXPos;
int minYPos = circleWidth + 10;
int maxYPos = stageHeight - minYPos;


int frame = 0;

Circle[] circles = new Circle[numOfCircles];

void setup() {
size(stageWidth, stageHeight);
colorMode(RGB, 100);
circleColor = color(100, 50, 0);
circleBGColor = color(100, 100, 100);
smooth();
background(20);
framerate(30);

for (int i = 0; i < numOfCircles; i++) {
circles[i] = new Circle(int(random(minXPos, maxXPos)), int(random(minYPos, maxYPos)));
}
}

void draw() {
background(20);

for (int i = 0; i < numOfCircles; i++) {
circles[i].moveTowardsTarget();
circles[i].drawMyBorder();
}

for (int i = 0; i < numOfCircles; i++) {
circles[i].drawMe();
}


}




class Circle {
int xPos, yPos;
int targetX, targetY;

Circle(int x, int y) {
xPos = x;
yPos = y;
setNewTarget();
}

void drawMe() {
fill(circleColor);
noStroke();
ellipse(xPos, yPos, circleWidth, circleWidth);
}

void drawMyBorder() {
fill(circleBGColor);
noStroke();
ellipse(xPos, yPos, circleBorderWidth, circleBorderWidth);
}

void setNewTarget() {
float alpha = random(TWO_PI);
if (aroundMouse(xPos, yPos, circleWidth)) {
test2 = abs(atan2(mouseX - xPos, mouseY - yPos));
if (test2 > TWO_PI) println("WHOAAAAAA");
println("yea" + test2);
}
targetX = xPos + int(newPosRadius * cos(alpha));
targetY = yPos + int(newPosRadius * sin(alpha));

if (!inBounds(targetX, targetY))
setNewTarget();
//if (mousePressed && aroundMouse(targetX, targetY, circleWidth))
// setNewTarget();
}

void moveTowardsTarget() {

if (diff(xPos, targetX) <= 7 && diff(yPos, targetY) <= 7)
setNewTarget();

xPos = xPos - int((xPos - targetX) / 8);
yPos = yPos - int((yPos - targetY) / 8);
}
}


int diff(int a, int b) {
return(abs(a - b));
}

boolean inBounds(int x, int y) {
if (x >= minXPos && x <= maxXPos && y >= minYPos && y <= maxXPos) {
return true;
} else {
return false;
}
}

boolean aroundMouse(int x, int y, int radius) {
if (q(diff(x, mouseX)) + q(diff(y, mouseY)) < q(radius)) {
return true;
} else {
return false;
}
}

int q(int a) {
return a*a;
}



Page Index Toggle Pages: 1