canvas

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package canvas defines the rendering backend abstraction. All drawing operations go through the Canvas interface, which decouples geometry rendering from the specific graphics library (gogpu/gg, SVG, etc.).

Index

Constants

This section is empty.

Variables

View Source
var (
	AnchorTopLeft      = TextAnchor{0, 0}
	AnchorTopCenter    = TextAnchor{0.5, 0}
	AnchorTopRight     = TextAnchor{1, 0}
	AnchorMiddleLeft   = TextAnchor{0, 0.5}
	AnchorCenter       = TextAnchor{0.5, 0.5}
	AnchorMiddleRight  = TextAnchor{1, 0.5}
	AnchorBottomLeft   = TextAnchor{0, 1}
	AnchorBottomCenter = TextAnchor{0.5, 1}
	AnchorBottomRight  = TextAnchor{1, 1}
)

Common text anchors.

Functions

func ExportPDF

func ExportPDF(r *recording.Recording, w io.Writer) (int64, error)

ExportPDF replays a recording into the native PDF backend and writes to w.

func ExportSVG

func ExportSVG(r *recording.Recording, w io.Writer) (int64, error)

ExportSVG replays a recording into the native SVG backend and writes to w.

Types

type Canvas

type Canvas interface {

	// Save pushes the current graphics state (color, transform, clip) onto a stack.
	Save()
	// Restore pops the graphics state stack.
	Restore()

	// Translate shifts the origin by (dx, dy).
	Translate(dx, dy float64)
	// Rotate applies a rotation of angle radians around the current origin.
	Rotate(angle float64)
	// ScaleXY applies a non-uniform scale.
	ScaleXY(sx, sy float64)

	// MoveTo starts a new sub-path at (x, y).
	MoveTo(x, y float64)
	// LineTo adds a line segment from the current point to (x, y).
	LineTo(x, y float64)
	// QuadraticTo adds a quadratic Bézier curve to (x, y) via control point (cx, cy).
	QuadraticTo(cx, cy, x, y float64)
	// CubicTo adds a cubic Bézier curve to (x, y) via control points (cx1, cy1) and (cx2, cy2).
	CubicTo(cx1, cy1, cx2, cy2, x, y float64)
	// ClosePath closes the current sub-path with a line to the start point.
	ClosePath()
	// ClearPath discards the current path without drawing.
	ClearPath()

	// SetColor sets the current drawing color for both fill and stroke.
	SetColor(c color.Color)
	// SetRGBA sets the current drawing color from normalized components.
	SetRGBA(r, g, b, a float64)
	// SetLineWidth sets the stroke width in pixels.
	SetLineWidth(w float64)
	// SetLineDash sets the dash pattern. Pass nil or empty for solid lines.
	SetLineDash(pattern ...float64)
	// Fill fills the current path with the current color, then clears the path.
	Fill()
	// Stroke draws the current path outline, then clears the path.
	Stroke()
	// FillPreserve fills without clearing the path (allows subsequent stroke).
	FillPreserve()
	// Clip clips rendering to the current path. Call after constructing a path
	// (e.g. DrawRectangle). Use Save/Restore to undo the clip.
	Clip()

	// DrawCircle adds a circle path centered at (cx, cy) with radius r.
	DrawCircle(cx, cy, r float64)
	// DrawRectangle adds a rectangle path at (x, y) with dimensions (w, h).
	DrawRectangle(x, y, w, h float64)
	// DrawLine adds a line path from (x1, y1) to (x2, y2).
	DrawLine(x1, y1, x2, y2 float64)

	// SetFontSize sets the font size in points.
	SetFontSize(size float64)
	// DrawStringAnchored draws text at (x, y) with anchor (ax, ay) ∈ [0,1].
	// (0,0) = top-left, (0.5,0.5) = center, (1,1) = bottom-right.
	DrawStringAnchored(text string, x, y, ax, ay float64)
	// MeasureString returns the width and height of the rendered text.
	MeasureString(text string) (w, h float64)

	// Clear fills the entire canvas with the given color.
	Clear(c color.Color)
	// Width returns the canvas width in pixels.
	Width() int
	// Height returns the canvas height in pixels.
	Height() int
}

Canvas is the core rendering abstraction. Implementations provide the actual drawing primitives targeting specific backends (CPU rasterizer, GPU pipeline, SVG writer, etc.).

The API is modeled after gogpu/gg's Context for natural integration, but abstracted so alternative backends can be swapped without changing any rendering logic.

type FontWeight

type FontWeight int

FontWeight describes font weight for text rendering.

const (
	WeightNormal FontWeight = 400
	WeightBold   FontWeight = 700
)

FontWeight constants for common weights.

type GGCanvas

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

GGCanvas wraps a gogpu/gg.Context to implement the Canvas interface. This is the primary CPU-based rendering backend.

func FromGGContext

func FromGGContext(ctx *gg.Context) *GGCanvas

FromGGContext wraps an existing gg.Context.

func NewGGCanvas

func NewGGCanvas(width, height int) *GGCanvas

NewGGCanvas creates a Canvas backed by a gogpu/gg rasterizer.

func (*GGCanvas) Clear

