PGraphics

An offscreen drawing buffer with the exact same drawing API as Sketch.

On this page · 4 entries

CreateGraphics

  • CreateGraphics(int w, int h) → PGraphics

Creates a new raster-backed offscreen surface.

BeginDraw · EndDraw

  • BeginDraw()
  • EndDraw()

Brackets a batch of drawing calls.

Get

  • Get() → PImage

Snapshots the current pixels as a standalone PImage.

Runnable example

A complete sketch using this section. Test in Dana opens it in the IDE, or paste it into a new tab and press Run.

C#
// PGraphics: an offscreen canvas. Paint trails into it, then show it like an image.
public class Offscreen : Sketch
{
    PGraphics layer;

    public override void Setup()
    {
        Size(600, 400);
        layer = CreateGraphics(Width, Height);
    }

    public override void Draw()
    {
        layer.BeginDraw();
        layer.Fill(0, 12);                 // slowly fade what's there
        layer.NoStroke();
        layer.Rect(0, 0, Width, Height);
        layer.FillHSB(FrameCount % 360, 80, 100);
        layer.Circle(MouseX, MouseY, 30);
        layer.EndDraw();

        Background(0);
        Image(layer, 0, 0);                         // full size
        Image(layer, Width - 160, 10, 150, 100);    // and a thumbnail
        NoFill(); Stroke(255);
        Rect(Width - 160, 10, 150, 100);
    }
}