2016-02-19 21:03:56 +01:00
|
|
|
# Go Graphics
|
|
|
|
|
|
|
|
`gg` is a library for rendering 2D graphics in pure Go.
|
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
|
|
|
go get github.com/fogleman/gg
|
|
|
|
|
|
|
|
## 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)
|
|
|
|
dc.SetLineWidth(10)
|
|
|
|
dc.Stroke()
|
|
|
|
dc.SavePNG("out.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
|
|
|
|
DrawLine(x1, y1, x2, y2 float64)
|
|
|
|
DrawRectangle(x, y, w, h float64)
|
|
|
|
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-19 21:03:56 +01:00
|
|
|
DrawImage(im image.Image, x, y int)
|
|
|
|
DrawString(x, y float64, s string)
|
|
|
|
|
|
|
|
MoveTo(x, y float64)
|
|
|
|
LineTo(x, y float64)
|
|
|
|
QuadraticTo(x1, y1, x2, y2 float64)
|
|
|
|
ClosePath()
|
|
|
|
ClearPath()
|
|
|
|
|
|
|
|
Clear()
|
|
|
|
Stroke()
|
|
|
|
Fill()
|
|
|
|
StrokePreserve()
|
|
|
|
FillPreserve()
|
|
|
|
```
|
|
|
|
|
|
|
|
## 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)
|
|
|
|
```
|
|
|
|
|
|
|
|
## Transformation Functions
|
|
|
|
|
2016-02-19 22:49:48 +01:00
|
|
|
All the usual matrix transformations are available too.
|
|
|
|
|
2016-02-19 21:03:56 +01:00
|
|
|
```go
|
|
|
|
Identity()
|
|
|
|
Translate(x, y float64)
|
|
|
|
Scale(x, y float64)
|
|
|
|
Rotate(angle float64)
|
|
|
|
RotateAbout(angle, x, y float64)
|
|
|
|
Shear(x, y float64)
|
|
|
|
TransformPoint(x, y float64) (tx, ty float64)
|
|
|
|
Push()
|
|
|
|
Pop()
|
|
|
|
```
|
2016-02-19 22:53:13 +01:00
|
|
|
|
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
|
|
|
|
LoadPNG(path string) (image.Image, error)
|
|
|
|
SavePNG(path string, im image.Image) error
|
|
|
|
```
|
|
|
|
|
2016-02-19 22:53:13 +01:00
|
|
|
## What's Missing?
|
|
|
|
|
|
|
|
If you need any of the features below, I recommend using `cairo` instead.
|
|
|
|
|
|
|
|
- Clipping Regions
|
|
|
|
- Gradients / Patterns
|
|
|
|
- Cubic Beziers
|
|
|
|
- Dashed Lines
|