gitmyhub

gg

Go ★ 4.8k updated 2y ago

Go Graphics - 2D rendering in Go with a simple API.

gg is a Go library for drawing 2D graphics and saving the results as image files. It is written entirely in Go, with no external graphics dependencies, and provides a straightforward API for drawing shapes, text, and images onto a canvas.

You create a drawing context by specifying a width and height in pixels. From there you can draw circles, rectangles, lines, arcs, ellipses, and polygons using simple function calls. Colors are set with RGB or hex values. Text can be drawn at any position with optional anchoring and word wrapping. You can load custom fonts by specifying a path to a font file.

The library also supports more advanced features. You can apply gradients: linear, radial, and conic. Transformations let you translate, scale, rotate, and shear the canvas, and each of these can be applied around an arbitrary point rather than just the origin. Clipping regions let you restrict drawing operations to a defined area. The Push and Pop functions save and restore the drawing state, making it easy to apply temporary transformations without affecting the rest of the image.

When you are done drawing, you save the result as a PNG file with a single function call.

The library is useful for generating images programmatically, such as creating charts, illustrations, visualizations, or automated thumbnails. The README includes short code examples that show how to draw a filled circle and how to draw overlapping ellipses using a rotation loop. A set of additional example programs is included in the repository for reference.