Faster way to draw part of an image?

i need to be able to draw the top part of an image, here’s what i have

void drawImageSplice(PGraphics pg,PImage img,int x,int y,int w,int h,float cut){
  int yp = round(h*cut);
  if(yp>0){
    PGraphics buffer = createGraphics(w,yp);
    buffer.beginDraw();
    buffer.image(img,0,0,w,h);
    buffer.endDraw();
    pg.image(buffer,x,y,w,yp);
  }
}

this works great locally but in a live html canvas it’s very slow. is there a faster way to do this? thanks

The clip function may be of use, since it allows you to temporarily draw only in specific rectangle on screen

2 Likes

oh that’s perfect thanks

wait apparently this doesn’t work in html at all. like it just stops the program immediately

it works great in local processing but in live html with a processing canvas it goes blank immediately. even just calling noClip() breaks the canvas.

image

Then an alternative which avoids making new PGraphics is the copy() method, which allows you to copy a certain rectangle of an image using the underlying pixels array. This may be more memory efficient, since you are not creating a new PGraphics every time you draw the image, but it likely isn’t much faster since you are copying pixels rather than drawing an image using image(). I haven’t tested the speed of this function, but it may be worth a shot.

2 Likes

do you know if clip() can be used in html. i still can’t get it to run for some reason