Hi everyone,
I try to translate indefinitely a loop of rectangles, but it doesn’t work properly
my code :
pushMatrix() ;
pushStyle();
fill(255);
for (float i=0; i < width; i = i + 100) {
for (int x = 0; x <= width; x += 120) {
translate(i, 0);
rect(x, 0, 60, height);
}
}
popStyle();
popMatrix();
One issue i see is that you’re translating by i yet have that jumping through the loop by 100, the screen is only 100,100 as default i believe, so it is working its just drawing the next rectangle off the screen, if you put it all in an active sketch by using setup and draw then make the screen size bigger you can see whats happening
EDIT: Turns out you dont even need the setup and draw, just add a size(1000,500); at the beginning
void setup() {
size(1000, 100);
}
void draw() {
pushMatrix() ;
pushStyle();
fill(255);
for (float i=0; i < width; i = i + 100) {
for (int x = 0; x <= width; x += 120) {
translate(i, 0);
rect(x, 0, 60, height);
}
}
popStyle();
popMatrix();
}
Thank’s for help.
I got my loop with stripes on the all screen :
pushMatrix() ;
pushStyle();
fill(255);
for (int x = 0; x <= width; x += 120) {
rect(x, 0, 60, height); // stripes all over the screen
}
popStyle();
popMatrix();
I want to make a translation of all the stripes from left to right, with no end of stripes
Yves
int visiblebars = 10;
int barwidth;
int barwidthtwice;
int shift;
int speed = 1;
PGraphics pg;
void setup() {
size(1000,600,P2D);
barwidth = width/visiblebars;
barwidthtwice = 2*barwidth;
int[] colors = {color(0,0,255), color(255,0,255)};
pg = createGraphics(width+barwidthtwice, height, P2D);
pg.beginDraw();
pg.noStroke();
for (int x = 0; x < visiblebars+2; x++) {
pg.fill(colors[x % 2]);
// without translate do
// pg.rect(x*barwidth,0,(x+1)*barwidth,height);
// if you want to use translate do this
// pg.pushMatrix();
// pg.translate(x*barwidth,0);
// pg.rect(0,0,barwidth,height);
// pg.popMatrix();
// or even shorter this
pg.translate(x>0 ? barwidth : 0 , 0);
pg.rect(0,0,barwidth,height);
}
pg.endDraw();
shift = 0;
}
void draw() {
// for left shift use
image(pg, -shift, 0);
// or for right shift use
// image(pg, -barwidthtwice+shift, 0);
shift = (shift+speed) % barwidthtwice;
}