func (c *GGCanvas) Clear(col color.Color)

Clear fills the entire canvas with the given color.

func (*GGCanvas) ClearPath

func (c *GGCanvas) ClearPath()

ClearPath discards the current path without drawing.

func (*GGCanvas) Clip

func (c *GGCanvas) Clip()

Clip clips rendering to the current path.

func (*GGCanvas) ClosePath

func (c *GGCanvas) ClosePath()

ClosePath closes the current sub-path.

func (*GGCanvas) Context

func (c *GGCanvas) Context() *gg.Context

Context returns the underlying gg.Context for direct access when needed (e.g., font loading, image export).

func (*GGCanvas) CubicTo

func (c *GGCanvas) CubicTo(cx1, cy1, cx2, cy2, x, y float64)

CubicTo adds a cubic Bézier curve via (cx1, cy1) and (cx2, cy2) to (x, y).

func (*GGCanvas) DrawCircle

func (c *GGCanvas) DrawCircle(cx, cy, r float64)

DrawCircle adds a circle path centered at (cx, cy) with radius r.

func (*GGCanvas) DrawLine

func (c *GGCanvas) DrawLine(x1, y1, x2, y2 float64)

DrawLine adds a line path from (x1, y1) to (x2, y2).

func (*GGCanvas) DrawRectangle

func (c *GGCanvas) DrawRectangle(x, y, w, h float64)

DrawRectangle adds a rectangle path at (x, y) with dimensions (w, h).

func (*GGCanvas) DrawStringAnchored

func (c *GGCanvas) DrawStringAnchored(s string, x, y, ax, ay float64)

DrawStringAnchored draws text at (x, y) with anchor (ax, ay).

func (*GGCanvas) EncodePNG

func (c *GGCanvas) EncodePNG(w io.Writer) error

EncodePNG writes the canvas as PNG to the given writer.

func (*GGCanvas) Fill

func (c *GGCanvas) Fill()

Fill fills the current path with the current color, then clears the path.

func (*GGCanvas) FillPreserve

func (c *GGCanvas) FillPreserve()

FillPreserve fills without clearing the path (allows subsequent stroke).

func (*GGCanvas) Height

func (c *GGCanvas) Height() int

Height returns the canvas height in pixels.

func (*GGCanvas) Image

func (c *GGCanvas) Image() image.Image

Image returns the underlying image.

func (*GGCanvas) LineTo

func (c *GGCanvas) LineTo(x, y float64)

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

func (*GGCanvas) MeasureString

func (c *GGCanvas) MeasureString(s string) (float64, float64)

MeasureString returns the width and height of the rendered text.

func (*GGCanvas) MoveTo

func (c *GGCanvas) MoveTo(x, y float64)

MoveTo starts a new sub-path at (x, y).

func (*GGCanvas) QuadraticTo

func (c *GGCanvas) QuadraticTo(cx, cy, x, y float64)

QuadraticTo adds a quadratic Bézier curve via control point (cx, cy) to (x, y).

func (*GGCanvas) Restore

func (c *GGCanvas) Restore()

Restore pops the graphics state from the stack.

func (*GGCanvas) Rotate

func (c *GGCanvas) Rotate(angle float64)

Rotate applies a rotation of angle radians.

func (*GGCanvas) Save

func (c *GGCanvas) Save()

Save pushes the current graphics state onto the stack.

func (*GGCanvas) SavePNG

func (c *GGCanvas) SavePNG(path string) error

SavePNG writes the canvas to a PNG file.

func (*GGCanvas) ScaleXY

func (c *GGCanvas) ScaleXY(sx, sy float64)

ScaleXY applies a non-uniform scale.

func (*GGCanvas) SetColor

func (c *GGCanvas) SetColor(col color.Color)

SetColor sets the current drawing color for both fill and stroke.

func (*GGCanvas) SetFontSize

func (c *GGCanvas) SetFontSize(size float64)

SetFontSize resolves the best available font at the given size. Resolution order: system font via fonts resolver -> embedded Go Regular fallback.

func (*GGCanvas) SetLineDash

func (c *GGCanvas) SetLineDash(pattern ...float64)

SetLineDash sets the dash pattern. Pass nil or empty for solid lines.

func (*GGCanvas) SetLineWidth

func (c *GGCanvas) SetLineWidth(w float64)

SetLineWidth sets the stroke width in pixels.

func (*GGCanvas) SetRGBA

func (c *GGCanvas) SetRGBA(r, g, b, a float64)

SetRGBA sets the current drawing color from normalized components.

func (*GGCanvas) Stroke

func (c *GGCanvas) Stroke()

Stroke draws the current path outline, then clears the path.

func (*GGCanvas) Translate

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

Translate shifts the origin by (dx, dy).

func (*GGCanvas) Width

func (c *GGCanvas) Width() int

Width returns the canvas width in pixels.

type RecordingCanvas

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

RecordingCanvas wraps a recording.Recorder to implement the Canvas interface. All drawing operations are captured as a recording that can later be replayed into any backend (SVG, PDF, raster, etc.) via [Recording.Playback].

func NewRecordingCanvas

