fogleman/gg

GitHub: fogleman/gg

一个纯 Go 实现的 2D 图形渲染库,提供简洁的 API 用于绘制形状、文本、渐变并导出图片。

Stars: 4789 | Forks: 383

# Go Graphics `gg` 是一个用于在纯 Go 中渲染 2D 图形的库。 ## 安装 ``` go get -u github.com/fogleman/gg ``` 或者,你可以使用 gopkg.in 获取特定的主版本: ``` go get -u gopkg.in/fogleman/gg.v1 ``` ## 文档 - godoc: https://godoc.org/github.com/fogleman/gg - pkg.go.dev: https://pkg.go.dev/github.com/fogleman/gg?tab=doc ## 你好,Circle! 看看有多简单! ``` 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") } ``` ## 示例 这里包含[大量示例](https://github.com/fogleman/gg/tree/master/examples)。它们主要用于测试代码,但也很适合用来学习。 ![示例](http://i.imgur.com/tMFoyzu.png) ## 创建 Context 有几种方法可以创建 context。 ``` NewContext(width, height int) *Context NewContextForImage(im image.Image) *Context NewContextForRGBA(im *image.RGBA) *Context ``` ## 绘制函数 你用过没有绘制矩形或圆形函数的图形库吗?真是太痛苦了! ``` DrawPoint(x, y, r float64) 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) DrawRegularPolygon(n int, x, y, r, rotation float64) DrawImage(im image.Image, x, y int) DrawImageAnchored(im image.Image, x, y int, ax, ay float64) SetPixel(x, y int) 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() ``` 通常需要将图像在某个点居中。使用 `DrawImageAnchored` 并将 `ax` 和 `ay` 设置为 0.5 即可实现。设置为 0 表示左对齐或顶部对齐。设置为 1 表示右对齐或底部对齐。`DrawStringAnchored` 对文本也做同样的事情,因此你不需要自己调用 `MeasureString`。 ## 文本函数 它甚至会为你实现自动换行! ``` 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) MeasureMultilineString(s string, lineSpacing float64) (w, h float64) WordWrap(s string, w float64) []string SetFontFace(fontFace font.Face) LoadFontFace(path string, points float64) error ``` ## 颜色函数 为方便起见,可以通过几种不同的方式设置颜色。 ``` 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 选项 ``` SetLineWidth(lineWidth float64) SetLineCap(lineCap LineCap) SetLineJoin(lineJoin LineJoin) SetDash(dashes ...float64) SetDashOffset(offset float64) SetFillRule(fillRule FillRule) ``` ## 渐变和图案 `gg` 支持 linear、radial 和 conic 渐变以及 surface patterns。你还可以实现自己的 patterns。 ``` SetFillStyle(pattern Pattern) SetStrokeStyle(pattern Pattern) NewSolidPattern(color color.Color) NewLinearGradient(x0, y0, x1, y1 float64) NewRadialGradient(x0, y0, r0, x1, y1, r1 float64) NewConicGradient(cx, cy, deg float64) NewSurfacePattern(im image.Image, op RepeatOp) ``` ## 变换函数 ``` 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() ``` 通常需要围绕非原点进行旋转或缩放。为此提供了便捷函数 `RotateAbout`、`ScaleAbout`、`ShearAbout`。 提供 `InvertY` 是为了应对需要 Y 轴从底部向顶部递增的情况(而不是默认的从顶部向底部递增)。 ## 栈函数 保存和恢复 context 的状态。这些操作可以嵌套。 ``` Push() Pop() ``` ## 裁剪函数 使用裁剪区域将绘制操作限制在你使用路径定义的区域内。 ``` Clip() ClipPreserve() ResetClip() AsMask() *image.Alpha SetMask(mask *image.Alpha) InvertMask() ``` ## 辅助函数 有时候你只是不想自己写这些。 ``` Radians(degrees float64) float64 Degrees(radians float64) float64 LoadImage(path string) (image.Image, error) LoadPNG(path string) (image.Image, error) SavePNG(path string, im image.Image) error ``` ![Separator](http://i.imgur.com/fsUvnPB.png) ## 另一个示例 请看下面这个示例的输出。 ``` 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)
标签:2D图形渲染, EVTX分析, Go, Ruby工具, SOC Prime, 图像处理, 开发工具, 日志审计, 绘图库