Every pixel you draw in TFT_eSPI is addressed through a straightforward two-dimensional coordinate system anchored to the top-left corner of the display. Understanding how that system works — and how it changes when you rotate the screen — is essential for writing layout code that adapts cleanly across different display orientations and sizes.Documentation 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.
The Pixel Coordinate System
TFT_eSPI uses a pixel-based Cartesian coordinate system defined by two axes:- X axis — horizontal, increasing from left to right.
- Y axis — vertical, increasing from top to bottom.
- Origin
(0, 0)— the top-left corner of the display after the current rotation is applied.
| Corner | Coordinate |
|---|---|
| Top-left | (0, 0) |
| Top-right | (239, 0) |
| Bottom-left | (0, 319) |
| Bottom-right | (239, 319) |
How Rotation Affects Coordinates
CallingsetRotation(m) with a value of 0–3 physically remaps the coordinate origin and axes so that (0, 0) always refers to the top-left corner of the display as it is held in the chosen orientation. The physical panel dimensions are fixed, but the logical width() and height() swap when you move between portrait and landscape modes.
setRotation(m) | Orientation | width() | height() | Origin location |
|---|---|---|---|---|
0 | Portrait (default) | native W | native H | Physical top-left |
1 | Landscape (90° CW) | native H | native W | Physical bottom-left |
2 | Portrait flipped (180°) | native W | native H | Physical bottom-right |
3 | Landscape flipped (270°) | native H | native W | Physical top-right |
The terms “native W” and “native H” refer to the hardware dimensions specified in
User_Setup.h (e.g., TFT_WIDTH and TFT_HEIGHT). For a 240 × 320 panel, width() is 240 in rotation 0 and 320 in rotation 1.Querying Width and Height at Runtime
Always query the display dimensions throughwidth() and height() rather than hard-coding numbers. Both methods return the current rotated dimensions and update automatically whenever you call setRotation().
Centering and Aligning Content
A common pattern is to position text or shapes relative to the screen edges. Usewidth() and height() as the reference dimensions:
Viewport Coordinates
TFT_eSPI supports a viewport — a clipping rectangle that constrains all drawing to a sub-region of the screen. When a viewport is active, coordinates can be interpreted in two ways depending on how the viewport was created:- Viewport with datum enabled (
vpDatum = true, the default): The coordinate origin(0, 0)shifts to the top-left of the viewport. Drawing calls use coordinates relative to the viewport. - Viewport with datum disabled (
vpDatum = false): Coordinates remain relative to the full screen; pixels outside the viewport are silently clipped.
width() and height() always reflect the full screen dimensions even when a viewport is active. Use getViewportWidth() and getViewportHeight() to query the active viewport size if you need to lay out content within it.Axis Resolution
The axis resolution in TFT_eSPI is fixed at 1 pixel per coordinate unit on both axes. There is no fractional addressing in the standard drawing API. The anti-aliased drawing functions (drawWideLine(), drawSpot(), drawSmoothArc()) accept float arguments to position elements with sub-pixel precision, but the underlying rasterisation still maps to whole pixels.