func NewRecordingCanvas(width, height int) *RecordingCanvas

NewRecordingCanvas creates a Canvas that records all drawing operations. After rendering, call RecordingCanvas.FinishRecording and replay into the desired backend (e.g., SVG, PDF).

func (*RecordingCanvas) Clear

func (c *RecordingCanvas) Clear(col color.Color)

Clear fills the entire canvas with the given color.

func (*RecordingCanvas) ClearPath

func (c *RecordingCanvas) ClearPath()

ClearPath discards the current path without drawing.

func (*RecordingCanvas) Clip

func (c *RecordingCanvas) Clip()

Clip clips rendering to the current path.

func (*RecordingCanvas) ClosePath

func (c *RecordingCanvas) ClosePath()

ClosePath closes the current sub-path.

func (*RecordingCanvas) CubicTo

func (c *RecordingCanvas) CubicTo(cx1, cy1, cx2, cy2, x, y float64)

CubicTo adds a cubic Bézier curve via (cx1, cy1) and (cx2, cy2) to (x, y).

func (*RecordingCanvas) DrawCircle

func (c *RecordingCanvas) DrawCircle(cx, cy, r float64)

DrawCircle adds a circle path centered at (cx, cy) with radius r.

func (*RecordingCanvas) DrawLine

func (c *RecordingCanvas) DrawLine(x1, y1, x2, y2 float64)

DrawLine adds a line path from (x1, y1) to (x2, y2).

func (*RecordingCanvas) DrawRectangle

func (c *RecordingCanvas) DrawRectangle(x, y, w, h float64)

DrawRectangle adds a rectangle path at (x, y) with dimensions (w, h).

func (*RecordingCanvas) DrawStringAnchored

func (c *RecordingCanvas) DrawStringAnchored(s string, x, y, ax, ay float64)

DrawStringAnchored draws text at (x, y) with anchor (ax, ay).

func (*RecordingCanvas) Fill

func (c *RecordingCanvas) Fill()

Fill fills the current path with the current color, then clears the path.

func (*RecordingCanvas) FillPreserve

func (c *RecordingCanvas) FillPreserve()

FillPreserve fills without clearing the path (allows subsequent stroke).

func (*RecordingCanvas) FinishRecording

func (c *RecordingCanvas) FinishRecording() *recording.Recording

FinishRecording completes the recording and returns the captured operations.

func (*RecordingCanvas) Height

func (c *RecordingCanvas) Height() int

Height returns the canvas height in pixels.

func (*RecordingCanvas) LineTo

func (c *RecordingCanvas) LineTo(x, y float64)

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

func (*RecordingCanvas) MeasureString

func (c *RecordingCanvas) MeasureString(s string) (float64, float64)

MeasureString returns the width and height of the rendered text.

func (*RecordingCanvas) MoveTo

func (c *RecordingCanvas) MoveTo(x, y float64)

MoveTo starts a new sub-path at (x, y).

func (*RecordingCanvas) QuadraticTo

func (c *RecordingCanvas) QuadraticTo(cx, cy, x, y float64)

QuadraticTo adds a quadratic Bézier curve via control point (cx, cy) to (x, y).

func (*RecordingCanvas) Restore

func (c *RecordingCanvas) Restore()

Restore pops the graphics state from the stack.

func (*RecordingCanvas) Rotate

func (c *RecordingCanvas) Rotate(angle float64)

Rotate applies a rotation of angle radians.

func (*RecordingCanvas) Save

func (c *RecordingCanvas) Save()

Save pushes the current graphics state onto the stack.

func (*RecordingCanvas) ScaleXY

func (c *RecordingCanvas) ScaleXY(sx, sy float64)

ScaleXY applies a non-uniform scale.

func (*RecordingCanvas) SetColor

func (c *RecordingCanvas) SetColor(col color.Color)

SetColor sets the current drawing color for both fill and stroke.

func (*RecordingCanvas) SetFontSize

func (c *RecordingCanvas) SetFontSize(size float64)

SetFontSize resolves the best available font at the given size.

func (*RecordingCanvas) SetLineDash

func (c *RecordingCanvas) SetLineDash(pattern ...float64)

SetLineDash sets the dash pattern. Pass nil or empty for solid lines.

func (*RecordingCanvas) SetLineWidth

func (c *RecordingCanvas) SetLineWidth(w float64)

SetLineWidth sets the stroke width in pixels.

func (*RecordingCanvas) SetRGBA

func (c *RecordingCanvas) SetRGBA(r, g, b, a float64)

SetRGBA sets the current drawing color from normalized components.

func (*RecordingCanvas) Stroke

func (c *RecordingCanvas) Stroke()

Stroke draws the current path outline, then clears the path.

func (*RecordingCanvas) Translate

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

Translate shifts the origin by (dx, dy).

func (*RecordingCanvas) Width

func (c *RecordingCanvas) Width() int

Width returns the canvas width in pixels.

type TextAnchor

type TextAnchor struct {
	X, Y float64 // [0,1] where (0,0) = top-left, (1,1) = bottom-right
}

TextAnchor defines text alignment as normalized coordinates.

Jump to

Keyboard shortcuts

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