vggio

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: May 12, 2023 License: BSD-3-Clause Imports: 23 Imported by: 7

Documentation

Overview

Package vggio provides a vg.Canvas implementation backed by Gio, a toolkit that implements portable immediate GUI mode in Go.

More informations about Gio can be found at https://gioui.org/.

Index

Examples

Constants

View Source
const DefaultDPI = 96

DefaultDPI is the default dot resolution for image drawing in dots per inch.

Variables

This section is empty.

Functions

func UseBackgroundColor

func UseBackgroundColor(c color.Color) option

UseBackgroundColor specifies the image background color. Without UseBackgroundColor, the default color is white.

func UseDPI

func UseDPI(dpi int) option

UseDPI sets the dots per inch of a canvas. It should only be used as an option argument when initializing a new canvas.

Types

type Canvas

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

Canvas implements the vg.Canvas interface, drawing to an image.Image using vgimg and painting that image into a Gioui context.

Example
package main

import (
	"image/color"
	"math"
	"os"
	"time"

	"gioui.org/app"
	"gioui.org/io/key"
	"gioui.org/io/system"
	"gioui.org/layout"
	"gioui.org/op"
	"gioui.org/op/clip"
	"gioui.org/unit"

	"gonum.org/v1/plot"
	"gonum.org/v1/plot/plotter"
	"gonum.org/v1/plot/vg"
	"gonum.org/v1/plot/vg/draw"
	"gonum.org/v1/plot/vg/vggio"
)

func main() {
	const (
		w   = 20 * vg.Centimeter
		h   = 15 * vg.Centimeter
		dpi = 96
	)
	go func(w, h vg.Length) {
		defer os.Exit(0)

		win := app.NewWindow(
			app.Title("Gonum"),
			app.Size(
				unit.Dp(float32(w.Dots(dpi))),
				unit.Dp(float32(h.Dots(dpi))),
			),
		)

		done := time.NewTimer(2 * time.Second)
		defer done.Stop()

		for e := range win.Events() {
			switch e := e.(type) {
			case system.FrameEvent:
				var (
					ops op.Ops
					gtx = layout.NewContext(&ops, e)
				)
				// register a global key listener for the escape key wrapping our entire UI.
				area := clip.Rect{Max: gtx.Constraints.Max}.Push(gtx.Ops)
				key.InputOp{
					Tag:  win,
					Keys: key.NameEscape + "|Ctrl-Q|Alt-Q",
				}.Add(gtx.Ops)

				for _, e := range gtx.Events(win) {
					switch e := e.(type) {
					case key.Event:
						switch e.Name {
						case key.NameEscape:
							return
						case "Q":
							if e.Modifiers.Contain(key.ModCtrl) {
								return
							}
							if e.Modifiers.Contain(key.ModAlt) {
								return
							}
						}
					}
				}
				area.Pop()

				p := plot.New()
				p.Title.Text = "My title"
				p.X.Label.Text = "X"
				p.Y.Label.Text = "Y"

				quad := plotter.NewFunction(func(x float64) float64 {
					return x * x
				})
				quad.Color = color.RGBA{B: 255, A: 255}

				exp := plotter.NewFunction(func(x float64) float64 {
					return math.Pow(2, x)
				})
				exp.Dashes = []vg.Length{vg.Points(2), vg.Points(2)}
				exp.Width = vg.Points(2)
				exp.Color = color.RGBA{G: 255, A: 255}

				sin := plotter.NewFunction(func(x float64) float64 {
					return 10*math.Sin(x) + 50
				})
				sin.Dashes = []vg.Length{vg.Points(4), vg.Points(5)}
				sin.Width = vg.Points(4)
				sin.Color = color.RGBA{R: 255, A: 255}

				p.Add(quad, exp, sin)
				p.Legend.Add("x^2", quad)
				p.Legend.Add("2^x", exp)
				p.Legend.Add("10*sin(x)+50", sin)
				p.Legend.ThumbnailWidth = 0.5 * vg.Inch

				p.X.Min = 0
				p.X.Max = 10
				p.Y.Min = 0
				p.Y.Max = 100

				p.Add(plotter.NewGrid())

				cnv := vggio.New(gtx, w, h, vggio.UseDPI(dpi))
				p.Draw(draw.New(cnv))
				e.Frame(cnv.Paint())

			case system.DestroyEvent:
				return
			}
		}
	}(w, h)

	app.Main()
}
Output:

func New

func New(gtx layout.Context, w, h vg.Length, opts ...option) *Canvas

New returns a new image canvas with the provided dimensions and options. The currently accepted options are UseDPI and UseBackgroundColor. If the resolution or background color are not specified, defaults are used.

func (*Canvas) DPI added in v0.9.0

func (c *Canvas) DPI() float64

DPI returns the resolution of the receiver in pixels per inch.

func (*Canvas) DrawImage added in v0.9.0

func (c *Canvas) DrawImage(rect vg.Rectangle, img image.Image)

DrawImage draws the image, scaled to fit the destination rectangle.

func (*Canvas) Fill added in v0.9.0

func (c *Canvas) Fill(p vg.Path)

Fill fills the given path.

func (*Canvas) FillString added in v0.9.0

func (c *Canvas) FillString(fnt font.Face, pt vg.Point, txt string)

FillString fills in text at the specified location using the given font. If the font size is zero, the text is not drawn.

func (*Canvas) Paint

func (c *Canvas) Paint() *op.Ops

Paint returns the painting operations.

func (*Canvas) Pop added in v0.9.0

func (c *Canvas) Pop()

Pop restores the context saved by the corresponding call to Push().

func (*Canvas) Push added in v0.9.0

func (c *Canvas) Push()

Push saves the current line width, the current dash pattern, the current transforms, and the current color onto a stack so that the state can later be restored by calling Pop().

func (*Canvas) Rotate added in v0.9.0

func (c *Canvas) Rotate(rad float64)

Rotate applies a rotation transform to the context. The parameter is specified in radians.

func (*Canvas) Scale added in v0.9.0

func (c *Canvas) Scale(x, y float64)

Scale applies a scaling transform to the context.

func (*Canvas) Screenshot added in v0.9.0

func (c *Canvas) Screenshot() (image.Image, error)

Screenshot returns a screenshot of the canvas as an image.

func (*Canvas) SetColor added in v0.9.0

func (c *Canvas) SetColor(clr color.Color)

SetColor sets the current drawing color. Note that fill color and stroke color are the same, so if you want different fill and stroke colors then you must set a color, draw fills, set a new color and then draw lines.

The initial color is black. If SetColor is called with a nil color then black is used.

func (*Canvas) SetLineDash added in v0.9.0

func (c *Canvas) SetLineDash(pattern []vg.Length, offset vg.Length)

SetLineDash sets the dash pattern for lines. The pattern slice specifies the lengths of alternating dashes and gaps, and the offset specifies the distance into the dash pattern to start the dash.

The initial dash pattern is a solid line.

func (*Canvas) SetLineWidth added in v0.9.0

func (c *Canvas) SetLineWidth(w vg.Length)

SetLineWidth sets the width of stroked paths. If the width is not positive then stroked lines are not drawn.

The initial line width is 1 point.

func (*Canvas) Size added in v0.9.0

func (c *Canvas) Size() (w, h vg.Length)

Size implement vg.CanvasSizer.

func (*Canvas) Stroke added in v0.9.0

func (c *Canvas) Stroke(p vg.Path)

Stroke strokes the given path.

func (*Canvas) Translate added in v0.9.0

func (c *Canvas) Translate(pt vg.Point)

Translate applies a translational transform to the context.

Jump to

Keyboard shortcuts

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