vector

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package vector draws paths into pixels on the CPU: fills with the nonzero or even-odd rule, strokes with joins, caps and dashes, solid colors and linear and radial gradients, clipping and group opacity. The ui package's Canvas and SVG are drawn with it, into an image the renderer shows.

Coordinates are float32 pixels, y down. Colors are straight (not premultiplied) R, G, B, A from 0 to 1; the image drawn into is premultiplied.

Index

Constants

This section is empty.

Variables

View Source
var Identity = Matrix{A: 1, D: 1}

Identity is the transform that changes nothing.

Functions

This section is empty.

Types

type Canvas

type Canvas struct {

	// Tolerance is how far, in pixels, flattened curves may stray; 0 means
	// 0.05, which loses no visible area to the straight segments.
	Tolerance float32
	// contains filtered or unexported fields
}

Canvas draws into a premultiplied RGBA image. It keeps a stack of states, each with a transform, a clip and an opacity, and of layers, which group drawing so it is faded as a whole.

func NewCanvas

func NewCanvas(w, h int) *Canvas

NewCanvas returns a transparent canvas of w x h pixels.

func (*Canvas) Clip

func (c *Canvas) Clip(p *Path, rule FillRule)

Clip limits later drawing to the inside of p (in the current coordinates), within the current clip.

func (*Canvas) Fill

func (c *Canvas) Fill(p *Path, paint Paint, rule FillRule)

Fill fills p with paint.

func (*Canvas) Image

func (c *Canvas) Image() *image.RGBA

Image returns the drawing: premultiplied RGBA, the canvas's own buffer.

func (*Canvas) Matrix

func (c *Canvas) Matrix() Matrix

Matrix returns the current transform.

func (*Canvas) PopLayer

func (c *Canvas) PopLayer()

PopLayer draws the innermost layer onto what is below it.

func (*Canvas) PushLayer

func (c *Canvas) PushLayer(alpha float32)

PushLayer starts a layer: drawing goes into it until PopLayer (or the Restore of an earlier Save), which draws it onto what is below at opacity alpha.

func (*Canvas) Reset

func (c *Canvas) Reset(w, h int)

Reset clears the canvas to transparent at w x h pixels and drops the states and layers, keeping the buffers it can.

func (*Canvas) Restore

func (c *Canvas) Restore()

Restore returns to the state of the matching Save. Layers pushed since it are drawn first.

func (*Canvas) Save

func (c *Canvas) Save()

Save pushes a copy of the current state; Restore pops it.

func (*Canvas) SetAlpha

func (c *Canvas) SetAlpha(a float32)

SetAlpha multiplies the opacity of later drawing by a (0 to 1).

func (*Canvas) SetTransform

func (c *Canvas) SetTransform(m Matrix)

SetTransform replaces the current transform.

func (*Canvas) Stroke

func (c *Canvas) Stroke(p *Path, paint Paint, s Stroke)

Stroke draws the outline of p with paint. The width and dashes are in the current coordinates, so a scaled transform scales them too.

func (*Canvas) Transform

func (c *Canvas) Transform(m Matrix)

Transform applies m before the current transform, so later drawing is in m's coordinates.

type Cap

type Cap uint8

Cap is the shape of the ends of an open stroke.

const (
	CapButt   Cap = iota // the stroke stops at the end
	CapRound             // a half circle past the end
	CapSquare            // a half square past the end
)

type Color

type Color struct{ R, G, B, A float32 }

Color is a straight (not premultiplied) R, G, B, A color from 0 to 1.

type FillRule

type FillRule uint8

FillRule decides which parts of a self-overlapping path are inside.

const (
	// NonZero fills every point the path winds around (SVG's default).
	NonZero FillRule = iota
	// EvenOdd fills points that an odd number of the path's edges surround,
	// so overlaps and nested subpaths make holes.
	EvenOdd
)

type Gradient

type Gradient struct {
	Kind GradientKind
	// X0, Y0 to X1, Y1 is a linear gradient's line.
	X0, Y0, X1, Y1 float32
	// A radial gradient runs from the focal point FX, FY to the circle of
	// radius R about CX, CY.
	CX, CY, R, FX, FY float32
	Stops             []Stop
	Spread            Spread
	Matrix            Matrix
}

Gradient is a linear or radial gradient, in the coordinates of the shape it paints, transformed by Matrix (SVG's gradientTransform).

type GradientKind

type GradientKind uint8

GradientKind is the shape of a gradient.

const (
	Linear GradientKind = iota
	Radial
)

type Join

type Join uint8

Join is the shape of a stroke's corners.

const (
	JoinMiter Join = iota // sharp, cut to a bevel past the miter limit
	JoinRound
	JoinBevel
)

type Matrix

type Matrix struct{ A, B, C, D, E, F float32 }

Matrix is a 2D affine transform: a point x, y becomes A*x + C*y + E, B*x + D*y + F (the SVG and canvas matrix(a b c d e f)).

func Rotate

func Rotate(angle float32) Matrix

func Scale

func Scale(sx, sy float32) Matrix

func Skew

func Skew(ax, ay float32) Matrix

Skew returns the transform that skews x by ax and y by ay radians.

func Translate

func Translate(x, y float32) Matrix

Translate, Scale and Rotate return the transforms that move by x, y, scale by sx, sy and rotate by angle radians (clockwise on screen).

func (Matrix) Apply

func (m Matrix) Apply(p Point) Point

Apply transforms p.

func (Matrix) Invert

func (m Matrix) Invert() (Matrix, bool)

Invert returns the inverse transform, and false when m flattens the plane.

func (Matrix) Then

func (m Matrix) Then(n Matrix) Matrix

Then returns the transform that applies m, then n.

type Paint

type Paint struct {
	Color    Color
	Gradient *Gradient
}

Paint is what fills or strokes a shape: a color, or a gradient when Gradient is set.

type Path

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

Path is a sequence of subpaths made of lines and Bézier curves. The zero value is an empty path.

func (*Path) Append

func (p *Path) Append(o *Path)

Append adds the subpaths of o to p.

func (*Path) Arc

func (p *Path) Arc(cx, cy, r, a0, a1 float32, ccw bool)

Arc adds an arc of the circle centered on cx, cy, of radius r, from angle a0 to a1 in radians (0 is to the right, angles grow clockwise on screen), counterclockwise when ccw is set. As in the HTML canvas, a line joins the current point to the arc's start, and a sweep of a full turn or more draws the whole circle.

func (*Path) ArcTo

func (p *Path) ArcTo(rx, ry, rotation float32, large, sweep bool, x, y float32)

ArcTo adds an elliptical arc to x, y, as the SVG path command A does: radii rx, ry, the ellipse rotated by rotation degrees, and the flags that pick one of the four arcs that join the points.

func (*Path) Bounds

func (p *Path) Bounds() (x0, y0, x1, y1 float32, ok bool)

Bounds returns the box of the path's points, control points included, and false for an empty path.

func (*Path) Close

func (p *Path) Close()

Close closes the subpath with a line back to its start.

func (*Path) CubicTo

func (p *Path) CubicTo(c1x, c1y, c2x, c2y, x, y float32)

CubicTo adds a cubic Bézier curve with the control points c1 and c2 to x, y.

func (*Path) Current

func (p *Path) Current() (Point, bool)

Current returns the end of the path so far, and false before a MoveTo.

func (*Path) Ellipse

func (p *Path) Ellipse(cx, cy, rx, ry float32)

Ellipse adds a closed ellipse centered on cx, cy.

func (*Path) Empty

func (p *Path) Empty() bool

Empty reports whether the path has nothing to draw.

func (*Path) LineTo

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

LineTo adds a straight line to x, y.

func (*Path) MoveTo

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

MoveTo starts a new subpath at x, y.

func (*Path) QuadTo

func (p *Path) QuadTo(cx, cy, x, y float32)

QuadTo adds a quadratic Bézier curve through the control point cx, cy to x, y.

func (*Path) Rect

func (p *Path) Rect(x, y, w, h float32)

Rect adds a closed rectangle.

func (*Path) RoundRect

func (p *Path) RoundRect(x, y, w, h, rx, ry float32)

RoundRect adds a closed rectangle whose corners are quarter ellipses of radii rx, ry, which are limited to half the sides.

func (*Path) Transform

func (p *Path) Transform(m Matrix) *Path

Transform returns the path with every point transformed by m.

type Point

type Point struct{ X, Y float32 }

Point is a position.

type Spread

type Spread uint8

Spread is how a gradient continues past its ends.

const (
	SpreadPad     Spread = iota // the end colors continue
	SpreadReflect               // the gradient repeats, mirrored each time
	SpreadRepeat                // the gradient repeats
)

type Stop

type Stop struct {
	Offset float32
	Color  Color
}

Stop is a color at Offset (0 to 1) along a gradient.

type Stroke

type Stroke struct {
	Width float32
	Cap   Cap
	Join  Join
	// MiterLimit is the longest miter, as a multiple of the width, before a
	// miter join becomes a bevel; 0 means 4, as in SVG.
	MiterLimit float32
	// Dash alternates the lengths of dashes and gaps; empty draws a solid line.
	// DashOffset is how far into the pattern the stroke starts.
	Dash       []float32
	DashOffset float32
}

Stroke describes how a path's outline is drawn.

Jump to

Keyboard shortcuts

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