API Reference

Complete reference for all AdvanceGG functions and types.

Context

The Context is the main drawing surface and entry point for all graphics operations.

Creating Contexts

Function Description
NewContext(width, height int) *Context Creates a new context with specified dimensions
NewContextForImage(img image.Image) *Context Creates a context from an existing image
NewContextForRGBA(img *image.RGBA) *Context Creates a context from an RGBA image

State Management

// Save and restore drawing state
dc.Push()    // Save current state
// ... drawing operations ...
dc.Pop()     // Restore saved state

// Clear the canvas
dc.Clear()   // Fill with current color
dc.SetRGB(1, 1, 1)
dc.Clear()   // White background

Drawing

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
dc.DrawLine(x1, y1, x2, y2)

// Polygons
dc.DrawRegularPolygon(sides, x, y, radius, rotation)

Fill and Stroke

// Fill shapes
dc.Fill()           // Fill and clear path
dc.FillPreserve()   // Fill but keep path

// Stroke outlines
dc.Stroke()         // Stroke and clear path
dc.StrokePreserve() // Stroke but keep path

// Combined operations
dc.FillPreserve()
dc.Stroke()         // Fill then stroke

Line Styles

// Line width
dc.SetLineWidth(5.0)

// Line cap styles
dc.SetLineCap(advancegg.LineCapRound)   // Round, Butt, Square

// Line join styles
dc.SetLineJoin(advancegg.LineJoinRound) // Round, Miter, Bevel

// Dash patterns
dc.SetDash(10, 5)                       // 10 on, 5 off
dc.SetDashOffset(2.5)                   // Offset pattern

Paths

Path Operations

// Start new path
dc.NewPath()

// Move without drawing
dc.MoveTo(x, y)

// Draw lines
dc.LineTo(x, y)

// Bézier curves
dc.QuadraticTo(cpx, cpy, x, y)          // Quadratic
dc.CubicTo(cp1x, cp1y, cp2x, cp2y, x, y) // Cubic

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

// Close path
dc.ClosePath()

Path2D Support

// Create Path2D object
path := advancegg.NewPath2D()
path.MoveTo(100, 100)
path.LineTo(200, 200)
path.Arc(150, 150, 50, 0, 2*math.Pi)

// Use with context
dc.DrawPath(path)
dc.Fill()

// Path operations
path.Transform(matrix)
bounds := path.GetBounds()
contains := path.Contains(x, y)

Text

Font Management

// Load fonts
dc.LoadFontFace("fonts/arial.ttf", 24)
dc.LoadFontFaceFromBytes(fontData, 24)

// Font properties
dc.SetFontSize(18)
dc.SetFontFace(fontFace)

Text Rendering

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

// Anchored text (alignment)
dc.DrawStringAnchored("Centered", x, y, 0.5, 0.5)
// ax, ay: 0=left/top, 0.5=center, 1=right/bottom

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

// Text measurement
w, h := dc.MeasureString("Text")
lines := dc.WordWrap("Text", width)

Advanced Text Features

// Text on path
advancegg.DrawTextOnCircle(dc, "Circular Text", centerX, centerY, radius)
advancegg.DrawTextOnWave(dc, "Wave Text", startX, startY, endX, amplitude, frequency)

// Unicode and emoji
dc.DrawString("Hello 世界 🌍", x, y)  // Automatic support

// Text effects
dc.SetTextShadow(offsetX, offsetY, blur, color)
dc.DrawString("Shadow Text", x, y)

Images

Loading and Saving

// Load images
img := advancegg.LoadPNG("input.png")
img := advancegg.LoadJPEG("input.jpg")
img := advancegg.LoadImage("input.gif") // Auto-detect format

// Save images
dc.SavePNG("output.png")
dc.SaveJPEG("output.jpg", 95)  // 95% quality
dc.SaveGIF("output.gif")

// Get image data
rgba := dc.Image()  // Get underlying RGBA image

Drawing Images

// Draw at position
dc.DrawImage(img, x, y)

// Draw with scaling
dc.DrawImageAnchored(img, x, y, ax, ay)

// Draw with transformation
dc.Push()
dc.Scale(2, 2)
dc.Rotate(math.Pi / 4)
dc.DrawImage(img, x, y)
dc.Pop()

Image Processing

// Apply filters
blurred := advancegg.ApplyBlur(img, 5.0)
sharpened := advancegg.ApplySharpen(img, 1.5)
grayscale := advancegg.ApplyGrayscale(img)

// Brightness and contrast
bright := advancegg.ApplyBrightness(img, 1.2)
contrast := advancegg.ApplyContrast(img, 1.5)

// Color adjustments
saturated := advancegg.ApplySaturation(img, 1.3)
hueShifted := advancegg.ApplyHueRotate(img, 30) // degrees

Colors

Setting Colors

// RGB colors
dc.SetRGB(1, 0, 0)        // Red (values 0-1)
dc.SetRGBA(1, 0, 0, 0.5)  // Semi-transparent red
dc.SetRGB255(255, 0, 0)   // RGB with 0-255 values

// Hex colors
dc.SetHexColor("#FF5733")
dc.SetHexColor("#F53")    // Short form

// Color objects
dc.SetColor(color.RGBA{255, 0, 0, 255})

Color Spaces

// CMYK (for print)
dc.SetCMYK(0, 1, 1, 0)    // Cyan=0, Magenta=100%, Yellow=100%, Black=0

// HSV (Hue, Saturation, Value)
dc.SetHSV(240, 1, 1)      // Blue: H=240°, S=100%, V=100%

// HSL (Hue, Saturation, Lightness)
dc.SetHSL(240, 1, 0.5)    // Blue: H=240°, S=100%, L=50%

// LAB color space
dc.SetLAB(50, 20, -30)    // L=50, a=20, b=-30

Gradients

// Linear gradient
gradient := advancegg.NewLinearGradient(0, 0, 100, 0)
gradient.AddColorStop(0, color.RGBA{255, 0, 0, 255})    // Red at start
gradient.AddColorStop(1, color.RGBA{0, 0, 255, 255})    // Blue at end
dc.SetFillStyle(gradient)

// Radial gradient
radial := advancegg.NewRadialGradient(50, 50, 0, 50, 50, 50)
radial.AddColorStop(0, color.RGBA{255, 255, 255, 255})  // White center
radial.AddColorStop(1, color.RGBA{0, 0, 0, 255})        // Black edge
dc.SetFillStyle(radial)

Transformations

// Translation
dc.Translate(dx, dy)

// Scaling
dc.Scale(sx, sy)          // Different X and Y scaling
dc.Scale(s, s)            // Uniform scaling

// Rotation
dc.Rotate(angle)          // Angle in radians
dc.RotateAbout(angle, x, y) // Rotate about point

// Shearing
dc.Shear(sx, sy)

// Matrix transformations
matrix := advancegg.Identity()
matrix = matrix.Scale(2, 2)
matrix = matrix.Rotate(math.Pi / 4)
dc.SetMatrix(matrix)

// Reset transformation
dc.Identity()

Layer System

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

// Add layers
background := layerManager.AddLayer("bg")
foreground := layerManager.AddLayer("fg")

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

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

// Layer properties
foreground.SetOpacity(0.8)
foreground.SetBlendMode(advancegg.BlendModeMultiply)
foreground.SetVisible(true)

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

Image Filters

Basic Filters
  • ApplyBlur(img, radius)
  • ApplySharpen(img, amount)
  • ApplyGrayscale(img)
  • ApplySepia(img)
  • ApplyInvert(img)
Advanced Filters
  • ApplyEdgeDetection(img)
  • ApplyEmboss(img)
  • ApplyPixelate(img, size)
  • ApplyVignette(img, strength)
  • ApplyNoise(img, amount)

Advanced Strokes

// Dashed strokes
dc.DrawDashedLine(x1, y1, x2, y2, []float64{10, 5, 2, 5})

// Gradient strokes
stops := []advancegg.StrokeGradientStop{
    {Position: 0.0, Color: color.RGBA{255, 0, 0, 255}},
    {Position: 1.0, Color: color.RGBA{0, 0, 255, 255}},
}
dc.DrawGradientLine(x1, y1, x2, y2, stops)

// Tapered strokes
dc.DrawTaperedLine(x1, y1, x2, y2, startWidth, endWidth)

Performance

// Enable performance optimizations
dc.SetSIMDEnabled(true)        // Use SIMD instructions
dc.SetMemoryPooling(true)      // Enable memory pooling
dc.SetBatchMode(true)          // Batch operations

// Debug and profiling
dc.SetDebugMode(true)          // Visual debugging
dc.EnableProfiling(true)       // Performance profiling
stats := dc.GetPerformanceStats()

// Caching
dc.EnableCaching(true)         // Cache rendered elements
dc.ClearCache()                // Clear cache manually

Quick Reference

Common Patterns
// Basic setup
dc := advancegg.NewContext(800, 600)
dc.SetRGB(1, 1, 1)
dc.Clear()

// Draw with style
dc.SetRGB(1, 0, 0)
dc.SetLineWidth(3)
dc.DrawCircle(400, 300, 100)
dc.Stroke()

// Save result
dc.SavePNG("output.png")
Common Gotchas
  • • Remember to call Fill() or Stroke()
  • • Use Push()/Pop() for state management
  • • Color values are 0-1, not 0-255
  • • Angles are in radians, not degrees
  • • Y axis increases downward