Skip to content

Viewport

Viewport

Viewport(
    canvas: Canvas,
    x: int,
    y: int,
    width: int,
    height: int,
    color: Color | list | tuple,
)

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: 2024-08-21T12:52:33.498949 (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: 2024-08-21T12:52:33.499016 (datetime)
   PARAMETERS
     - OBJECT_ID: 2 (int)
     - CANVAS_ID: 1 (Canvas)
     - X: 0 (int)
     - Y: 0 (int)
     - WIDTH: 512 (int)
     - HEIGHT: 512 (int)
     - COLOR: [0, 0, 0, 1] (converted) (Color | list | tuple)

{
  "jsonrpc": "2.0",
  "commands": [
    {
      "method": "core.Canvas",
      "id": 1,
      "timestamp": 1724237553.498949,
      "parameters": {
        "id": 1,
        "width": 512,
        "height": 512,
        "dpi": 100.0
      }
    },
    {
      "method": "core.Viewport",
      "id": 2,
      "timestamp": 1724237553.499016,
      "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 int

X coordinate of the viewport bottom left corner

required
y int

Y coordinate of the viewport bottom left corner

required
width int

Width of the viewport in pixels.

required
height int

Height of the viewport in pixels.

required
color Color | list | tuple

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.

Example
1
2
3
4
from gsp.core import Canvas, Viewport
canvas = Canvas(512, 512, 100.0)
viewport = Viewport(canvas, 0, 0, 512, 512, (0,0,0,1))
viewport.render("png")
1. COMMAND
     - METHOD: "core.Canvas" (str)
     - COMMAND_ID: 1 (int)
     - TIMESTAMP: 2024-08-21T12:52:33.569851 (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: 2024-08-21T12:52:33.569916 (datetime)
   PARAMETERS
     - OBJECT_ID: 2 (int)
     - CANVAS_ID: 1 (Canvas)
     - X: 0 (int)
     - Y: 0 (int)
     - WIDTH: 512 (int)
     - HEIGHT: 512 (int)
     - COLOR: [0, 0, 0, 1] (converted) (Color | list | tuple)

3. COMMAND
     - METHOD: "Viewport/render" (str)
     - COMMAND_ID: 3 (int)
     - TIMESTAMP: 2024-08-21T12:52:33.569939 (datetime)
   PARAMETERS
     - OBJECT_ID: 2 (int)
     - TARGET: png (str)

{
  "jsonrpc": "2.0",
  "commands": [
    {
      "method": "core.Canvas",
      "id": 1,
      "timestamp": 1724237553.569851,
      "parameters": {
        "id": 1,
        "width": 512,
        "height": 512,
        "dpi": 100.0
      }
    },
    {
      "method": "core.Viewport",
      "id": 2,
      "timestamp": 1724237553.569916,
      "parameters": {
        "id": 2,
        "canvas(id)": 1,
        "x": 0,
        "y": 0,
        "width": 512,
        "height": 512,
        "color": [
          0,
          0,
          0,
          1
        ]
      }
    },
    {
      "method": "Viewport/render",
      "id": 3,
      "timestamp": 1724237553.569939,
      "parameters": {
        "id": 2,
        "target": "png"
      }
    }
  ]
}

Parameters:

Name Type Description Default
target str

Filename of the target

required