Using PGraphics for Off‑Screen Rendering in Processing
Learn how to use Processing’s PGraphics for off‑screen rendering to reuse drawings, apply effects, and avoid costly redraws each frame.
31 Jan 2026, 02:57 UTC

Useful answer
PGraphics creates an off‑screen buffer that you can draw to once and reuse many times. This lets you layer effects, apply post‑processing, or avoid redrawing static content each frame without re‑executing the main draw() loop.
How it works
First allocate a PGraphics object with createGraphics(width, height, renderer). The renderer must be P2D or P3D; the default JAVA2D ignores off‑screen drawing. Inside a beginDraw() / endDraw() pair you issue any Processing drawing commands just as you would on the main canvas. After endDraw() the buffer holds the rasterised result, which you can display with image(pg, x, y) or use as a texture.
Worked example
PGraphics pg;void setup() {
size(400, 400, P2D); // main canvas uses P2D renderer
pg = createGraphics(width, height, P2D); // off‑screen buffer
pg.beginDraw();
pg.background(200); // light gray
pg.fill(0); // black
pg.ellipse(200, 200, 100, 100); // circle in the centre
pg.endDraw();
}void draw() {
background(30); // dark background each frame
image(pg, 0, 0); // draw the buffered circle
}The circle appears on a dark background. Changing the circle’s position inside the beginDraw()/endDraw() block updates the displayed image without redrawing the gray background each frame.
Limits and common mistakes
- Memory use – the buffer occupies roughly
width × height × 4bytes (RGBA). Large buffers can exceed GPU memory and cause slowdowns orOutOfMemoryErrorat startup. - Renderer dependence – off‑screen drawing only works with
P2DorP3D. Using the defaultJAVA2Drenderer (e.g.,size(400,400)without a renderer) will produce no visible result from the PGraphics buffer. - Missing begin/end – issuing drawing commands outside a
beginDraw()/endDraw()pair affects the main canvas, not the buffer, and the off‑screen image stays blank. - Re‑creating each frame – calling
createGraphics insidedraw()defeats the performance benefit and repeatedly allocates memory.
Practical verification
- Run the example code in Processing 4.x. You should see a gray circle on a dark background.
- Modify the sketch to use
JAVA2D(removeP2Dfrom bothsize()andcreateGraphics()). The circle will disappear, confirming the renderer requirement. - Check memory usage with Processing’s built‑in profiler or an external tool; observe that memory grows linearly with the buffer’s pixel count.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.