Processing > AdaptiveOutput

1.  Description

AdaptiveOutput is a class for Processing. It allows you to set a sketch's resolution independently from the window size. It is similar to when watching a movie on your computer: the whole image is always visible, scaled appropriately, and black bars are added automatically to keep the image at its proper ratio.

In the following example, the "output" resolution is set to 1024x1024 pixels. A green 1024x1024 pixels rectangle is drawn and then an image with a resolution of 1375x448 pixels is drawn. The 1024x1024 pixels "output" resolution is automatically scaled and its ratio is always maintained even when changing the window size from 320x240, to 600x240, and 320x600.

The image is a portrait of Washakie, Chief of Shoshones. Its source: https://picryl.com/media/washakie-chief-of-shoshones-head-and-shoulders-portrait-facing-front

2.  The code

You can download the code and example projet here: Processing_AdaptiveOutput.zip . AdaptiveOutput is a class defined in the tab named AdaptiveOutput.

AdaptiveOutput output;

void setup() {

        // SET ANY SCREEN SIZE, EVEN FULLSCREEN
        // THIS IS JUST YOUR WINDOW SIZE.
        // YOU WILL SET YOUR WORKING SKETCH SIZE LATER.
        size(320,240,P3D);
        //fullScreen(P3D);

        // THIS IS THE OUTPUT RESOLUTION TO WHICH
        // YOU SHOULD DRAW. IT WILL ALWAYS BE SCALED
        // TO FIT YOUR WINDOW *BUT* WILL KEEP THE
        // PROPER RATIO. IF YOUR ARE USING VIDEO, IT
        // COULD FOR EXAMPLE BE THE RESOLUTION OF THE
        // VIDEO.
        output = new AdaptiveOutput(this,1024,1024);

}

void draw() {

        // SET THE COLOR OF YOUR "BLACK BARS"
        background(0);

        // WHEN YOU WANT TO DRAW INTO THE SCALED OUTPUT AREA, ALWAYS ENTER THE MATRIX FIRST
        output.enterMatrix();


        // DO YOUR DRAWING HERE


        // EXIT THE OUTPUT MATRIX WHEN YOU ARE DONE DRAWING INTO THE THE SCALED OUTPUT AREA
        output.exitMatrix();

}

3.  Special considerations

For AdaptiveOutput output = new AdaptiveOutput( desiredWidth , desiredHeight );