Skip to content

Viewport

Viewport

Viewport(
    canvas: Canvas,
    x: Transform | float | int,
    y: Transform | float | int,
    width: Transform | float | int,
    height: Transform | float | int,
    color: Color,
)

A viewport is a rectangular two-dimensional surface from a canvas, located at (x, y) coordinates (bottom left corner) with size equal to width×height pixels and a background color.

Notes

Future implementation will allows viewports to have an arbitrary rotation.

Example
1
2
3
from gsp.core import Canvas, Viewport
canvas = Canvas(512, 512, 100.0)
viewport = Viewport(canvas, 0, 0, 512, 512, (0,0,0,1))
1. COMMAND
     - METHOD: "core.Canvas" (str)
     - COMMAND_ID: 1 (int)
     - TIMESTAMP: 2025-03-18T15:02:33.441212 (datetime)
   PARAMETERS
     - OBJECT_ID: 1 (int)
     - WIDTH: 512 (int)
     - HEIGHT: 512 (int)
     - DPI: 100.0 (float)

2. COMMAND
     - METHOD: "core.Viewport" (str)
     - COMMAND_ID: 2 (int)
     - TIMESTAMP: 2025-03-18T15:02:33.441365 (datetime)
   PARAMETERS
     - OBJECT_ID: 2 (int)
     - CANVAS_ID: 1 (Canvas)
     - X: 0 (Transform | float | int)
     - Y: 0 (Transform | float | int)
     - WIDTH: 512 (Transform | float | int)
     - HEIGHT: 512 (Transform | float | int)
     - COLOR: Color(r=0, g=0, b=0, a=1) (converted) (Color)

{
  "jsonrpc": "2.0",
  "commands": [
    {
      "method": "core.Canvas",
      "id": 1,
      "timestamp": 1742306553.441212,
      "parameters": {
        "id": 1,
        "width": 512,
        "height": 512,
        "dpi": 100.0
      }
    },
    {
      "method": "core.Viewport",
      "id": 2,
      "timestamp": 1742306553.441365,
      "parameters": {
        "id": 2,
        "canvas(id)": 1,
        "x": 0,
        "y": 0,
        "width": 512,
        "height": 512,
        "color": [
          0,
          0,
          0,
          1
        ]
      }
    }
  ]
}

A viewport is a rectangular two-dimensional surface.

Parameters:

Name Type Description Default
canvas Canvas

Canvas where to create the viewport

required
x Transform | float | int

X coordinate of the viewport bottom left corner

required
y Transform | float | int

Y coordinate of the viewport bottom left corner

required
width Transform | float | int

Width of the viewport in pixels.

required
height Transform | float | int

Height of the viewport in pixels.

required
color Color

Background color of the viewport

required

render

render(target: str)

Render the viewport to the specified target. If no target is specified, return a raw image as bytes.

python docs/snippets/Viewport_render.py

Parameters:

Name Type Description Default
target str

Filename of the target

required