restoring static background after animated object moves through scene.
in
Programming Questions
•
8 months ago
This is one of my first scripts using animation. I lay out the background elements in setup(). This is a picture of sky, moon, ocean, and foreground. These elements never change.
Using draw() I move a barge with blinking light across the horizon and I also have a cloud moving across the sky.
Question - how to I restore the background after the barge (and the cloud) has changed position. I'm looking for the most elegant/practical/general way. I guess I'm looking for a function or procedure that redraws the whole static background before each new update of the barge and cloud elements.
I guess I could redo the whole generation of the background in draw(), but I'm sure there's a much better way.
Code is below. Note that a substantial part of the code in the setup() section has been commented out. This code simply generated static snowflakes in the scene.
Thanks.
===============================
/** My Animation_1
* Processing: test programs
* by rl777 */
int i,j,lines,toggle;
float cx, cy, x1, x2, y1, y2, xdelta, ydelta;
float horizon, horizon2,gap;
float shippos;
float px, py, px2, py2;
PrintWriter output;
void setup(){
rectMode(CORNERS);
size(800, 800);
background(0);
fill(100);
rect(20,20,780,780);
output = createWriter("mydata.txt"); //line 17
toggle=1;
horizon=320;
horizon2=350;
shippos=20.0;
//stroke(255);
// the ground
//fill(0,0,255);
fill(40);
rect(20,320,780,780);
// the ocean
fill(100);
rect(20,horizon,780,horizon2);
// the ocean lines
lines=10;
gap=(horizon2-horizon)/lines;
for (j=0;(j<lines);j++){
println(j);
stroke(0,255,0);
line(20,(horizon+.1*j*j*gap),780,(horizon+.1*j*j*gap));
println(gap + " --- " + j + "--- " + (horizon+j*gap));
}
// the moon
fill(240);
ellipse(650,100,40,40);
for (i = 1; i<6000; i++){
cx=random(20,780);
cy=random(20,780);
xdelta=random(1,4);
ydelta=random(1,4);
x1=cx-xdelta;
x2=cx+xdelta;
y1=cy-ydelta;
y2=cy+ydelta;
strokeWeight(1);
fill(255,0,0);
/*
line(cx,y1,cx,y2);
line(x1,cy,x2,cy);
line(x1,y1,x2,y2);
line(x1,y2,x2,y1);
*/
//point(cx,cy);
fill(random(200,255));
float diameter=random(10-15);
//ellipse(cx,cy,diameter,diameter);
stroke(128);
line(100,400,700,400);
line(400,100,400,700);
stroke(0);
text(i,750,750);
//debugging output
// println(i);
// println(i + " ---" + cx + " ---" + cy + " --- " + px + " --- " + py + " --- " + px2 + " --- " + py2);
// output.println(i + " ---" + angle + " ---" + angle2 + " --- " + px + " --- " + py + " --- " + px2 + " --- " + py2);
}
}
void draw(){
toggle=-toggle;
println("toggle - " + toggle);
shippos=shippos+.1;
// for (shippos=20;shippos<700;shippos++){
fill(240);
rect(-30+shippos,314,0+shippos,319);
fill(0,0,255);
if (toggle==-1) {fill(255,0,0);}
ellipse(-20+shippos,310,8,8);
fill(240);
ellipse(700-shippos*.1,200,40,10);
// }
//println("Stop");
//output.println("Stop");
output.flush();
output.close();
//exit();
}
1