How to speed up rect drawing and fading?
in
Programming Questions
•
3 years ago
I'm writing a program that will include the reading of pixels from a source image and redrawing them as rectangles. The redrawing will happen in response to input, and they will fade out after some amount of time. This little test program below, illustrates this with a 250x250 pixel jpg as the input. Press 1 and the image appears for 1 second, then fades out over 1 second. Press 2 and it appears for 2, fades for 1, etc.
I've found that when the source is 250x250 pixels, that it runs pretty quickly. But when I increase the source to 500x500, the thing gets *really* slow.
Is there a way to speed this up? I tried drawing on a PGraphics and then showing that but couldn't get it to work. Any pointers are appreciated.
- PImage input;
- boolean newInput;
- int frate, delay, delayCount;
- float fadefac;
- void setup() {
- size(300,300,P2D);
- frate = 30;
- delay = 0;
- fadefac = 255.0/frate;
- input=loadImage("250x250.jpg");
- newInput = false;
- frameRate(frate);
- }
- void draw() {
- background(0);
- appearThenFade();
- }
- void appearThenFade() {
- int alpha = 0;
- color curColor;
- input.loadPixels();
- if (newInput) {
- if (delayCount > frate) { // if it's not a fade
- for(int y=0;y<input.height;y++) {
- for(int x=0;x<input.width;x++) {
- drawRect(x,y,255);
- }
- }
- }
- else { // it's a fade
- for(int y=0;y<input.height;y++) {
- for(int x=0;x<input.width;x++) {
- alpha = int(delayCount*fadefac);
- drawRect(x,y,alpha);
- }
- }
- }
- delayCount--;
- if(delayCount == 0) {
- newInput = false;
- }
- }
- }
- void drawRect(int x,int y,int a) {
- int loc = x + y * input.width;
- color curColor=input.pixels[loc];
- stroke(curColor,a);
- fill(curColor,a);
- rect(x,y,1,1);
- }
- void keyPressed() {
- newInput = true;
- if (key == '0') {
- delayCount = 0;
- }
- else if (key == '1') {
- delayCount = 1*frate;
- }
- else if (key == '2') {
- delayCount = 2*frate;
- }
- else if (key == '3') {
- delayCount = 3*frate;
- }
- }
1