Background image is too much for CPU.

edited May 2014 in How To...


I'm a beginner at processing. I have created an animation where circles bounce around the screen. I tried to put an image in the background using this in the draw function.

PImage img; img = loadImage("stars.jpg"); background(img);

It works but it makes the animation extremely slow and choppy. I understand that this is happening because for every frame the image file has to be accessed and drawn, which takes a lot of resources. I tried putting the image in the setup but then my moving balls leave black trails.

Is there a way to put an image on the background of an animation without visibly slowing down the animation?

Thank you.



  • Function loadImage() is extremely SLOW! Any resources a program use should be loaded & instantiated inside setup()! :-&

  • edited May 2014 Answer ✓
    // Pace on its own this outside setup or draw
    PImage img; 
    // Place this in setup()
    img = loadImage("stars.jpg"); 
    // This should be the first line in draw()

    This makes the variable img accessible in both setup and draw

  • edited May 2014

    @quark Thank you! It works!

Sign In or Register to comment.