Skip to content

gg

Dépôt officiel : fogleman/gg: Go Graphics - 2D rendering in Go with a simple API. (github.com)

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

Exemples officiels : gg/examples at master · fogleman/gg (github.com)

gg est un moteur de rendu graphique 2D assez ancien, adapté pour générer des images.

Installation

go get -u github.com/fogleman/gg

Démarrage rapide

go
package main

import "github.com/fogleman/gg"

func main() {
    dc := gg.NewContext(1000, 1000) // Créer un canevas de hauteur 1000 et largeur 1000
    dc.DrawCircle(500, 500, 400) // Dessiner un cercle de rayon 400 aux coordonnées (500,500)
    dc.SetRGB(0, 0, 0) // Définir la couleur noire
    dc.Fill() // Remplir
    dc.SavePNG("out.png") // Sauvegarder dans un fichier image
}

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) // Définir les coordonnées et le rayon du point
   }
   dc.Fill() // Remplir
   dc.SavePNG("out.png")
}

Lignes

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

go
func TestLines(t *testing.T) {
   dc := gg.NewContext(1000, 1000)
   dc.SetRGB(0, 0, 0)
   dc.SetLineWidth(5) // Définir l'épaisseur du trait
   dc.DrawLine(1000, 0, 0, 1000)
   dc.DrawLine(1000, 1000, 0, 0)
   dc.Stroke() // Tracer les lignes
   dc.SavePNG("lines.png")
}

Golang by www.golangdev.cn edit