Involved Source Filesgeom.golen.gotee.go Package vg defines an interface for drawing 2D vector graphics.
This package is designed with the hope that many different
vector graphics back-ends can conform to the interface.
Code Examples
package main
import (
"archive/tar"
"bytes"
"compress/gzip"
"fmt"
"io"
"log"
"net/http"
"golang.org/x/image/font/opentype"
"gonum.org/v1/plot"
"gonum.org/v1/plot/font"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
)
func main() {
// download font from debian
const url = "http://http.debian.net/debian/pool/main/f/fonts-ipafont/fonts-ipafont_00303.orig.tar.gz"
resp, err := http.Get(url)
if err != nil {
log.Fatalf("could not download IPA font file: %+v", err)
}
defer resp.Body.Close()
ttf, err := untargz("IPAfont00303/ipam.ttf", resp.Body)
if err != nil {
log.Fatalf("could not untar archive: %+v", err)
}
fontTTF, err := opentype.Parse(ttf)
if err != nil {
log.Fatal(err)
}
mincho := font.Font{Typeface: "Mincho"}
font.DefaultCache.Add([]font.Face{
{
Font: mincho,
Face: fontTTF,
},
})
if !font.DefaultCache.Has(mincho) {
log.Fatalf("no font %q!", mincho.Typeface)
}
plot.DefaultFont = mincho
plotter.DefaultFont = mincho
p := plot.New()
p.Title.Text = "Hello, 世界!"
p.X.Label.Text = "世"
p.Y.Label.Text = "界"
labels, err := plotter.NewLabels(
plotter.XYLabels{
XYs: make(plotter.XYs, 1),
Labels: []string{"こんにちは世界"},
},
)
if err != nil {
log.Fatalf("could not create labels: %+v", err)
}
p.Add(labels)
p.Add(plotter.NewGrid())
err = p.Save(10*vg.Centimeter, 10*vg.Centimeter, "mincho-font.png")
if err != nil {
log.Fatalf("could not save plot: %+v", err)
}
}
func untargz(name string, r io.Reader) ([]byte, error) {
gr, err := gzip.NewReader(r)
if err != nil {
return nil, fmt.Errorf("could not create gzip reader: %v", err)
}
defer gr.Close()
tr := tar.NewReader(gr)
for {
hdr, err := tr.Next()
if err != nil {
if err == io.EOF {
return nil, fmt.Errorf("could not find %q in tar archive", name)
}
return nil, fmt.Errorf("could not extract header from tar archive: %v", err)
}
if hdr == nil || hdr.Name != name {
continue
}
buf := new(bytes.Buffer)
_, err = io.Copy(buf, tr)
if err != nil {
return nil, fmt.Errorf("could not extract %q file from tar archive: %v", name, err)
}
return buf.Bytes(), nil
}
}
package main
import (
"image/color"
"image/png"
"log"
"math"
"os"
"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/vgimg"
)
func main() {
p := plot.New()
p.Title.Text = "sin(x)"
p.X.Label.Text = "x"
p.Y.Label.Text = "f(x)"
p.X.Min = -2 * math.Pi
p.X.Max = +2 * math.Pi
fct := plotter.NewFunction(func(x float64) float64 {
return math.Sin(x)
})
fct.Color = color.RGBA{R: 255, A: 255}
p.Add(fct, plotter.NewGrid())
c := vgimg.New(10*vg.Centimeter, 5*vg.Centimeter)
p.Draw(draw.New(c))
// Save image.
f, err := os.Create("testdata/sine.png")
if err != nil {
log.Fatalf("could not create output image file: %+v", err)
}
defer f.Close()
err = png.Encode(f, c.Image())
if err != nil {
log.Fatalf("could not encode image to PNG: %+v", err)
}
err = f.Close()
if err != nil {
log.Fatalf("could not close output image file: %+v", err)
}
}
package main
import (
"image/color"
"log"
"math"
"os"
"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/vgimg"
)
func main() {
p := plot.New()
p.Title.Text = "cos(x)"
p.X.Label.Text = "x"
p.Y.Label.Text = "f(x)"
p.X.Min = -2 * math.Pi
p.X.Max = +2 * math.Pi
fct := plotter.NewFunction(func(x float64) float64 {
return math.Cos(x)
})
fct.Color = color.RGBA{B: 255, A: 255}
p.Add(fct, plotter.NewGrid())
c := vgimg.PngCanvas{
Canvas: vgimg.New(10*vg.Centimeter, 5*vg.Centimeter),
}
p.Draw(draw.New(c))
// Save image.
f, err := os.Create("testdata/cosine.png")
if err != nil {
log.Fatalf("could not create output image file: %+v", err)
}
defer f.Close()
_, err = c.WriteTo(f)
if err != nil {
log.Fatalf("could not encode image to PNG: %+v", err)
}
err = f.Close()
if err != nil {
log.Fatalf("could not close output image file: %+v", err)
}
}
Package-Level Type Names (total 8)
/* sort by: | */
A Canvas is the main drawing interface for 2D vector
graphics.
The origin is in the bottom left corner. DrawImage draws the image, scaled to fit
the destination rectangle. Fill fills the given path. FillString fills in text at the specified
location using the given font.
If the font size is zero, the text is not drawn. Pop restores the context saved by the
corresponding call to 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(). Rotate applies a rotation transform to the context.
The parameter is specified in radians. Scale applies a scaling transform to the
context. 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. 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. 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. Stroke strokes the given path. Translate applies a translational transform
to the context.CanvasSizer(interface)CanvasWriterTo(interface)
func MultiCanvas(cs ...Canvas) Canvas
func Initialize(c Canvas)
func MultiCanvas(cs ...Canvas) Canvas
CanvasSizer is a Canvas with a defined size. DrawImage draws the image, scaled to fit
the destination rectangle. Fill fills the given path. FillString fills in text at the specified
location using the given font.
If the font size is zero, the text is not drawn. Pop restores the context saved by the
corresponding call to 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(). Rotate applies a rotation transform to the context.
The parameter is specified in radians. Scale applies a scaling transform to the
context. 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. 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. 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.( CanvasSizer) Size() (x, y Length) Stroke strokes the given path. Translate applies a translational transform
to the context.CanvasWriterTo(interface)
CanvasSizer : Canvas
CanvasWriterTo is a CanvasSizer with a WriteTo method. DrawImage draws the image, scaled to fit
the destination rectangle. Fill fills the given path. FillString fills in text at the specified
location using the given font.
If the font size is zero, the text is not drawn. Pop restores the context saved by the
corresponding call to 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(). Rotate applies a rotation transform to the context.
The parameter is specified in radians. Scale applies a scaling transform to the
context. 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. 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. 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.( CanvasWriterTo) Size() (x, y Length) Stroke strokes the given path. Translate applies a translational transform
to the context.( CanvasWriterTo) WriteTo(w io.Writer) (n int64, err error)
CanvasWriterTo : Canvas
CanvasWriterTo : CanvasSizer
CanvasWriterTo : github.com/gobwas/ws.HandshakeHeader
CanvasWriterTo : io.WriterTo
A Length is a unit-independent representation of length.
Internally, the length is stored in postscript points.
Arc adds an arc to the path defined by the center
point of the arc's circle, the radius of the circle
and the start and sweep angles. Close closes the path by connecting the current
location to the start location with a line. CubeTo adds a cubic curve element to the path,
given by the control points p1 and p2 and the end point pt. Line draws a line from the current point to the
given point. Move moves the current location of the path to
the given point. QuadTo adds a quadratic curve element to the path,
given by the control point p1 and end point pt.
func Rectangle.Path() (p Path)
func Canvas.Fill(Path)
func Canvas.Stroke(Path)
func CanvasSizer.Fill(Path)
func CanvasSizer.Stroke(Path)
func CanvasWriterTo.Fill(Path)
func CanvasWriterTo.Stroke(Path)
A PathComp is a component of a path structure. Start and Angle are only used for ArcComps.
They define the start angle and sweep angle of
the arc around the circle. The units of the
angle are radians. Control is one or two intermediate points
for a CurveComp used by QuadTo and CubeTo. The Pos field is used as the destination
of a MoveComp or LineComp and is the center
point of an ArcComp. It is not used in
the CloseComp. Radius is only used for ArcComps, it is
the radius of the circle defining the arc. Start and Angle are only used for ArcComps.
They define the start angle and sweep angle of
the arc around the circle. The units of the
angle are radians. Type is the type of a particluar component.
Based on the type, each of the following
fields may have a different meaning or may
be meaningless.
A Rectangle represents a rectangular region of 2d space.MaxPointMinPoint Add returns the rectangle r translated by p. Path returns the path of a Rect specified by its
upper left corner, width and height. Size returns the width and height of a Rectangle.
func Rectangle.Add(p Point) Rectangle
func Canvas.DrawImage(rect Rectangle, img image.Image)
func CanvasSizer.DrawImage(rect Rectangle, img image.Image)
func CanvasWriterTo.DrawImage(rect Rectangle, img image.Image)
Package-Level Functions (total 4)
Initialize sets all of the canvas's values to their
initial values.
MultiCanvas creates a canvas that duplicates its drawing operations to all
the provided canvases, similar to the Unix tee(1) command.
Each drawing operation is sent to each listed canvas, one at a time.
ParseLength parses a Length string.
A Length string is a possible signed floating number with a unit.
e.g. "42cm" "2.4in" "66pt"
If no unit was given, ParseLength assumes it was (postscript) points.
Currently valid units are:
- mm (millimeter)
- cm (centimeter)
- in (inch)
- pt (point)
Points returns a length for the given number of points.
Package-Level Constants (total 8)
Constants that tag the type of each path
component.
Common lengths.
Constants that tag the type of each path
component.
Constants that tag the type of each path
component.
Common lengths.
Constants that tag the type of each path
component.
Common lengths.
Constants that tag the type of each path
component.
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.