Getting Started with AdvanceGG

Learn how to create stunning 2D graphics with AdvanceGG in just a few minutes.

Installation

AdvanceGG requires Go 1.18 or later. Install it using Go modules:

go get github.com/GrandpaEJ/advancegg
Note: AdvanceGG has no external dependencies beyond the Go standard library and a few well-maintained packages for font and image handling.

Quick Start

Create your first graphic in just a few lines of code:

package main

import "github.com/GrandpaEJ/advancegg"

func main() {
    // Create a new 800x600 canvas
    dc := advancegg.NewContext(800, 600)
    
    // Set background color to dark blue
    dc.SetRGB(0.1, 0.1, 0.3)
    dc.Clear()
    
    // Draw a red circle
    dc.SetRGB(1, 0, 0)
    dc.DrawCircle(400, 300, 100)
    dc.Fill()
    
    // Add white text
    dc.SetRGB(1, 1, 1)
    dc.DrawString("Hello AdvanceGG!", 300, 350)
    
    // Save as PNG
    dc.SavePNG("hello.png")
}

Run this program and you'll get a beautiful PNG image with a red circle and text!

Basic Concepts

Context

The Context is the main drawing surface. Think of it as a digital canvas where you create your graphics:

// Create a 1920x1080 canvas
dc := advancegg.NewContext(1920, 1080)

// Or create from an existing image
img := image.NewRGBA(image.Rect(0, 0, 800, 600))
dc := advancegg.NewContextForRGBA(img)

Coordinate System

AdvanceGG uses a standard 2D coordinate system:

  • Origin (0, 0) is at the top-left corner
  • X increases to the right
  • Y increases downward
  • All coordinates are in pixels (float64)

Drawing Operations

AdvanceGG follows a path-based drawing model:

  1. Set Style: Choose colors, line width, fonts, etc.
  2. Create Path: Define shapes using move, line, curve operations
  3. Render: Fill or stroke the path to make it visible
// 1. Set style
dc.SetRGB(0, 0.5, 1)
dc.SetLineWidth(5)

// 2. Create path
dc.MoveTo(100, 100)
dc.LineTo(200, 150)
dc.LineTo(150, 200)
dc.ClosePath()

// 3. Render
dc.Stroke() // or dc.Fill() or dc.FillPreserve() + dc.Stroke()

Your First Program

Let's create a more complex example that demonstrates multiple features:

package main

import (
    "math"
    "github.com/GrandpaEJ/advancegg"
)

func main() {
    // Create canvas
    dc := advancegg.NewContext(800, 600)
    
    // Gradient background
    for y := 0; y < 600; y++ {
        t := float64(y) / 600.0
        dc.SetRGB(0.1+t*0.2, 0.1+t*0.3, 0.3+t*0.4)
        dc.DrawLine(0, float64(y), 800, float64(y))
        dc.Stroke()
    }
    
    // Draw multiple circles with transparency
    for i := 0; i < 10; i++ {
        angle := float64(i) * 2 * math.Pi / 10
        x := 400 + 150*math.Cos(angle)
        y := 300 + 150*math.Sin(angle)
        
        // Set color with transparency
        dc.SetRGBA(1, float64(i)/10, 0.5, 0.7)
        dc.DrawCircle(x, y, 30)
        dc.Fill()
    }
    
    // Add title with shadow effect
    dc.SetRGB(0, 0, 0)
    dc.DrawString("AdvanceGG Graphics", 252, 102) // Shadow
    dc.SetRGB(1, 1, 1)
    dc.DrawString("AdvanceGG Graphics", 250, 100) // Main text
    
    // Save the result
    dc.SavePNG("first-program.png")
}

Drawing Features

Basic Shapes

// Rectangles
dc.DrawRectangle(x, y, width, height)
dc.DrawRoundedRectangle(x, y, width, height, radius)

// Circles and ellipses
dc.DrawCircle(x, y, radius)
dc.DrawEllipse(x, y, rx, ry)

// Lines and polygons
dc.DrawLine(x1, y1, x2, y2)
dc.DrawRegularPolygon(6, x, y, radius, rotation)

Advanced Paths

// Bézier curves
dc.MoveTo(100, 100)
dc.QuadraticTo(200, 50, 300, 100)  // Quadratic
dc.CubicTo(400, 50, 500, 150, 600, 100)  // Cubic

// Arcs
dc.DrawArc(x, y, radius, startAngle, endAngle)

Text Rendering

// Load custom font
dc.LoadFontFace("fonts/arial.ttf", 24)

// Basic text
dc.DrawString("Hello World", x, y)
dc.DrawStringAnchored("Centered", x, y, 0.5, 0.5)

// Word wrapping
dc.DrawStringWrapped("Long text...", x, y, ax, ay, width, lineSpacing, align)

// Text on path
advancegg.DrawTextOnCircle(dc, "Circular Text", centerX, centerY, radius)

Image Processing

// Load and draw images
img := advancegg.LoadPNG("input.png")
dc.DrawImage(img, x, y)

// Apply filters
filtered := advancegg.ApplyBlur(img, 5.0)
filtered = advancegg.ApplyBrightness(filtered, 1.2)
dc.DrawImage(filtered, x, y)

// Save in different formats
dc.SavePNG("output.png")
dc.SaveJPEG("output.jpg", 95) // 95% quality

Color Management

// RGB colors
dc.SetRGB(1, 0, 0)        // Red
dc.SetRGBA(1, 0, 0, 0.5)  // Semi-transparent red
dc.SetHexColor("#FF5733") // Hex color

// Other color spaces
dc.SetCMYK(0, 1, 1, 0)    // CMYK
dc.SetHSV(240, 1, 1)      // HSV (blue)
dc.SetLAB(50, 20, -30)    // LAB color space

// Gradients
gradient := advancegg.NewLinearGradient(0, 0, 100, 0)
gradient.AddColorStop(0, color.RGBA{255, 0, 0, 255})
gradient.AddColorStop(1, color.RGBA{0, 0, 255, 255})
dc.SetFillStyle(gradient)

Layer System

// Create layer manager
layerManager := advancegg.NewLayerManager(800, 600)

// Add layers
bgLayer := layerManager.AddLayer("background")
fgLayer := layerManager.AddLayer("foreground")

// Draw on specific layers
bgLayer.SetRGB(0.2, 0.2, 0.4)
bgLayer.Clear()

fgLayer.SetRGB(1, 1, 0)
fgLayer.DrawCircle(400, 300, 100)
fgLayer.Fill()

// Set blend modes
fgLayer.SetBlendMode(advancegg.BlendModeMultiply)
fgLayer.SetOpacity(0.8)

// Composite final image
result := layerManager.Flatten()
result.SavePNG("layered.png")

Next Steps

API Reference

Explore the complete API documentation with detailed examples.

View API Docs
Examples

See real-world examples and learn advanced techniques.

Browse Examples
Pro Tips
  • Use dc.Push() and dc.Pop() to save and restore drawing state
  • Enable debug mode with dc.SetDebugMode(true) for development
  • Use dc.MeasureString() to calculate text dimensions before drawing
  • Batch similar operations together for better performance
  • Use layers for complex compositions with blend modes