We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi web,
I have created a simple animation with the processing but when I run it as a processing.js embedded html canvas it drops below 10fps.
I understand its relative to a problem with my call looping and memory concurrency, but I am not an expert on how to cap my code so its more efficient when loaded and run through a web browser.
If you can look at it and let me know where I could improve, I'll be grateful for your web advice.
float r; float theta; float theta_vel; float theta_acc; float max_distance;
void setup() { frameRate(30);
r = 250;
theta = 0;
theta_vel = .04;
theta_acc = 0;
size(400,400,P3D);
max_distance = dist(0, 0, width, height); }
void draw() { background(0,0); //lights(); rotateX(.66);
translate(180,240); for(int i = 0; i <= width/7; i += 3) { for(int j = 0; j <= width/7; j += 3) { float size = random(dist(random(100,200), random(100,200), i, j)); size = size/max_distance * 410; pushMatrix(); translate(i, j); float c = map(size, 0, 255, 0, 255); fill(255, c, c);
box(17,7,size);
popMatrix();
}
}
theta_vel += theta_acc;
theta += theta_vel;
}
Answers
It might help to not create new variables inside the loop. so before the for loop make:
float size, c;
(But i'm not sure how smart compilers are those days).For the rest,
width/7
get's calculated really often, it might be good to store inside a variable.For the rest there is not much to it i guess.
Maybe try to come up with a algorithm to draw only the top and front of the cube. Now a lot of cube's are inside or on the back so they can never been seen but take a lot of cpu time.
Since your animation is repetitive, you can pre-render a number of frames and then just (re-)display those.
Adapted Code
Something like this, but then better: