vggio

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2020 License: BSD-3-Clause Imports: 7 Imported by: 7

Documentation

Overview

Package vggio provides a vg.Canvas implementation backed by Gioui.

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 {
	*vgimg.Canvas
	// 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 (
	"log"
	"os"
	"time"

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

	"gonum.org/v1/plot"
	"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) {
		win := app.NewWindow(
			app.Title("Gonum"),
			app.Size(
				unit.Px(float32(w.Dots(dpi))),
				unit.Px(float32(h.Dots(dpi))),
			),
		)
		defer win.Close()

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

		for {
			select {
			case e := <-win.Events():
				switch e := e.(type) {
				case system.FrameEvent:
					p, err := plot.New()
					if err != nil {
						log.Fatalf("could not create plot: %+v", err)
					}
					p.Title.Text = "My title"
					p.X.Label.Text = "X"
					p.Y.Label.Text = "Y"

					gtx := layout.NewContext(new(op.Ops), e)
					cnv := vggio.New(gtx, w, h, vggio.UseDPI(dpi))
					p.Draw(draw.New(cnv))
					cnv.Paint(e)

				case system.DestroyEvent:
					os.Exit(0)

				case key.Event:
					switch e.Name {
					case "Q", key.NameEscape:
						os.Exit(0)
					}
				}
			case <-done.C:
				os.Exit(0)
			}
		}
	}(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) Paint

func (c *Canvas) Paint(e system.FrameEvent)

Paint paints the canvas' content on the screen.

Jump to

Keyboard shortcuts

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