gg

repository·master·Indexed 26 days ago

https://github.com/fogleman/gg

A pure Go library for rendering 2D graphics. It provides a high-level API for drawing shapes, text, and images, as well as support for transformations, gradients, clipping regions, and word wrapping.

Tokens
1.4K
Snippets
2
Records
11
Agent score
39%

What's inside gg

  1. Install the gg library

    master

    You can install gg using go get. To get the latest version:

    go get -u github.com/fogleman/gg

    Alternatively, use gopkg.in to target a specific major version:

    go get -u gopkg.in/fogleman/gg.v1
    go get -u github.com/fogleman/gg
  2. Helper functions for math and I/O

    master

    Convenience functions for common tasks:

    • 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
  3. Use clipping regions

    master

    Clipping restricts drawing operations to a specific area defined by a path.

    • Clip(): Clips to the current path.
    • ClipPreserve(): Clips to the current path but keeps the path in the context.
    • ResetClip(): Resets the clipping region.
    • AsMask() *image.Alpha
    • SetMask(mask *image.Alpha)
    • InvertMask()
  4. Apply transformations

    master

    Transformations allow you to manipulate the coordinate system. You can also use 'About' variants to transform relative to a specific point instead of the origin.

    Standard Transformations:

    • Identity(): Resets the transformation matrix.
    • Translate(x, y float64)
    • Scale(x, y float64)
    • Rotate(angle float64)
    • Shear(x, y float64)

    Transformations about a point:

    • ScaleAbout(sx, sy, x, y float64)
    • RotateAbout(angle, x, y float64)
    • ShearAbout(sx, sy, x, y float64)

    Other:

    • TransformPoint(x, y float64) (tx, ty float64): Applies current transformations to a point.
    • InvertY(): Inverts the Y axis (useful if you want Y to increase from bottom to top).
  5. Use gradients and patterns

    master

    You can apply gradients or surface patterns as fill or stroke styles using SetFillStyle(pattern Pattern) or SetStrokeStyle(pattern Pattern).

    Pattern Types:

    • 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)
  6. Draw basic shapes and paths

    master

    The library provides functions for drawing common geometric shapes and managing paths.

    Shapes:

    • 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)

    Path Manipulation:

    • 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()

    Rendering:

    • Clear(): Clears the context.
    • Stroke(): Renders the current path with the current stroke style.
    • Fill(): Renders the current path with the current fill style.
    • StrokePreserve(): Renders the path and keeps it in the context.
    • FillPreserve(): Renders the path and keeps it in the context.
  7. Create a graphics context

    master

    A context (*Context) is required to perform drawing operations. You can create one from scratch with dimensions, or wrap an existing image.

    • NewContext(width, height int) *Context: Creates a new blank context with specified dimensions.
    • NewContextForImage(im image.Image) *Context: Creates a context from an existing image.Image.
    • NewContextForRGBA(im *image.RGBA) *Context: Creates a context from an existing *image.RGBA.
  8. Draw and anchor images

    master

    You can draw images onto the context using absolute coordinates or anchor points.

    • DrawImage(im image.Image, x, y int): Draws an image at the specified top-left coordinates.
    • DrawImageAnchored(im image.Image, x, y int, ax, ay float64): Draws an image using an anchor point.
      • ax=0, ay=0: Top-left alignment.
      • ax=0.5, ay=0.5: Center alignment.
      • ax=1, ay=1: Bottom-right alignment.
  9. Set colors and styles

    master

    Colors can be set using RGB, RGBA, hex strings, or standard color.Color objects.

    Color Methods:

    • 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 Options:

    • SetLineWidth(lineWidth float64)
    • SetLineCap(lineCap LineCap)
    • SetLineJoin(lineJoin LineJoin)
    • SetDash(dashes ...float64)
    • SetDashOffset(offset float64)
    • SetFillRule(fillRule FillRule)
  10. Render text and handle word wrap

    master

    The library supports font loading, text measurement, and automatic word wrapping.

    Text Drawing:

    • DrawString(s string, x, y float64): Draws a string at x, y.
    • DrawStringAnchored(s string, x, y, ax, ay float64): Draws a string with an anchor point (e.g., 0.5, 0.5 for center).
    • DrawStringWrapped(s string, x, y, ax, ay, width, lineSpacing float64, align Align): Draws text with automatic word wrapping.

    Measurement and Setup:

    • MeasureString(s string) (w, h float64): Returns the width and height of a string.
    • MeasureMultilineString(s string, lineSpacing float64) (w, h float64): Returns dimensions for multiline text.
    • WordWrap(s string, w float64) []string: Splits a string into lines based on a width.
    • SetFontFace(fontFace font.Face): Sets the current font face.
    • LoadFontFace(path string, points float64) error: Loads a font from a file path.
  11. Manage context state with Push and Pop

    master

    Use Push() and Pop() to save and restore the state of the context (such as transformations, colors, and clipping). These calls can be nested to create complex drawing sequences.

    dc.Push()
    dc.Rotate(0.5)
    dc.DrawCircle(10, 10, 5)
    dc.Fill()
    dc.Pop()