![]() |
TGX 1.1.1
A tiny 2D/3D graphics library optimized for 32 bits microcontrollers.
|
This page describes, with examples, most 2D drawing primitives implemented in TGX.
/examples/ subdirectory of the library. In particular, the example TestPrimitives contains all the code listed below.The 2D API is centered around the tgx::Image class. Drawing methods modify the pixels of an image, or of a sub-image when clipping to a rectangular region is needed.
The API is roughly organized into the following groups:
| Group | Main purpose | Typical methods |
|---|---|---|
| Pixel access | Read or write individual pixels. | drawPixel(), drawPixelf(), readPixel(), operator() |
| Image copy and blitting | Copy, blend, scale, rotate or mask another image. | blit(), blitMasked(), blitRotated(), blitScaledRotated(), copyFrom() |
| Fast primitives | Draw simple integer-coordinate shapes as quickly as possible. | drawLine(), drawFastHLine(), fillRect(), fillTriangle(), fillCircle() |
| High-quality primitives | Draw anti-aliased shapes with floating-point coordinates and sub-pixel precision. | drawLineAA(), drawThickLineAA(), fillPolygonAA(), fillCircleAA() |
| Text | Draw built-in bitmap fonts, measure text, or place text using anchors. | drawText(), drawTextEx(), measureText(), measureChar() |
| External image/font bindings | Use optional decoder or font-rendering libraries when they are available. | PNGDecode(), JPEGDecode(), GIFplayFrame(), setOpenFontRender() |
Most 2D drawing methods follow a small set of naming rules. Once these rules are known, method names become fairly predictable:
| Name part | Meaning | Examples |
|---|---|---|
draw... | Draw only the outline of a shape. | drawLine(), drawRect(), drawCircle() |
fill... | Draw the interior of a shape. Some filled methods can also take an outline color. | fillRect(), fillTriangle(), fillCircle() |
Thick | Use a thicker outline. | drawThickRect(), drawThickLineAA(), fillThickCircleAA() |
AA | High-quality anti-aliased version. These methods usually use floating-point coordinates and sub-pixel precision. | drawLineAA(), fillPolygonAA(), drawThickEllipseAA() |
Fast | Specialized fast version for common cases. | drawFastHLine(), drawFastVLine() |
Masked | Use one source color as transparent. | blitMasked(), drawTexturedMaskedTriangle() |
Gradient | Interpolate colors across the shape. | fillScreenVGradient(), drawGradientTriangle() |
Textured | Map an image onto a triangle or quad. | drawTexturedTriangle(), drawTexturedQuad() |
As a rule of thumb, use the non-AA integer methods for static, pixel-aligned graphics and use the AA methods when shapes move smoothly, use non-integer coordinates, have diagonal edges, or need visually smooth outlines.
TGX is often used on small MCUs, so the 2D API tries to keep the common paths simple and fast. A few rules help when choosing between methods:
| Situation | Recommended approach |
|---|---|
| Pixel-aligned static graphics | Prefer integer, non-AA methods such as drawLine(), fillRect(), fillTriangle() or fillCircle(). |
| Smooth motion, diagonal edges or sub-pixel placement | Use the AA methods when the visual quality matters. They are usually more expensive. |
| Repeated horizontal or vertical lines | Prefer drawFastHLine() and drawFastVLine() over the general line method. |
| Drawing inside a rectangular panel, widget or viewport | Create a sub-image and draw into it. Sub-images are cheap and automatically clip drawing to their own bounds. |
| Copying rectangular images | Prefer blit() or copyFrom() over manual pixel loops. |
| Large sprites, textures or fonts on MCUs | Keep frequently used pixel data in fast memory when the platform provides several memory regions. |
Most public drawing methods clip their output to the current image or sub-image. Direct pixel access with operator() is intentionally different: it gives raw access to the buffer and assumes that the coordinates are valid.

| Method | Description |
|---|---|
| clear() | Set all pixels of the image to the same color. |
| fillScreen() | Same as clear(). |
| fillScreenVGradient() | Fill the image with a vertical gradient between two colors. |
| fillScreenHGradient() | Fill the image with a horizontal gradient between two colors. |
Code used to generate the image:

| Method | Description |
|---|---|
| drawPixel() | Set the color of a given pixel. |
| drawPixelf() | Set the color of a given pixel, using a floating-point position vector. |
| readPixel() | Read the color of a pixel. |
| readPixelf() | Read the color of a pixel, using a floating-point position vector. |
| operator() | Direct access to the pixel color memory location (read/write). |
Code used to generate the image:

| Method | Description |
|---|---|
| fill(start_pos, new_color) | Fill a connected component with a given color. |
| fill(start_pos, border_color, new_color) | Fill a region delimited by a given color. |
Code used to generate the image:

Blitting means copying pixels from a source image (the sprite) into a destination image. This is the usual way to draw small images, icons, sprites, off-screen buffers or pre-rendered UI elements.
| Method | Description |
|---|---|
| blit(sprite, pos, opacity) | Copy the sprite at a given position, with optional opacity. |
| blitMasked(sprite, mask_color, pos, opacity) | Copy the sprite but skip pixels that match a transparent color. |
| blitRotated(sprite, pos, angle, opacity) | Rotate the sprite by a quarter turn (0, 90, 180 or 270 degrees) before drawing it. |
| blitScaledRotated(sprite, pos_src, pos_dst, scale, angle, opacity) | Scale and rotate the sprite by an arbitrary angle around a source anchor point. |
| blitScaledRotatedMasked(sprite, mask_color, pos_src, pos_dst, scale, angle, opacity) | Same as blitScaledRotated(), but with one transparent source color. |
| copyFrom() | Copy and resize another image into the whole destination image, possibly converting color types. |
All blit methods clip automatically to the destination image or sub-image. Advanced overloads also accept a custom blending operator instead of the standard opacity parameter.
Code used to generate the image:
See also blitBackward() and methods for image copying, resizing and color type conversion: copyFrom(), copyReduceHalf(), reduceHalf() and convert().

Use these methods when drawing parallel lines as they are faster than the general line drawing methods.
| Method | Description |
|---|---|
| drawFastVLine() | Draw a vertical line segment. |
| drawFastHLine() | Draw a horizontal line segment. |
Code used to generate the image:

| Method | Description |
|---|---|
| drawLine() | Draw a basic straight line (single pixel thick, no anti-aliasing). Fastest method. |
| drawSegment() | Draw a basic line segment and choose independently whether each endpoint is drawn. |
| drawLineAA() | Draw a line segment with anti-aliasing (single pixel thick). |
| drawThickLineAA() | Draw a thick line with anti-aliasing and specify how the ends are drawn (rounded, flat, arrows...). |
| drawWedgeLineAA() | Draw a line segment with anti-aliasing and varying thickness, and specify how the ends are drawn. |
Code used to generate the image:

| Method | Description |
|---|---|
| drawRect() | Draw a basic rectangle. Fastest method. |
| drawThickRect() | Draw a rectangle with a thick outline. |
| fillRect() | Draw a filled rectangle. |
| fillThickRect() | Draw a filled rectangle with a thick outline of a different color. |
| fillRectHGradient() | Draw a filled rectangle with a horizontal gradient between two colors. |
| fillRectVGradient() | Draw a filled rectangle with a vertical gradient between two colors. |
See also drawThickRectAA(), fillRectAA(), fillThickRectAA(). These methods may be useful for smooth animations but usually not for static display (as rectangle edges are parallel to pixel boundaries and therefore do not usually require anti-aliasing).
Code used to generate the image:

| Method | Description |
|---|---|
| drawRoundRect() | Draw a single pixel thick rectangle with rounded corners. Fastest non-AA method. |
| fillRoundRect() | Draw a filled rectangle with rounded corners. Fastest non-AA method. |
| drawRoundRectAA() | Draw a single pixel thick rectangle with rounded corners, with anti-aliasing. |
| drawThickRoundRectAA() | Draw a rounded-corner rectangle with a thick outline, with anti-aliasing. |
| fillRoundRectAA() | Draw a filled rectangle with rounded corners, with anti-aliasing. |
| fillThickRoundRectAA() | Draw a filled rectangle with rounded corners and a thick outline of a different color, with anti-aliasing. |
Code used to generate the image:

| Method | Description |
|---|---|
| drawTriangle() | Draw a simple triangle. Fastest method. |
| fillTriangle() | Draw a simple filled triangle. Fastest method. |
| drawTriangleAA() | Draw a triangle with a single pixel thick outline, with anti-aliasing. |
| drawThickTriangleAA() | Draw a triangle with a thick outline, with anti-aliasing. |
| fillTriangleAA() | Draw a filled triangle with anti-aliasing. |
| fillThickTriangleAA() | Draw a filled triangle with a thick outline of a different color, with anti-aliasing. |
Code used to generate the image:

These methods are still part of the 2D API, but internally they use the TGX triangle rasterizer. They are useful when an image or a color field must be warped onto an arbitrary triangle instead of copied as a rectangle. They are more expensive than a simple blit(), but they interpolate texture coordinates, vertex colors, or both across the triangle.
| Method | Description |
|---|---|
| drawTexturedTriangle() | Draw a filled triangle with texture mapping. |
| drawTexturedTriangle(..., blend_op) | Draw a textured triangle using a custom blending operator, function, functor or lambda. |
| drawGradientTriangle() | Draw a filled triangle with gradient color (Gouraud shading). |
| drawTexturedGradientTriangle() | Draw a filled triangle with both texture mapping and gradient color. |
| drawTexturedMaskedTriangle() | Draw a textured triangle while skipping one transparent source color. |
| drawTexturedGradientMaskedTriangle() | Draw a textured gradient triangle while skipping one transparent source color. |
Code used to generate the image:

| Method | Description |
|---|---|
| drawQuad() | Draw a simple quad. Fastest method. |
| fillQuad() | Draw a simple filled quad. Fastest method. |
| drawQuadAA() | Draw a quad with a single pixel thick outline, with anti-aliasing. |
| drawThickQuadAA() | Draw a quad with a thick outline, with anti-aliasing. |
| fillQuadAA() | Draw a filled quad with anti-aliasing. |
| fillThickQuadAA() | Draw a filled quad with a thick outline of a different color, with anti-aliasing. |
Code used to generate the image:

These methods are still part of the 2D API, but internally they use the TGX triangle rasterizer. They are useful when an image or a color field must be warped onto an arbitrary quad instead of copied as a rectangle. They are more expensive than a simple blit(), but they interpolate texture coordinates, vertex colors, or both across the quad.
| Method | Description |
|---|---|
| drawTexturedQuad() | Draw a filled quad with texture mapping. |
| drawTexturedQuad(..., blend_op) | Draw a textured quad using a custom blending operator, function, functor or lambda. |
| drawGradientQuad() | Draw a filled quad with gradient color (Gouraud shading). |
| drawTexturedGradientQuad() | Draw a filled quad with both texture mapping and gradient color. |
| drawTexturedMaskedQuad() | Draw a textured quad while skipping one transparent source color. |
| drawTexturedGradientMaskedQuad() | Draw a textured gradient quad while skipping one transparent source color. |
Code used to generate the image:

| Method | Description |
|---|---|
| drawPolyline() | Draw a simple polyline. Fastest method. |
| drawPolylineAA() | Draw a polyline with anti-aliasing. |
| drawThickPolylineAA() | Draw a thick polyline with anti-aliasing and specify how the endings look. |
Code used to generate the image:

| Method | Description |
|---|---|
| drawPolygon() | Draw a simple polygon. Fastest method. |
| fillPolygon() | Draw a simple filled polygon. Fastest method. |
| drawPolygonAA() | Draw a polygon with anti-aliasing. |
| drawThickPolygonAA() | Draw a polygon with a thick outline, with anti-aliasing. |
| fillPolygonAA() | Draw a filled polygon with anti-aliasing. |
| fillThickPolygonAA() | Draw a filled polygon with a thick outline, with anti-aliasing. |
Code used to generate the image:

| Method | Description |
|---|---|
| drawCircle() | Draw a simple circle. Fastest method. |
| fillCircle() | Draw a simple filled circle. Fastest method. |
| drawCircleAA() | Draw a circle with anti-aliasing. |
| drawThickCircleAA() | Draw a circle with a thick outline, with anti-aliasing. |
| fillCircleAA() | Draw a filled circle with anti-aliasing. |
| fillThickCircleAA() | Draw a filled circle with a thick outline, with anti-aliasing. |
Code used to generate the image:

| Method | Description |
|---|---|
| drawEllipse() | Draw a simple ellipse. Fastest method. |
| fillEllipse() | Draw a simple filled ellipse. Fastest method. |
| drawEllipseAA() | Draw an ellipse with anti-aliasing. |
| drawThickEllipseAA() | Draw an ellipse with a thick outline, with anti-aliasing. |
| fillEllipseAA() | Draw a filled ellipse with anti-aliasing. |
| fillThickEllipseAA() | Draw a filled ellipse with a thick outline, with anti-aliasing. |
Code used to generate the image:

| Method | Description |
|---|---|
| fillCircleSectorAA() | Draw a circle sector (pie), with anti-aliasing. |
| drawCircleArcAA() | Draw a single-pixel circle arc, with anti-aliasing. |
| drawThickCircleArcAA() | Draw a thick circle arc, with anti-aliasing. |
| fillThickCircleSectorAA() | Draw a circle sector (pie) with a thick outline arc, with anti-aliasing. |
Code used to generate the image:

| Method | Description |
|---|---|
| drawQuadBezier() | Draw a quadratic Bezier curve. |
| drawCubicBezier() | Draw a cubic Bezier curve. |
| drawThickQuadBezierAA() | Draw a thick quadratic Bezier curve, with anti-aliasing. |
| drawThickCubicBezierAA() | Draw a thick cubic Bezier curve, with anti-aliasing. |
Code used to generate the image:

| Method | Description |
|---|---|
| drawQuadSpline() | Draw a simple quadratic spline. |
| drawCubicSpline() | Draw a simple cubic spline. |
| drawThickQuadSplineAA() | Draw a thick quadratic spline, with anti-aliasing. |
| drawThickCubicSplineAA() | Draw a thick cubic spline, with anti-aliasing. |
| drawThickClosedSplineAA() | Draw a thick closed spline, with anti-aliasing. |
| drawClosedSpline() | Draw a simple closed spline. |
| fillClosedSplineAA() | Draw a closed spline and fill its interior, with anti-aliasing. |
| fillThickClosedSplineAA() | Draw a thick closed spline and fill its interior, with anti-aliasing. |
Code used to generate the image:

| Method | Description |
|---|---|
| drawText() | Draw text using a given font. Simple "legacy" method. |
| drawTextEx() | Draw text using a given font. Extended method with anchor placement (see tgx::Anchor). |
| drawChar() | Draw a single character and return the next cursor position. |
| measureText() | Compute the bounding box of a text string without drawing it. |
| measureChar() | Compute the bounding box of a character without drawing it. |
TGX can draw bitmap fonts directly from the font headers included with the project. The simple drawText() method places text at the default text anchor, which is baseline-left. Use drawTextEx() when the text must be centered, aligned to a box, placed relative to another anchor point, or measured consistently before drawing.
| Concept | Meaning |
|---|---|
| Baseline | The default text position is on the text baseline, not on the top-left corner of the text box. |
| Anchor | drawTextEx() interprets the position according to the selected tgx::Anchor. |
| Measurement | measureText() and measureChar() return bounding boxes without drawing anything. |
| Built-in fonts | TGX ships bitmap font headers that must be explicitly included when used. |
| Converted fonts | The recommended way to create new TGX font headers is the bundled `tools/tgx_font.py` converter. |
| TrueType fonts | For TrueType rendering, use the optional OpenFontRender binding described in Drawing text with TrueType fonts. |
additional includes for the text fonts
Code used to generate the image:
The TGX library supports fonts using:
The following fonts are already packaged with TGX but must be included explicitly in the project when used:
#include "font_tgx_Arial.h"#include "font_tgx_Arial_Bold.h"#include "font_tgx_OpenSans.h"#include "font_tgx_OpenSans_Bold.h"#include "font_tgx_OpenSans_Italic.h"Each font above is available in the following sizes: 8pt, 9pt, 10pt, 11pt, 12pt, 13pt, 14pt, 16pt, 18pt, 20pt, 24pt, 28pt, 32pt, 40pt, 48pt, 60pt, 72pt, 96pt.
TGX can cooperate with a few external Arduino libraries to add image decoding and TrueType font rendering to the Image class. These bindings are optional: if the external library is not installed or not included by the sketch, TGX still works normally and no extra dependency is required.
| Extension | External library | Purpose |
|---|---|---|
| Drawing text with TrueType fonts | OpenFontRender | Draw TrueType fonts into a TGX image. |
| Drawing PNG images | PNGdec | Decode PNG images into a TGX image. |
| Drawing JPEG images | JPEGDEC | Decode JPEG images into a TGX image. |
| Drawing GIF images | AnimatedGIF | Decode and display GIF frames into a TGX image. |
Install Takkao's OpenFontRender library from https://github.com/takkaO/OpenFontRender/.
Include both libraries in the sketch to activate the TGX/OpenFontRender binding:
Then bind an OpenFontRender object to an image with Image::setOpenFontRender(ofr). After that, OpenFontRender can be used normally: text drawn with ofr is rendered into the selected TGX image.
Example:
/examples/Teensy4/2D/OpenFontRender_test/.Check the OpenFontRender documentation for additional details.
Install Bitbank2's PNGdec library from the Arduino library manager / PlatformIO or directly from https://github.com/bitbank2/PNGdec/.
Include both libraries in the sketch to activate the TGX/PNGdec binding:
Open the PNG with the TGX_PNGDraw callback, then decode it into an image with Image::PNGDecode().
Example:
/examples/Teensy4/2D/PNG_test/.Check the PNGdec documentation for additional details.
Install Bitbank2's JPEGDEC library from the Arduino library manager / PlatformIO or directly from https://github.com/bitbank2/JPEGDEC.
Include both libraries in the sketch to activate the TGX/JPEGDEC binding:
Open the JPEG with the TGX_JPEGDraw callback, then decode it into an image with Image::JPEGDecode().
Example:
/examples/Teensy4/2D/JPEG_test/.Check the JPEGDEC documentation for additional details.
Install Bitbank2's AnimatedGIF library from the Arduino library manager / PlatformIO or directly from https://github.com/bitbank2/AnimatedGIF.
Include both libraries in the sketch to activate the TGX/AnimatedGIF binding:
Open the GIF with the TGX_GIFDraw callback, then draw frames into an image with Image::GIFplayFrame().
This method wraps the playFrame() method from the AnimatedGIF library and can be called repeatedly to display successive frames.
Example:
/examples/Teensy4/2D/GIF_test/.Check the AnimatedGIF documentation for additional details.
This page covered the main 2D drawing methods available on tgx::Image. The examples above are intentionally small; complete sketches are available in /examples.
For the memory model, color types, image views and coordinate conventions, see Basic concepts. For 3D rendering, meshes, shaders and projection matrices, continue with the 3D API introduction.