Enterprise-Grade 2D Graphics Library for Go
Pure Go | GPU Accelerated | Rust-Inspired Architecture
Go 1.25+ Required
gg is a professional 2D graphics library for Go, designed to power IDEs, browsers, and graphics-intensive applications. Built with modern Rust-inspired patterns from vello and tiny-skia, it delivers enterprise-grade rendering with zero CGO dependencies.
| Category | Capabilities |
|---|---|
| Rendering | Immediate & retained mode, GPU acceleration via WebGPU, CPU fallback |
| Shapes | Rectangles, circles, ellipses, arcs, bezier curves, polygons, stars |
| Text | TrueType fonts, MSDF rendering, emoji/COLRv1, bidirectional text, CJK |
| Compositing | 29 blend modes (Porter-Duff + Advanced + HSL), layer isolation |
| Images | 7 pixel formats, PNG/JPEG I/O, mipmaps, affine transforms |
| Performance | Sparse strips GPU, tile-based parallel rendering, LRU caching |
gg (Public API)
│
┌───────────────────┼───────────────────┐
│ │ │
Immediate Mode Retained Mode Resources
(Context API) (Scene Graph) (Images, Fonts)
│ │ │
└───────────────────┼───────────────────┘
│
RenderBackend Interface
│
┌────────────────┼────────────────┐
│ │
Software GPU
(CPU SIMD) (gogpu/wgpu WebGPU)
The main drawing type is named Context following industry standards:
| Library | Drawing Type |
|---|---|
| HTML5 Canvas API | CanvasRenderingContext2D |
| Cairo | cairo_t (context) |
| Apple CoreGraphics | CGContext |
| piet (Rust) | RenderContext |
| fogleman/gg | Context |
In HTML5, the Canvas is just the HTML element (surface), while the Context is what performs all drawing operations (canvas.getContext("2d")). This semantic distinction applies here: Context contains drawing state (colors, transforms, fonts) and provides the drawing API.
Variable naming convention:
dc := gg.NewContext(512, 512) // dc = drawing contextThis convention (dc for drawing, ctx for context.Context) avoids confusion with Go's standard library and is used throughout fogleman/gg, Cairo, and CoreGraphics codebases.
go get github.com/gogpu/ggpackage main
import (
"github.com/gogpu/gg"
"github.com/gogpu/gg/text"
)
func main() {
// Create a 512x512 drawing context
dc := gg.NewContext(512, 512)
defer dc.Close() // Clean resource release
dc.ClearWithColor(gg.White)
// Draw a gradient circle
dc.SetHexColor("#3498db")
dc.DrawCircle(256, 256, 100)
dc.Fill()
// Text rendering with font fallback
source, _ := text.NewFontSourceFromFile("arial.ttf")
defer source.Close()
dc.SetFont(source.Face(32))
dc.SetColor(gg.Black)
dc.DrawString("Hello, GoGPU!", 180, 260)
dc.SavePNG("output.png")
}Classic canvas-style drawing with transformation stack:
dc := gg.NewContext(800, 600)
defer dc.Close()
// Shapes with transforms
dc.Push()
dc.Translate(400, 300)
dc.Rotate(math.Pi / 4)
dc.DrawRectangle(-50, -50, 100, 100)
dc.SetRGB(0.2, 0.5, 0.8)
dc.Fill()
dc.Pop()
// Bezier paths
dc.MoveTo(100, 100)
dc.QuadraticTo(200, 50, 300, 100)
dc.CubicTo(350, 150, 350, 250, 300, 300)
dc.SetLineWidth(3)
dc.Stroke()Type-safe path construction with method chaining:
path := gg.BuildPath().
MoveTo(100, 100).
LineTo(200, 100).
QuadTo(250, 150, 200, 200).
CubicTo(150, 250, 100, 250, 50, 200).
Close().
Circle(300, 150, 50).
Star(400, 150, 40, 20, 5).
Build()
dc.SetPath(path)
dc.Fill()GPU-optimized scene graph for complex applications:
scene := gg.NewScene()
// Build scene with layers
scene.PushLayer(gg.BlendMultiply, 0.8)
scene.Fill(style, transform, gg.Solid(gg.Red), gg.Circle(150, 200, 100))
scene.Fill(style, transform, gg.Solid(gg.Blue), gg.Circle(250, 200, 100))
scene.PopLayer()
// Render to pixmap
renderer := scene.NewRenderer()
renderer.Render(target, scene)Full Unicode support with bidirectional text:
// Font composition with fallback
mainFont, _ := text.NewFontSourceFromFile("Roboto.ttf")
emojiFont, _ := text.NewFontSourceFromFile("NotoEmoji.ttf")
defer mainFont.Close()
defer emojiFont.Close()
multiFace, _ := text.NewMultiFace(
mainFont.Face(24),
text.NewFilteredFace(emojiFont.Face(24), text.RangeEmoji),
)
dc.SetFont(multiFace)
dc.DrawString("Hello World! Nice day!", 50, 100)
// Layout with wrapping
opts := text.LayoutOptions{
MaxWidth: 400,
WrapMode: text.WrapWordChar,
Alignment: text.AlignCenter,
}
layout := text.LayoutText("Long text...", face, 16, opts)Sophisticated compositing with alpha masks:
// Create mask from current drawing
dc.DrawCircle(200, 200, 100)
dc.Fill()
mask := dc.AsMask()
// Apply mask to new context
dc2 := gg.NewContext(400, 400)
dc2.SetMask(mask)
dc2.DrawRectangle(0, 0, 400, 400)
dc2.Fill() // Only visible through mask
// Mask operations
dc2.InvertMask()
dc2.ClearMask()29 blend modes with isolated layers:
dc.PushLayer(gg.BlendOverlay, 0.7)
dc.SetRGB(1, 0, 0)
dc.DrawCircle(150, 200, 100)
dc.Fill()
dc.SetRGB(0, 0, 1)
dc.DrawCircle(250, 200, 100)
dc.Fill()
dc.PopLayer() // Composite with overlay blendgg is part of the GoGPU ecosystem — Pure Go GPU computing libraries.
| Project | Description | Purpose |
|---|---|---|
| gogpu/gogpu | Graphics framework | GPU abstraction, windowing, input |
| gogpu/wgpu | Pure Go WebGPU | Vulkan, Metal, GLES, Software backends |
| gogpu/naga | Shader compiler | WGSL → SPIR-V, MSL, GLSL |
| gogpu/gg | 2D graphics (this repo) | Canvas API, scene graph, GPU text |
| gogpu/ui | GUI toolkit | Widgets, layouts, themes (planned) |
Note: Always use the latest versions. Check each repository for current releases.
| Operation | Time | Notes |
|---|---|---|
| sRGB → Linear | 0.16ns | 260x faster than math.Pow |
| LayerCache.Get | 90ns | Thread-safe LRU |
| DirtyRegion.Mark | 10.9ns | Lock-free atomic |
| MSDF lookup | <10ns | Zero-allocation |
| Path iteration | 438ns | iter.Seq, 0 allocs |
- ROADMAP.md — Development roadmap and version history
- CHANGELOG.md — Detailed release notes
- CONTRIBUTING.md — Contribution guidelines
- pkg.go.dev — API reference
- GoGPU: From Idea to 100K Lines in Two Weeks
- Pure Go 2D Graphics Library with GPU Acceleration
- GoGPU Announcement
We welcome contributions! See CONTRIBUTING.md for guidelines.
Priority areas:
- API feedback and testing
- Examples and documentation
- Performance benchmarks
- Cross-platform testing
MIT License — see LICENSE for details.