gg is a library for rendering 2D graphics in pure Go.
go get -u github.com/FloatTech/gg
Alternatively, you may use gopkg.in to grab a specific major-version:
go get -u github.com/FloatTech/gg.v1
- godoc: https://godoc.org/github.com/FloatTech/gg
- pkg.go.dev: https://pkg.go.dev/github.com/FloatTech/gg?tab=doc
Look how easy!
package main
import"github.com/FloatTech/gg"funcmain() {
dc:=gg.NewContext(1000, 1000)
dc.DrawCircle(500, 500, 400)
dc.SetRGB(0, 0, 0)
dc.Fill()
dc.SavePNG("out.png")
}There are lots of examples included. They're mostly for testing the code, but they're good for learning, too.
There are a few ways of creating a context.
NewContext(width, heightint) *ContextNewContextForImage(imimage.Image) *ContextNewContextForRGBA(im*image.RGBA) *ContextEver used a graphics library that didn't have functions for drawing rectangles or circles? What a pain!
DrawPoint(x, y, rfloat64)
DrawLine(x1, y1, x2, y2float64)
DrawRectangle(x, y, w, hfloat64)
DrawRoundedRectangle(x, y, w, h, rfloat64)
DrawCircle(x, y, rfloat64)
DrawArc(x, y, r, angle1, angle2float64)
DrawEllipse(x, y, rx, ryfloat64)
DrawEllipticalArc(x, y, rx, ry, angle1, angle2float64)
DrawRegularPolygon(nint, x, y, r, rotationfloat64)
DrawImage(imimage.Image, x, yint)
DrawImageAnchored(imimage.Image, x, yint, ax, ayfloat64)
SetPixel(x, yint)
MoveTo(x, yfloat64)
LineTo(x, yfloat64)
QuadraticTo(x1, y1, x2, y2float64)
CubicTo(x1, y1, x2, y2, x3, y3float64)
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.
It will even do word wrap for you!
DrawString(sstring, x, yfloat64)
DrawStringAnchored(sstring, x, y, ax, ayfloat64)
DrawStringWrapped(sstring, x, y, ax, ay, width, lineSpacingfloat64, alignAlign)
MeasureString(sstring) (w, hfloat64)
MeasureMultilineString(sstring, lineSpacingfloat64) (w, hfloat64)
WordWrap(sstring, wfloat64) []stringSetFontFace(fontFacefont.Face)
LoadFontFace(pathstring, pointsfloat64) errorColors can be set in several different ways for your convenience.
SetRGB(r, g, bfloat64)
SetRGBA(r, g, b, afloat64)
SetRGB255(r, g, bint)
SetRGBA255(r, g, b, aint)
SetColor(ccolor.Color)
SetHexColor(xstring)SetLineWidth(lineWidthfloat64)
SetLineCap(lineCapLineCap)
SetLineJoin(lineJoinLineJoin)
SetDash(dashes...float64)
SetDashOffset(offsetfloat64)
SetFillRule(fillRuleFillRule)gg supports linear, radial and conic gradients and surface patterns. You can also implement your own patterns.
SetFillStyle(patternPattern)
SetStrokeStyle(patternPattern)
NewSolidPattern(colorcolor.Color)
NewLinearGradient(x0, y0, x1, y1float64)
NewRadialGradient(x0, y0, r0, x1, y1, r1float64)
NewConicGradient(cx, cy, degfloat64)
NewSurfacePattern(imimage.Image, opRepeatOp)Identity()
Translate(x, yfloat64)
Scale(x, yfloat64)
Rotate(anglefloat64)
Shear(x, yfloat64)
ScaleAbout(sx, sy, x, yfloat64)
RotateAbout(angle, x, yfloat64)
ShearAbout(sx, sy, x, yfloat64)
TransformPoint(x, yfloat64) (tx, tyfloat64)
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.
Save and restore the state of the context. These can be nested.
Push()
Pop()Use clipping regions to restrict drawing operations to an area that you defined using paths.
Clip()
ClipPreserve()
ResetClip()
AsMask() *image.AlphaSetMask(mask*image.Alpha)
InvertMask()Sometimes you just don't want to write these yourself.
Radians(degreesfloat64) float64Degrees(radiansfloat64) float64LoadImage(pathstring) (image.Image, error)
LoadPNG(pathstring) (image.Image, error)
SavePNG(pathstring, imimage.Image) errorSee the output of this example below.
package main
import"github.com/FloatTech/gg"funcmain() {
constS=1024dc:=gg.NewContext(S, S)
dc.SetRGBA(0, 0, 0, 0.1)
fori:=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")
}


