2016-02-19 21:03:56 +01:00
# Go Graphics
`gg` is a library for rendering 2D graphics in pure Go.
2016-02-23 03:40:53 +01:00
![Stars ](http://i.imgur.com/CylQIJt.png )
2016-02-20 21:41:59 +01:00
2016-02-19 21:03:56 +01:00
## Installation
2017-04-14 19:57:42 +02:00
go get -u github.com/fogleman/gg
Alternatively, you may use gopkg.in to grab a specific major-version:
go get -u gopkg.in/fogleman/gg.v1
2016-02-19 21:03:56 +01:00
2017-05-04 03:14:43 +02:00
## Documentation
2016-02-23 22:39:09 +01:00
https://godoc.org/github.com/fogleman/gg
2016-02-19 21:03:56 +01:00
## Hello, Circle!
2016-02-19 22:49:48 +01:00
Look how easy!
2016-02-19 21:03:56 +01:00
```go
package main
import "github.com/fogleman/gg"
func main() {
dc := gg.NewContext(1000, 1000)
dc.DrawCircle(500, 500, 400)
dc.SetRGB(0, 0, 0)
2016-02-20 02:46:19 +01:00
dc.Fill()
2016-02-19 21:03:56 +01:00
dc.SavePNG("out.png")
}
```
2016-02-25 03:55:37 +01:00
## Examples
There are [lots of examples ](https://github.com/fogleman/gg/tree/master/examples ) included. They're mostly for testing the code, but they're good for learning, too.
![Examples ](http://i.imgur.com/tMFoyzu.png )
2016-02-19 22:55:58 +01:00
## Creating Contexts
There are a few ways of creating a context.
```go
NewContext(width, height int) *Context
NewContextForImage(im image.Image) *Context
NewContextForRGBA(im *image.RGBA) *Context
```
2016-02-19 21:03:56 +01:00
## Drawing Functions
Ever used a graphics library that didn't have functions for drawing rectangles
or circles? What a pain!
```go
2016-12-14 20:51:04 +01:00
DrawPoint(x, y, r float64)
2016-02-19 21:03:56 +01:00
DrawLine(x1, y1, x2, y2 float64)
DrawRectangle(x, y, w, h float64)
2016-02-20 03:15:22 +01:00
DrawRoundedRectangle(x, y, w, h, r float64)
2016-02-19 21:03:56 +01:00
DrawCircle(x, y, r float64)
2016-02-19 22:53:13 +01:00
DrawArc(x, y, r, angle1, angle2 float64)
DrawEllipse(x, y, rx, ry float64)
DrawEllipticalArc(x, y, rx, ry, angle1, angle2 float64)
2016-02-25 18:47:20 +01:00
DrawRegularPolygon(n int, x, y, r, rotation float64)
2016-02-19 21:03:56 +01:00
DrawImage(im image.Image, x, y int)
2016-02-20 03:35:56 +01:00
DrawImageAnchored(im image.Image, x, y int, ax, ay float64)
2016-12-14 20:51:04 +01:00
SetPixel(x, y int)
2016-02-19 21:03:56 +01:00
MoveTo(x, y float64)
LineTo(x, y float64)
QuadraticTo(x1, y1, x2, y2 float64)
2016-02-21 05:11:34 +01:00
CubicTo(x1, y1, x2, y2, x3, y3 float64)
2016-02-19 21:03:56 +01:00
ClosePath()
ClearPath()
2016-02-22 04:22:17 +01:00
NewSubPath()
2016-02-19 21:03:56 +01:00
Clear()
Stroke()
Fill()
StrokePreserve()
FillPreserve()
```
2016-02-21 23:20:33 +01:00
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.
2016-02-20 04:51:46 +01:00
## Text Functions
2016-02-25 03:36:44 +01:00
It will even do word wrap for you!
2016-02-20 04:51:46 +01:00
```go
DrawString(s string, x, y float64)
DrawStringAnchored(s string, x, y, ax, ay float64)
2016-02-25 03:30:09 +01:00
DrawStringWrapped(s string, x, y, ax, ay, width, lineSpacing float64, align Align)
2016-02-20 04:51:46 +01:00
MeasureString(s string) (w, h float64)
2016-02-25 03:30:09 +01:00
WordWrap(s string, w float64) []string
2016-02-20 04:51:46 +01:00
SetFontFace(fontFace font.Face)
2016-03-05 20:46:55 +01:00
LoadFontFace(path string, points float64) error
2016-02-20 04:51:46 +01:00
```
2016-02-19 21:03:56 +01:00
## Color Functions
Colors can be set in several different ways for your convenience.
```go
2016-02-19 22:53:13 +01:00
SetRGB(r, g, b float64)
SetRGBA(r, g, b, a float64)
SetRGB255(r, g, b int)
SetRGBA255(r, g, b, a int)
2016-02-19 21:03:56 +01:00
SetColor(c color.Color)
SetHexColor(x string)
```
2016-02-19 23:12:17 +01:00
## Stroke & Fill Options
2016-02-19 21:03:56 +01:00
2016-02-19 23:12:17 +01:00
```go
SetLineWidth(lineWidth float64)
SetLineCap(lineCap LineCap)
SetLineJoin(lineJoin LineJoin)
2016-02-23 03:00:39 +01:00
SetDash(dashes ...float64)
2016-02-19 23:12:17 +01:00
SetFillRule(fillRule FillRule)
```
2016-12-14 20:51:04 +01:00
## Gradients & Patterns
`gg` supports linear and radial gradients and surface patterns. You can also implement your own patterns.
```go
SetFillStyle(pattern Pattern)
SetStrokeStyle(pattern Pattern)
NewSolidPattern(color color.Color)
NewLinearGradient(x0, y0, x1, y1 float64)
NewRadialGradient(x0, y0, r0, x1, y1, r1 float64)
NewSurfacePattern(im image.Image, op RepeatOp)
```
2016-02-19 23:12:17 +01:00
## Transformation Functions
2016-02-19 22:49:48 +01:00
2016-02-19 21:03:56 +01:00
```go
Identity()
Translate(x, y float64)
Scale(x, y float64)
Rotate(angle float64)
Shear(x, y float64)
2016-02-20 20:53:33 +01:00
ScaleAbout(sx, sy, x, y float64)
RotateAbout(angle, x, y float64)
ShearAbout(sx, sy, x, y float64)
2016-02-19 21:03:56 +01:00
TransformPoint(x, y float64) (tx, ty float64)
2016-02-20 03:00:56 +01:00
InvertY()
2016-02-21 21:45:28 +01:00
```
2016-02-21 23:20:33 +01:00
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.
2016-02-21 21:45:28 +01:00
## Stack Functions
Save and restore the state of the context. These can be nested.
```go
2016-02-19 21:03:56 +01:00
Push()
Pop()
```
2016-02-19 22:53:13 +01:00
2016-02-25 20:16:48 +01:00
## Clipping Functions
Use clipping regions to restrict drawing operations to an area that you
defined using paths.
```go
Clip()
ClipPreserve()
ResetClip()
```
2016-02-19 22:55:58 +01:00
## Helper Functions
Sometimes you just don't want to write these yourself.
```go
Radians(degrees float64) float64
Degrees(radians float64) float64
2016-02-25 21:06:04 +01:00
LoadImage(path string) (image.Image, error)
2016-02-19 22:55:58 +01:00
LoadPNG(path string) (image.Image, error)
SavePNG(path string, im image.Image) error
```
2016-02-23 03:40:53 +01:00
![Separator ](http://i.imgur.com/fsUvnPB.png )
2016-02-19 23:05:21 +01:00
## Another Example
See the output of this example below.
```go
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 ](http://i.imgur.com/J9CBZef.png )