Skip to content

gg

Official Repository: fogleman/gg: Go Graphics - 2D rendering in Go with a simple API. (github.com)

Official Documentation: gg package - github.com/fogleman/gg - Go Packages

Official Examples: gg/examples at master · fogleman/gg (github.com)

gg is a well-established 2D graphics rendering engine, suitable for generating images.

Installation

go get -u github.com/fogleman/gg

Quick Start

go
package main

import "github.com/fogleman/gg"

func main() {
    dc := gg.NewContext(1000, 1000) // Create canvas, height 1000, width 1000
    dc.DrawCircle(500, 500, 400) // Draw a circle with radius 400 at coordinates (500,500)
    dc.SetRGB(0, 0, 0) // Set color to black
    dc.Fill() // Fill
    dc.SavePNG("out.png") // Save to image file
}

Points

![](/images/gg/points.png =400x400)

go
func TestDot(t *testing.T) {
   dc := gg.NewContext(1000, 1000)
   dc.SetRGB(0, 0, 0)
   for i := 1; i < 10; i++ {
      dc.DrawPoint(float64(50*i), float64(50*i), 5) // Set point coordinates and radius
   }
   dc.Fill() // Fill
   dc.SavePNG("out.png")
}

Lines

![](/images/gg/lines.png =400x400)

go
func TestLines(t *testing.T) {
   dc := gg.NewContext(1000, 1000)
   dc.SetRGB(0, 0, 0)
   dc.SetLineWidth(5) // Set line width
   dc.DrawLine(1000, 0, 0, 1000)
   dc.DrawLine(1000, 1000, 0, 0)
   dc.Stroke() // Draw lines
   dc.SavePNG("lines.png")
}

Golang by www.golangdev.cn edit