[SOLVED] Fullscreen sketch scaling and preformance

I want to have my sketch resolution be the same across different screen sizes. I have tried using PGraphics with image() but the performance suffers and my sketches frame rate drops. Am I using PGraphics and image() incorrectly / is there a better way to do this? I’ll post my code below.

PGraphics pg;

Fly fly;

void setup() {
  pg=createGraphics(900, 900);
  fly = new Fly(pg);

void draw() {

class Fly {

  PGraphics pg;

  float speed;
  ArrayList<PVector> loc;
  ArrayList<Float> theta;
  Float s=0f;

  public Fly(PGraphics pg) {
    this.pg = pg;
    theta = new ArrayList<Float>();
    speed =5;
    loc = new ArrayList<PVector>();
    for (int i=0; i<20; i++) {
      loc.add(new PVector(pg.width/2, pg.height/2));
      theta.add(new Float(0));

  public void Draw() {
    pg.fill(255, 10);
    pg.rect(0, 0, pg.width, pg.height);
    for (int i=0; i<loc.size(); i++) {
      pg.ellipse(loc.get(i).x, loc.get(i).y, 5, 5);
      loc.set(i, loc.get(i).add(new PVector(cos(theta.get(i))*speed, sin(theta.get(i))*speed)));
      theta.set(i, theta.get(i)+random(-1, 1));
      if (loc.get(i).x > pg.width)loc.get(i).x=0;
      if (loc.get(i).y >pg.height)loc.get(i).y=0;
      if (loc.get(i).x<0)loc.get(i).x=pg.width;
      if (loc.get(i).y<0)loc.get(i).y=pg.height;

    //image(pg, width/2 - pg.width/2, height/2 - pg.height/2);//Preformance does not suffer here
    image(pg, width/3-(height/5)/2, (height/5)/2, height-height/5, height-height/5);//Preformance suffers here with down scaling
1 Like

Check out:


Hi Sara,

Thanks for the response. I know that I can do relative sizing and have a windowed sketch with size(), but I want the sketch to be fullscreen with a letter-boxed area where the drawing happens with a fixed resolution.

I plan on adding more sketches to this sketchbook to make a top down shooter like game. If I made the sizing relative people with larger monitors would have more room to move around and would have an easier time dodging bullet patterns than someone on a smaller screen. I also don’t want to force players to play the game in windowed mode with no scaled fullscreen option.

Do you want the sketch to expand itself to fullscreen, but keep its original aspect ratio, or for it to simply keep its size with any screen?

If you want the prior option, you can just put:

if (width > height) pg=createGraphics(height, height);
if (width < height) pg=createGraphics(width, width);

Then use what I did on

1 Like

I want it to keep its size, and have it be positioned in the center of the screen. In my sketch right now I have it so that the square where everything is being drawn will have borders on all sides.

I want it to behave exactly like the photo. What I have above works to do this, it is just very slow and laggy.

After looking around some more I figured out that all I had to do was change the renderer to P2D to get better preformance, I feel silly :disappointed_relieved:

1 Like

If the canvas is always going to be 900 x 900, and you just want to move it to the center of the screen, you could just draw everything in one corner, then use translate(); to move it to the center.

P2D only makes the sketch faster by putting the quality of the sketch down, but a sketch of that scale shouldn’t drop the fps down to 10-30.


Translating would work well for centering, but if someone has a screen height or width less than 900px the canvas will be out of bounds. I would want the canvas to appear smaller to fit on the screen but behave like it were 900x900. If a person’s screen is really massive the canvas should appear larger, but behave like a 900x900 size. I don’t mind if the image appears a bit stretched.

So you want to scale the sketch to the screen size, while keeping the 1:1 (900:900) ratio?

Yes, that is exactly what I want to do.

Just follow what I said above then :slight_smile:


Ohhh. I get it now. I feel even more silly than before :horse:. Thanks for being so patient with me.