fork of github.com/fogleman/gg with some bug fixes
Go to file
Michael Fogleman 6e542bac45 update readme
2016-02-24 21:30:09 -05:00
examples revert changes to lorem example 2016-02-24 21:28:04 -05:00
.gitignore ignore *.png 2016-02-18 22:13:05 -05:00
bezier.go QuadraticBezier 2016-02-21 16:20:17 -05:00
context.go word wrap implemented 2016-02-24 21:26:51 -05:00
LICENSE.md add LICENSE 2016-02-18 22:13:36 -05:00
matrix.go *About functions in Context only 2016-02-21 15:54:54 -05:00
path.go update constant 2016-02-23 23:00:23 -05:00
point.go fi -> fix, fp -> fixp 2016-02-22 22:33:22 -05:00
README.md update readme 2016-02-24 21:30:09 -05:00
util.go move path functions to path.go 2016-02-22 22:37:52 -05:00
wrap.go word wrap implemented 2016-02-24 21:26:51 -05:00

Go Graphics

gg is a library for rendering 2D graphics in pure Go.

Stars

Installation

go get github.com/fogleman/gg

GoDoc

https://godoc.org/github.com/fogleman/gg

Hello, Circle!

Look how easy!

package main

import "github.com/fogleman/gg"

func main() {
    dc := gg.NewContext(1000, 1000)
    dc.DrawCircle(500, 500, 400)
    dc.SetRGB(0, 0, 0)
    dc.Fill()
    dc.SavePNG("out.png")
}

Creating Contexts

There are a few ways of creating a context.

NewContext(width, height int) *Context
NewContextForImage(im image.Image) *Context
NewContextForRGBA(im *image.RGBA) *Context

Drawing Functions

Ever used a graphics library that didn't have functions for drawing rectangles or circles? What a pain!

DrawLine(x1, y1, x2, y2 float64)
DrawRectangle(x, y, w, h float64)
DrawRoundedRectangle(x, y, w, h, r float64)
DrawCircle(x, y, r float64)
DrawArc(x, y, r, angle1, angle2 float64)
DrawEllipse(x, y, rx, ry float64)
DrawEllipticalArc(x, y, rx, ry, angle1, angle2 float64)
DrawImage(im image.Image, x, y int)
DrawImageAnchored(im image.Image, x, y int, ax, ay float64)

MoveTo(x, y float64)
LineTo(x, y float64)
QuadraticTo(x1, y1, x2, y2 float64)
CubicTo(x1, y1, x2, y2, x3, y3 float64)
ClosePath()
ClearPath()
NewSubPath()

Clear()
Stroke()
Fill()
StrokePreserve()
FillPreserve()

It is often desired to center an image at a point. Use DrawImageAnchored with ax and ay set to 0.5 to do this. Use 0 to left or top align. Use 1 to right or bottom align. DrawStringAnchored does the same for text, so you don't need to call MeasureString yourself.

Text Functions

DrawString(s string, x, y float64)
DrawStringAnchored(s string, x, y, ax, ay float64)
DrawStringWrapped(s string, x, y, ax, ay, width, lineSpacing float64, align Align)
MeasureString(s string) (w, h float64)
WordWrap(s string, w float64) []string
SetFontFace(fontFace font.Face)
LoadFontFace(path string, points float64)

Color Functions

Colors can be set in several different ways for your convenience.

SetRGB(r, g, b float64)
SetRGBA(r, g, b, a float64)
SetRGB255(r, g, b int)
SetRGBA255(r, g, b, a int)
SetColor(c color.Color)
SetHexColor(x string)

Stroke & Fill Options

SetLineWidth(lineWidth float64)
SetLineCap(lineCap LineCap)
SetLineJoin(lineJoin LineJoin)
SetDash(dashes ...float64)
SetFillRule(fillRule FillRule)

Transformation Functions

Identity()
Translate(x, y float64)
Scale(x, y float64)
Rotate(angle float64)
Shear(x, y float64)
ScaleAbout(sx, sy, x, y float64)
RotateAbout(angle, x, y float64)
ShearAbout(sx, sy, x, y float64)
TransformPoint(x, y float64) (tx, ty float64)
InvertY()

It is often desired to rotate or scale about a point that is not the origin. The functions RotateAbout, ScaleAbout, ShearAbout are provided as a convenience.

InvertY is provided in case Y should increase from bottom to top vs. the default top to bottom.

Note: transforms do not currently affect DrawImage or DrawString.

Stack Functions

Save and restore the state of the context. These can be nested.

Push()
Pop()

Helper Functions

Sometimes you just don't want to write these yourself.

Radians(degrees float64) float64
Degrees(radians float64) float64
LoadPNG(path string) (image.Image, error)
SavePNG(path string, im image.Image) error

Separator

What's Missing?

If you need any of the features below, I recommend using cairo instead. Or even better, implement it and submit a pull request!

  • Clipping Regions
  • Gradients / Patterns

How Do it Do?

gg is mostly a wrapper around github.com/golang/freetype/raster. The goal is to provide some more functionality and a nicer API that will suffice for most use cases.

Another Example

See the output of this example below.

package main

import "github.com/fogleman/gg"

func main() {
	const S = 1024
	dc := gg.NewContext(S, S)
	dc.SetRGBA(0, 0, 0, 0.1)
	for i := 0; i < 360; i += 15 {
		dc.Push()
		dc.RotateAbout(gg.Radians(float64(i)), S/2, S/2)
		dc.DrawEllipse(S/2, S/2, S*7/16, S/8)
		dc.Fill()
		dc.Pop()
	}
	dc.SavePNG("out.png")
}

Ellipses