A viewport is a rectangular clipping region that constrains all subsequent drawing operations to a defined area of the screen. Any pixels that would fall outside the viewport are silently discarded — the display hardware outside that region is never touched. This makes viewports useful for building windowed UIs, protecting status bars from being overwritten, and optimising partial-screen updates by ensuring drawing calls can never stray into unintended areas. TFT_eSPI signals viewport capability at compile time via theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Marcussacapuces91/doc-TFT_eSPI/llms.txt
Use this file to discover all available pages before exploring further.
TFT_ESPI_FEATURES constant: bit 0 set (#define TFT_ESPI_FEATURES 1) indicates that the viewport API is available.
Setting a Viewport
X coordinate of the viewport’s top-left corner in screen coordinates.
Y coordinate of the viewport’s top-left corner in screen coordinates.
Width of the viewport in pixels.
Height of the viewport in pixels.
Controls how drawing coordinates are interpreted after the viewport is active.
true(default): Drawing coordinates are relative to the viewport’s top-left corner — coordinate(0, 0)means the top-left of the viewport.false: Drawing coordinates are absolute screen coordinates — the viewport only clips; it does not shift the origin.
Example
Removing a Viewport
resetViewport(), coordinates are once again absolute screen coordinates.
Viewport Getters
Once a viewport is active, you can query its parameters:| Method | Return type | Description |
|---|---|---|
getViewportX() | int32_t | X origin of the viewport in screen coordinates |
getViewportY() | int32_t | Y origin of the viewport in screen coordinates |
getViewportWidth() | int32_t | Width of the viewport in pixels |
getViewportHeight() | int32_t | Height of the viewport in pixels |
getViewportDatum() | bool | true if datum is relative to viewport origin |
Checking for Intersection
true if the rectangle described by (x, y, w, h) intersects the current viewport. This is useful before performing expensive drawing operations — you can skip work entirely when nothing would be visible:
Drawing a Frame Around the Viewport
w pixels in color around the current viewport boundary. The frame is drawn outside the viewport, so the interior clipping region is unaffected. This is useful for visually highlighting the active region during development or for UI panel borders.
Direct CGRAM Window Control
For advanced low-level use, TFT_eSPI exposes direct control over the display’s CGRAM address window — the rectangular region within the display driver that the next batch of pixel data will be written into.setWindow vs. setAddrWindow
setWindow(xs, ys, xe, ye)— sets the CGRAM window using inclusive start and end coordinates.setAddrWindow(xs, ys, w, h)— convenience wrapper that takes a top-left origin plus width and height instead of end coordinates.
pushColor() or writeColor() calls will fill the window row by row. You must call startWrite() before and endWrite() after to bracket the SPI transaction:
Clipping Window Helpers
Two boolean helpers check whether a given window intersects the current viewport. They also clip the coordinates in place to the intersection:clipAddrWindow()— works with origin + size format (x, y, w, h).clipWindow()— works with start + end format (xs, ys, xe, ye).
false if the rectangle lies entirely outside the viewport (no pixels would be drawn), allowing you to bail out early. They return true and modify the pointed-to values to the clipped coordinates when partial overlap exists.
Viewport with Absolute Coordinates
SettingvpDatum = false keeps the coordinate origin at the physical display’s top-left corner. The viewport still clips all drawing to the defined rectangle, but you do not need to offset your coordinates:
TFT_ESPI_FEATURES (value 1) has bit 0 set to indicate that viewport capability is compiled in. You can check this at runtime to write code that gracefully degrades if a stripped-down build is in use: