Skip to main content

rendering

Package rendering provides low-level graphics primitives for drawing.

This package defines the fundamental types for rendering user interfaces: geometry (Size, Offset, Rect), colors, gradients, paints, and the Canvas interface for executing drawing operations.

Geometry

Basic geometric types for layout and positioning:

size := rendering.Size{Width: 100, Height: 50}
offset := rendering.Offset{X: 10, Y: 20}
rect := rendering.RectFromLTWH(0, 0, 100, 50)

Colors

Colors are stored as ARGB (0xAARRGGBB):

red := rendering.RGB(255, 0, 0)           // Opaque red
semiTransparent := rendering.RGBA(0, 0, 0, 128) // 50% black

Drawing

The Canvas interface provides drawing operations:

canvas.DrawRect(rect, paint)
canvas.DrawRRect(rrect, paint)
canvas.DrawText(layout, offset)

Paint controls how shapes are filled or stroked:

paint := rendering.DefaultPaint()
paint.Color = rendering.ColorBlue
paint.Style = rendering.PaintStyleStroke
paint.StrokeWidth = 2

Variables

Common colors.

var (
ColorTransparent = Color(0x00000000)
ColorBlack = Color(0xFF000000)
ColorWhite = Color(0xFFFFFFFF)
ColorRed = Color(0xFFFF0000)
ColorGreen = Color(0xFF00FF00)
ColorBlue = Color(0xFF0000FF)
)

type BlurStyle

BlurStyle controls how the blur mask is generated.

type BlurStyle int
const (
// BlurStyleNormal blurs inside and outside the shape.
BlurStyleNormal BlurStyle = iota
// BlurStyleSolid keeps the shape solid inside, blurs outside.
BlurStyleSolid
// BlurStyleOuter draws nothing inside, blurs outside only.
BlurStyleOuter
// BlurStyleInner blurs inside the shape only, nothing outside.
BlurStyleInner
)

type BoxShadow

BoxShadow defines a shadow to draw behind a shape.

type BoxShadow struct {
Color Color
Offset Offset
BlurRadius float64 // sigma = blurRadius * 0.5
Spread float64
BlurStyle BlurStyle
}

func BoxShadowElevation

func BoxShadowElevation(level int, color Color) *BoxShadow

BoxShadowElevation returns a Material-style elevation shadow. Level should be 1-5, where higher levels have larger blur and offset.

func NewBoxShadow

func NewBoxShadow(color Color, blurRadius float64) *BoxShadow

NewBoxShadow creates a simple drop shadow with the given color and blur radius. Offset defaults to (0, 2) for a subtle downward shadow.

func (BoxShadow) Sigma

func (s BoxShadow) Sigma() float64

Sigma returns the blur sigma for Skia's mask filter. Returns 0 if BlurRadius is negative.

type Canvas

Canvas records or renders drawing commands.

type Canvas interface {
// Save pushes the current transform and clip state.
Save()

// SaveLayerAlpha saves a new layer with the given opacity (0.0 to 1.0).
// All drawing until the matching Restore() call will be composited with this opacity.
SaveLayerAlpha(bounds Rect, alpha float64)

// Restore pops the most recent transform and clip state.
Restore()

// Translate moves the origin by the given offset.
Translate(dx, dy float64)

// Scale scales the coordinate system by the given factors.
Scale(sx, sy float64)

// Rotate rotates the coordinate system by radians.
Rotate(radians float64)

// ClipRect restricts future drawing to the given rectangle.
ClipRect(rect Rect)

// ClipRRect restricts future drawing to the given rounded rectangle.
ClipRRect(rrect RRect)

// Clear fills the entire canvas with the given color.
Clear(color Color)

// DrawRect draws a rectangle with the provided paint.
DrawRect(rect Rect, paint Paint)

// DrawRRect draws a rounded rectangle with the provided paint.
DrawRRect(rrect RRect, paint Paint)

// DrawCircle draws a circle with the provided paint.
DrawCircle(center Offset, radius float64, paint Paint)

// DrawLine draws a line segment with the provided paint.
DrawLine(start, end Offset, paint Paint)

// DrawText draws a pre-shaped text layout at the given position.
DrawText(layout *TextLayout, position Offset)

// DrawImage draws an image with its top-left corner at the given position.
DrawImage(image image.Image, position Offset)

// DrawPath draws a path with the provided paint.
DrawPath(path *Path, paint Paint)

// DrawRectShadow draws a shadow behind a rectangle.
DrawRectShadow(rect Rect, shadow BoxShadow)

// DrawRRectShadow draws a shadow behind a rounded rectangle.
DrawRRectShadow(rrect RRect, shadow BoxShadow)

// SaveLayerBlur saves a layer with a backdrop blur effect.
// Content drawn before this call will be blurred within the bounds.
// Call Restore() to apply the blur and pop the layer.
SaveLayerBlur(bounds Rect, sigmaX, sigmaY float64)

// Size returns the size of the canvas in pixels.
Size() Size
}

type Color

Color is stored as ARGB (0xAARRGGBB).

type Color uint32

func RGB

func RGB(r, g, b uint8) Color

RGB constructs an opaque Color from red, green, blue bytes.

func RGBA

func RGBA(r, g, b, a uint8) Color

RGBA constructs a Color from red, green, blue, alpha bytes.

func (Color) RGBAF

func (c Color) RGBAF() (r, g, b, a float64)

RGBAF returns normalized color components (0.0 to 1.0).

func (Color) WithAlpha

func (c Color) WithAlpha(a uint8) Color

WithAlpha returns a copy of the color with the given alpha (0-255).

type DisplayList

DisplayList is an immutable list of drawing operations. It can be replayed onto any Canvas implementation.

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

func (*DisplayList) Paint

func (d *DisplayList) Paint(canvas Canvas)

Paint replays the recorded operations onto the provided canvas.

func (*DisplayList) Size

func (d *DisplayList) Size() Size

Size returns the size recorded when the display list was created.

type FontManager

FontManager manages font registration for text rendering.

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

func DefaultFontManager

func DefaultFontManager() *FontManager

DefaultFontManager returns a shared font manager with a bundled font. For backward compatibility, returns nil on error.

func DefaultFontManagerErr

func DefaultFontManagerErr() (*FontManager, error)

DefaultFontManagerErr returns a shared font manager with a bundled font. It returns both the manager and any error that occurred during initialization.

func NewFontManager

func NewFontManager() (*FontManager, error)

NewFontManager creates a font manager using system defaults.

func (*FontManager) Face

func (m *FontManager) Face(style TextStyle) (font.Face, error)

Face resolves a font face for the given style. Skia-backed builds do not expose font.Face instances.

func (*FontManager) RegisterFont

func (m *FontManager) RegisterFont(name string, data []byte) error

RegisterFont registers a new font family from TrueType data.

type FontStyle

FontStyle represents normal or italic text styles.

type FontStyle int
const (
FontStyleNormal FontStyle = iota
FontStyleItalic
)

type FontWeight

FontWeight represents a numeric font weight.

type FontWeight int
const (
FontWeightNormal FontWeight = 400
FontWeightSemibold FontWeight = 600
FontWeightBold FontWeight = 700
)

type Gradient

Gradient describes a linear or radial gradient.

type Gradient struct {
Type GradientType
Linear LinearGradient
Radial RadialGradient
}

func NewLinearGradient

func NewLinearGradient(start, end Offset, stops []GradientStop) *Gradient

NewLinearGradient constructs a linear gradient definition.

func NewRadialGradient

func NewRadialGradient(center Offset, radius float64, stops []GradientStop) *Gradient

NewRadialGradient constructs a radial gradient definition.

func (*Gradient) IsValid

func (g *Gradient) IsValid() bool

IsValid reports whether the gradient has usable stops.

func (*Gradient) Stops

func (g *Gradient) Stops() []GradientStop

Stops returns the gradient stops for the configured type.

type GradientStop

GradientStop defines a color stop within a gradient.

type GradientStop struct {
Position float64
Color Color
}

type GradientType

GradientType describes the gradient variant.

type GradientType int
const (
// GradientTypeNone indicates no gradient is applied.
GradientTypeNone GradientType = iota
// GradientTypeLinear indicates a linear gradient.
GradientTypeLinear
// GradientTypeRadial indicates a radial gradient.
GradientTypeRadial
)

type LinearGradient

LinearGradient defines a gradient between two points.

type LinearGradient struct {
Start Offset
End Offset
Stops []GradientStop
}

type Offset

Offset represents a 2D point or vector in pixel coordinates.

type Offset struct {
X float64
Y float64
}

type Paint

Paint describes how to draw a shape on the canvas.

type Paint struct {
Color Color
Gradient *Gradient
Style PaintStyle
StrokeWidth float64
}

func DefaultPaint

func DefaultPaint() Paint

DefaultPaint returns a basic fill paint.

type PaintStyle

PaintStyle describes how shapes are filled or stroked.

type PaintStyle int
const (
// PaintStyleFill fills the shape interior.
PaintStyleFill PaintStyle = iota

// PaintStyleStroke draws only the outline.
PaintStyleStroke

// PaintStyleFillAndStroke fills and then strokes the outline.
PaintStyleFillAndStroke
)

type Path

Path represents a vector path consisting of move, line, and curve commands.

type Path struct {
Commands []PathCommand
FillRule PathFillRule
}

func NewPath

func NewPath() *Path

NewPath creates a new empty path with nonzero fill rule.

func NewPathWithFillRule

func NewPathWithFillRule(fillRule PathFillRule) *Path

NewPathWithFillRule creates a new empty path with the specified fill rule.

func (*Path) Clear

func (p *Path) Clear()

Clear removes all commands from the path.

func (*Path) Close

func (p *Path) Close()

Close closes the current subpath by drawing a line to the starting point.

func (*Path) CubicTo

func (p *Path) CubicTo(x1, y1, x2, y2, x3, y3 float64)

CubicTo adds a cubic bezier curve from the current point to (x3, y3) with control points (x1, y1) and (x2, y2).

func (*Path) IsEmpty

func (p *Path) IsEmpty() bool

IsEmpty returns true if the path has no commands.

func (*Path) LineTo

func (p *Path) LineTo(x, y float64)

LineTo adds a line segment from the current point to (x, y).

func (*Path) MoveTo

func (p *Path) MoveTo(x, y float64)

MoveTo starts a new subpath at the given point.

func (*Path) QuadTo

func (p *Path) QuadTo(x1, y1, x2, y2 float64)

QuadTo adds a quadratic bezier curve from the current point to (x2, y2) with control point (x1, y1).

type PathCommand

PathCommand represents a single path command with its arguments.

type PathCommand struct {
Op PathOp
Args []float64
}

type PathFillRule

PathFillRule represents the fill rule for a path.

type PathFillRule int
const (
// FillRuleNonZero uses the nonzero winding rule.
FillRuleNonZero PathFillRule = iota
// FillRuleEvenOdd uses the even-odd rule (for paths with holes).
FillRuleEvenOdd
)

type PathOp

PathOp represents a path operation type.

type PathOp int
const (
PathOpMoveTo PathOp = iota
PathOpLineTo
PathOpQuadTo
PathOpCubicTo
PathOpClose
)

type PictureRecorder

PictureRecorder records drawing commands into a display list.

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

func (*PictureRecorder) BeginRecording

func (r *PictureRecorder) BeginRecording(size Size) Canvas

BeginRecording starts a new recording session.

func (*PictureRecorder) EndRecording

func (r *PictureRecorder) EndRecording() *DisplayList

EndRecording finishes the recording and returns a display list.

type RRect

RRect represents a rounded rectangle with per-corner radii.

type RRect struct {
Rect Rect
TopLeft Radius
TopRight Radius
BottomRight Radius
BottomLeft Radius
}

func RRectFromRectAndRadius

func RRectFromRectAndRadius(rect Rect, radius Radius) RRect

RRectFromRectAndRadius creates a rounded rectangle with uniform corner radii.

func (RRect) UniformRadius

func (r RRect) UniformRadius() float64

UniformRadius returns a single radius value if all corners match, or 0 if not.

type RadialGradient

RadialGradient defines a gradient from a center point.

type RadialGradient struct {
Center Offset
Radius float64
Stops []GradientStop
}

type Radius

Radius represents corner radii for rounded rectangles.

type Radius struct {
X float64
Y float64
}

func CircularRadius

func CircularRadius(value float64) Radius

CircularRadius creates a circular radius with equal X/Y values.

type Rect

Rect represents a rectangle using left, top, right, bottom coordinates.

type Rect struct {
Left float64
Top float64
Right float64
Bottom float64
}

func RectFromLTWH

func RectFromLTWH(left, top, width, height float64) Rect

RectFromLTWH constructs a Rect from left, top, width, height values.

func (Rect) Center

func (r Rect) Center() Offset

Center returns the center point of the rectangle.

func (Rect) Height

func (r Rect) Height() float64

Height returns the height of the rectangle.

func (Rect) Size

func (r Rect) Size() Size

Size returns the size of the rectangle.

func (Rect) Width

func (r Rect) Width() float64

Width returns the width of the rectangle.

type Size

Size represents width and height dimensions in pixels.

type Size struct {
Width float64
Height float64
}

type SkiaCanvas

SkiaCanvas implements Canvas using the Skia backend.

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

func NewSkiaCanvas

func NewSkiaCanvas(canvas unsafe.Pointer, size Size) *SkiaCanvas

NewSkiaCanvas wraps a Skia canvas pointer as a Canvas.

func (*SkiaCanvas) Clear

func (c *SkiaCanvas) Clear(color Color)

func (*SkiaCanvas) ClipRRect

func (c *SkiaCanvas) ClipRRect(rrect RRect)

func (*SkiaCanvas) ClipRect

func (c *SkiaCanvas) ClipRect(rect Rect)

func (*SkiaCanvas) DrawCircle

func (c *SkiaCanvas) DrawCircle(center Offset, radius float64, paint Paint)

func (*SkiaCanvas) DrawImage

func (c *SkiaCanvas) DrawImage(image image.Image, position Offset)

func (*SkiaCanvas) DrawLine

func (c *SkiaCanvas) DrawLine(start, end Offset, paint Paint)

func (*SkiaCanvas) DrawPath

func (c *SkiaCanvas) DrawPath(path *Path, paint Paint)

func (*SkiaCanvas) DrawRRect

func (c *SkiaCanvas) DrawRRect(rrect RRect, paint Paint)

func (*SkiaCanvas) DrawRRectShadow

func (c *SkiaCanvas) DrawRRectShadow(rrect RRect, shadow BoxShadow)

func (*SkiaCanvas) DrawRect

func (c *SkiaCanvas) DrawRect(rect Rect, paint Paint)

func (*SkiaCanvas) DrawRectShadow

func (c *SkiaCanvas) DrawRectShadow(rect Rect, shadow BoxShadow)

func (*SkiaCanvas) DrawText

func (c *SkiaCanvas) DrawText(layout *TextLayout, position Offset)

func (*SkiaCanvas) Restore

func (c *SkiaCanvas) Restore()

func (*SkiaCanvas) Rotate

func (c *SkiaCanvas) Rotate(radians float64)

func (*SkiaCanvas) Save

func (c *SkiaCanvas) Save()

func (*SkiaCanvas) SaveLayerAlpha

func (c *SkiaCanvas) SaveLayerAlpha(bounds Rect, alpha float64)

func (*SkiaCanvas) SaveLayerBlur

func (c *SkiaCanvas) SaveLayerBlur(bounds Rect, sigmaX, sigmaY float64)

func (*SkiaCanvas) Scale

func (c *SkiaCanvas) Scale(sx, sy float64)

func (*SkiaCanvas) Size

func (c *SkiaCanvas) Size() Size

func (*SkiaCanvas) Translate

func (c *SkiaCanvas) Translate(dx, dy float64)

type TextLayout

TextLayout contains measured text metrics and a resolved font face.

type TextLayout struct {
Text string
Style TextStyle
Size Size
Ascent float64
Descent float64
Face font.Face
LineHeight float64
Lines []TextLine
}

func LayoutText

func LayoutText(text string, style TextStyle, manager *FontManager) (*TextLayout, error)

LayoutText measures and shapes the given text using the provided font manager.

func LayoutTextWithConstraints

func LayoutTextWithConstraints(text string, style TextStyle, manager *FontManager, maxWidth float64) (*TextLayout, error)

LayoutTextWithConstraints measures and wraps text within the given width.

type TextLine

TextLine represents a single laid-out line of text.

type TextLine struct {
Text string
Width float64
}

type TextShadow

TextShadow defines a shadow to draw behind text.

type TextShadow struct {
Color Color
Offset Offset
BlurRadius float64 // sigma = blurRadius * 0.5, 0 = hard shadow
}

func NewTextShadow

func NewTextShadow(color Color, blurRadius float64) *TextShadow

NewTextShadow creates a simple text shadow with the given color and blur radius. Offset defaults to (0, 2) for a subtle downward shadow.

func (TextShadow) Sigma

func (s TextShadow) Sigma() float64

Sigma returns the blur sigma for Skia's mask filter. Returns 0 if BlurRadius is zero or negative.

type TextStyle

TextStyle describes how text should be rendered.

type TextStyle struct {
Color Color
Gradient *Gradient
FontFamily string
FontSize float64
FontWeight FontWeight
FontStyle FontStyle
PreserveWhitespace bool
Shadow *TextShadow
}

func (TextStyle) WithColor

func (s TextStyle) WithColor(c Color) TextStyle

WithColor returns a copy of the TextStyle with the specified color.

Generated by gomarkdoc