offscreen

package
v0.1.17 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 1, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package offscreen renders a widget.Widget tree into an *image.RGBA without a GPU, window, or running application.

This enables headless widget rendering for screenshot testing, multi-process compositors (e.g. github.com/gogpu/compose), PDF/image export, and CI pipelines.

The renderer uses CPU-only rasterization via github.com/gogpu/gg. A Material 3 light theme is applied by default; override with WithTheme.

Basic usage

r := offscreen.NewRenderer(400, 120)
r.Render(primitives.Text("Hello, World!").FontSize(24))
img := r.Image() // *image.RGBA — ready for png.Encode, testing, compositing

HiDPI rendering

r := offscreen.NewRenderer(800, 240, offscreen.WithScale(2.0))

Custom theme and background

dark := material3.NewDark(widget.Hex(0x00BFA5))
r := offscreen.NewRenderer(400, 120,
    offscreen.WithTheme(dark),
    offscreen.WithBackground(widget.ColorWhite),
)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Renderer)

Option configures a Renderer.

func WithBackground

func WithBackground(c widget.Color) Option

WithBackground sets the canvas clear color before drawing. The default is transparent (zero alpha).

func WithFitSize added in v0.1.17

func WithFitSize() Option

WithFitSize enables measure-then-render mode. The renderer measures the widget's preferred size before allocating the canvas, so the caller does not need to know dimensions upfront. Pass width=0, height=0 to NewRenderer.

This follows the universal enterprise pattern (Flutter IntrinsicWidth, Qt6 sizeHint, SwiftUI sizeThatFits, Compose SubcomposeLayout).

Use WithMaxSize to constrain expansion for widgets that grow unbounded.

func WithMaxSize added in v0.1.17

func WithMaxSize(width, height int) Option

WithMaxSize sets maximum dimensions for fit-to-content mode. A value of 0 means unlimited in that axis. Only effective when WithFitSize is also set.

func WithScale

func WithScale(s float32) Option

WithScale sets the display scale factor for HiDPI rendering. The default is 1.0. A value of 2.0 renders at Retina density.

func WithTheme

func WithTheme(tp widget.ThemeProvider) Option

WithTheme overrides the default Material 3 light theme.

type Renderer

type Renderer struct {
	// contains filtered or unexported fields
}

Renderer renders a widget tree into an offscreen image.

Create with NewRenderer. Configure with Option functions. Call Renderer.Render to draw a widget, then Renderer.Image to retrieve the result.

func NewRenderer

func NewRenderer(width, height int, opts ...Option) *Renderer

NewRenderer creates an offscreen renderer with the given pixel dimensions.

Width and height must be positive; they are clamped to a minimum of 1. By default, a Material 3 light theme is applied and the background is transparent. Override with WithTheme, WithBackground, and WithScale.

func (*Renderer) Image

func (r *Renderer) Image() *image.RGBA

Image returns the rendered image, or nil if [Render] has not been called.

func (*Renderer) Render

func (r *Renderer) Render(w widget.Widget)

Render lays out and draws the widget into the offscreen buffer.

In default mode, the widget receives the renderer dimensions as loose constraints. In fit-to-content mode (WithFitSize), the widget is measured first to determine its preferred size, then the canvas is allocated at that size. Calling Render again replaces the previous image.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL