vgimg

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2020 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package vgimg implements the vg.Canvas interface using github.com/fogleman/gg as a backend to output raster images.

Index

Examples

Constants

View Source
const (
	// DefaultDPI is the default dot resolution for image
	// drawing in dots per inch.
	DefaultDPI = 96

	// DefaultWidth and DefaultHeight are the default canvas
	// dimensions.
	DefaultWidth  = 4 * vg.Inch
	DefaultHeight = 4 * vg.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.

Example
package main

import (
	"image/color"
	"log"

	"github.com/elamre/plot"
	"github.com/elamre/plot/vg"
	"github.com/elamre/plot/vg/draw"
	"github.com/elamre/plot/vg/vgimg"
)

func main() {
	p, err := plot.New()
	if err != nil {
		log.Fatalf("%+v", err)
	}
	p.Title.Text = "Title"
	p.X.Label.Text = "X"
	p.Y.Label.Text = "Y"

	const (
		width  = 10 * vg.Centimeter
		height = 10 * vg.Centimeter
	)

	// Create a new canvas with the given dimensions,
	// and specify an explicit background color for the plot.
	c := vgimg.NewWith(
		vgimg.UseWH(width, height),
		vgimg.UseBackgroundColor(color.Transparent),
	)

	dc := draw.New(c)
	p.Draw(dc)
}
Output:

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.

Example
package main

import (
	"log"

	"github.com/elamre/plot"
	"github.com/elamre/plot/vg"
	"github.com/elamre/plot/vg/draw"
	"github.com/elamre/plot/vg/vgimg"
)

func main() {
	p, err := plot.New()
	if err != nil {
		log.Fatalf("%+v", err)
	}
	p.Title.Text = "Title"
	p.X.Label.Text = "X"
	p.Y.Label.Text = "Y"

	const (
		width  = 10 * vg.Centimeter
		height = 10 * vg.Centimeter
	)

	// Create a new canvas with the given dimensions,
	// and specify an explicit DPI value (dot per inch)
	// for the plot.
	c := vgimg.NewWith(
		vgimg.UseWH(width, height),
		vgimg.UseDPI(72),
	)

	dc := draw.New(c)
	p.Draw(dc)
}
Output:

func UseImage

func UseImage(img draw.Image) option

UseImage specifies an image to create the canvas from. The minimum point of the given image should probably be 0,0.

Note that a copy of the input image is performed. This means that modifications applied to the canvas are not reflected on the original image.

Example
package main

import (
	"image"
	"log"

	"github.com/elamre/plot"
	"github.com/elamre/plot/vg/draw"
	"github.com/elamre/plot/vg/vgimg"
)

func main() {
	p, err := plot.New()
	if err != nil {
		log.Fatalf("%+v", err)
	}
	p.Title.Text = "Title"
	p.X.Label.Text = "X"
	p.Y.Label.Text = "Y"

	img := image.NewRGBA(image.Rect(0, 100, 0, 100))
	// Create a new canvas using the given image to specify the dimensions
	// of the plot.
	//
	// Note that modifications applied to the canvas will not be reflected on
	// the input image.
	c := vgimg.NewWith(
		vgimg.UseImage(img),
	)

	dc := draw.New(c)
	p.Draw(dc)
}
Output:

func UseImageWithContext

func UseImageWithContext(img draw.Image, ctx *gg.Context) option

UseImageWithContext specifies both an image and a graphic context to create the canvas from. The minimum point of the given image should probably be 0,0.

func UseWH

func UseWH(w, h vg.Length) option

UseWH specifies the width and height of the canvas. The size is rounded up to the nearest pixel.

Types

type Canvas

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

Canvas implements the vg.Canvas interface, drawing to an image.Image using draw2d.

func New

func New(w, h vg.Length) *Canvas

New returns a new image canvas.

func NewWith

func NewWith(o ...option) *Canvas

NewWith returns a new image canvas created according to the specified options. The currently accepted options are UseWH, UseDPI, UseImage, and UseImageWithContext. Each of the options specifies the size of the canvas (UseWH, UseImage), the resolution of the canvas (UseDPI), or both (useImageWithContext). If size or resolution are not specified, defaults are used. It panics if size and resolution are overspecified (i.e., too many options are passed).

func (*Canvas) DPI

func (c *Canvas) DPI() float64

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

func (*Canvas) DrawImage

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

DrawImage implements the vg.Canvas.DrawImage method.

func (*Canvas) Fill

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

func (*Canvas) FillString

func (c *Canvas) FillString(font vg.Font, pt vg.Point, str string)

func (*Canvas) Image

func (c *Canvas) Image() draw.Image

Image returns the image the canvas is drawing to.

The dimensions of the returned image must not be modified.

func (*Canvas) Pop

func (c *Canvas) Pop()

func (*Canvas) Push

func (c *Canvas) Push()

func (*Canvas) Rotate

func (c *Canvas) Rotate(t float64)

func (*Canvas) Scale

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

func (*Canvas) SetColor

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

func (*Canvas) SetLineDash

func (c *Canvas) SetLineDash(ds []vg.Length, offs vg.Length)

func (*Canvas) SetLineWidth

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

func (*Canvas) Size

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

func (*Canvas) Stroke

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

func (*Canvas) Translate

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

type JpegCanvas

type JpegCanvas struct {
	*Canvas
}

A JpegCanvas is an image canvas with a WriteTo method that writes a jpeg image.

func (JpegCanvas) WriteTo

func (c JpegCanvas) WriteTo(w io.Writer) (int64, error)

WriteTo implements the io.WriterTo interface, writing a jpeg image.

type PngCanvas

type PngCanvas struct {
	*Canvas
}

A PngCanvas is an image canvas with a WriteTo method that writes a png image.

func (PngCanvas) WriteTo

func (c PngCanvas) WriteTo(w io.Writer) (int64, error)

WriteTo implements the io.WriterTo interface, writing a png image.

type TiffCanvas

type TiffCanvas struct {
	*Canvas
}

A TiffCanvas is an image canvas with a WriteTo method that writes a tiff image.

func (TiffCanvas) WriteTo

func (c TiffCanvas) WriteTo(w io.Writer) (int64, error)

WriteTo implements the io.WriterTo interface, writing a tiff image.

Jump to

Keyboard shortcuts

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