Drawing API

Complete reference for drawing shapes, lines, and basic graphics operations.

Basic Shapes

Rectangles

// Draw rectangle
dc.DrawRectangle(x, y, width, height)

// Draw rounded rectangle
dc.DrawRoundedRectangle(x, y, width, height, radius)

// Example
dc.SetRGB(0.2, 0.4, 0.8)
dc.DrawRectangle(100, 100, 200, 150)
dc.Fill()

dc.SetRGB(0.8, 0.2, 0.4)
dc.DrawRoundedRectangle(350, 100, 200, 150, 20)
dc.Fill()

Circles and Ellipses

// Draw circle
dc.DrawCircle(centerX, centerY, radius)

// Draw ellipse
dc.DrawEllipse(centerX, centerY, radiusX, radiusY)

// Example
dc.SetRGB(1, 0.5, 0)
dc.DrawCircle(200, 300, 80)
dc.Fill()

dc.SetRGB(0.5, 1, 0.2)
dc.DrawEllipse(500, 300, 120, 60)
dc.Fill()

Lines

// Draw line
dc.DrawLine(x1, y1, x2, y2)

// Example with styling
dc.SetRGB(0, 0, 0)
dc.SetLineWidth(3)
dc.DrawLine(50, 50, 750, 550)
dc.Stroke()

Polygons

// Draw regular polygon
dc.DrawRegularPolygon(sides, centerX, centerY, radius, rotation)

// Examples
dc.SetRGB(0.8, 0.2, 0.8)
dc.DrawRegularPolygon(6, 200, 200, 80, 0) // Hexagon
dc.Fill()

dc.SetRGB(0.2, 0.8, 0.8)
dc.DrawRegularPolygon(5, 500, 200, 80, math.Pi/10) // Pentagon
dc.Fill()

Fill and Stroke Operations

Basic Operations

// Fill - fills the current path and clears it
dc.Fill()

// Stroke - strokes the current path and clears it
dc.Stroke()

// FillPreserve - fills but keeps the path
dc.FillPreserve()

// StrokePreserve - strokes but keeps the path
dc.StrokePreserve()

// Combined fill and stroke
dc.SetRGB(1, 0, 0)
dc.DrawCircle(200, 200, 50)
dc.FillPreserve()

dc.SetRGB(0, 0, 0)
dc.SetLineWidth(3)
dc.Stroke()

Fill Rules

// Set fill rule for complex paths
dc.SetFillRule(advancegg.FillRuleWinding)    // Default
dc.SetFillRule(advancegg.FillRuleEvenOdd)    // Alternative

Line Styles

Line Width

// Set line width
dc.SetLineWidth(width)

// Examples
dc.SetLineWidth(1)   // Thin line
dc.SetLineWidth(5)   // Medium line
dc.SetLineWidth(10)  // Thick line

Line Caps

// Line cap styles
dc.SetLineCap(advancegg.LineCapButt)    // Square end
dc.SetLineCap(advancegg.LineCapRound)   // Rounded end
dc.SetLineCap(advancegg.LineCapSquare)  // Extended square end

// Example
dc.SetLineWidth(20)
dc.SetLineCap(advancegg.LineCapRound)
dc.DrawLine(100, 100, 300, 100)
dc.Stroke()

Line Joins

// Line join styles
dc.SetLineJoin(advancegg.LineJoinMiter)  // Sharp corner
dc.SetLineJoin(advancegg.LineJoinRound)  // Rounded corner
dc.SetLineJoin(advancegg.LineJoinBevel)  // Beveled corner

// Miter limit for sharp corners
dc.SetMiterLimit(10.0)

// Example
dc.SetLineWidth(15)
dc.SetLineJoin(advancegg.LineJoinRound)
dc.MoveTo(100, 200)
dc.LineTo(200, 100)
dc.LineTo(300, 200)
dc.Stroke()

Dash Patterns

// Set dash pattern
dc.SetDash(dashLength, gapLength)

// Complex dash patterns
dc.SetDashPattern([]float64{10, 5, 2, 5}) // dash, gap, dash, gap

// Set dash offset
dc.SetDashOffset(5.0)

// Clear dash pattern
dc.ClearDash()

// Examples
dc.SetDash(10, 5)  // 10 pixels on, 5 pixels off
dc.DrawLine(100, 300, 500, 300)
dc.Stroke()

dc.SetDashPattern([]float64{20, 5, 5, 5})
dc.DrawLine(100, 350, 500, 350)
dc.Stroke()

Advanced Drawing

Clipping

// Set clipping region
dc.DrawCircle(400, 300, 100)
dc.Clip()

// All subsequent drawing will be clipped to this circle
dc.SetRGB(1, 0, 0)
dc.DrawRectangle(350, 250, 100, 100)
dc.Fill()

// Reset clipping
dc.ResetClip()

Masks

// Create mask from current path
dc.DrawCircle(200, 200, 80)
mask := dc.AsMask()

// Apply mask to drawing operations
dc.SetMask(mask)
dc.SetRGB(0, 1, 0)
dc.DrawRectangle(150, 150, 100, 100)
dc.Fill()

// Clear mask
dc.ClearMask()

Composite Operations

// Set composite operation
dc.SetCompositeOperation(advancegg.CompositeOperationSourceOver)  // Default
dc.SetCompositeOperation(advancegg.CompositeOperationMultiply)
dc.SetCompositeOperation(advancegg.CompositeOperationScreen)
dc.SetCompositeOperation(advancegg.CompositeOperationOverlay)

Complete Examples

Basic Shapes Gallery

package main

import "github.com/GrandpaEJ/advancegg"

func main() {
    dc := advancegg.NewContext(800, 600)
    
    // Background
    dc.SetRGB(0.95, 0.95, 0.98)
    dc.Clear()
    
    // Rectangle
    dc.SetRGB(0.2, 0.4, 0.8)
    dc.DrawRectangle(50, 50, 150, 100)
    dc.Fill()
    
    // Rounded rectangle
    dc.SetRGB(0.8, 0.2, 0.4)
    dc.DrawRoundedRectangle(250, 50, 150, 100, 20)
    dc.Fill()
    
    // Circle
    dc.SetRGB(0.2, 0.8, 0.2)
    dc.DrawCircle(125, 250, 60)
    dc.Fill()
    
    // Ellipse
    dc.SetRGB(0.8, 0.5, 0.2)
    dc.DrawEllipse(325, 250, 80, 50)
    dc.Fill()
    
    // Polygon
    dc.SetRGB(0.6, 0.2, 0.8)
    dc.DrawRegularPolygon(6, 125, 400, 60, 0)
    dc.Fill()
    
    // Star (custom polygon)
    dc.SetRGB(1, 0.8, 0.2)
    drawStar(dc, 325, 400, 60, 30, 5)
    dc.Fill()
    
    dc.SavePNG("shapes-gallery.png")
}

func drawStar(dc *advancegg.Context, centerX, centerY, outerRadius, innerRadius float64, points int) {
    angle := 2 * math.Pi / float64(points*2)
    
    dc.MoveTo(centerX+outerRadius, centerY)
    
    for i := 1; i < points*2; i++ {
        radius := outerRadius
        if i%2 == 1 {
            radius = innerRadius
        }
        
        x := centerX + radius*math.Cos(float64(i)*angle)
        y := centerY + radius*math.Sin(float64(i)*angle)
        dc.LineTo(x, y)
    }
    
    dc.ClosePath()
}

Quick Reference

Function Description Parameters
DrawRectangle Draw a rectangle x, y, width, height
DrawRoundedRectangle Draw a rounded rectangle x, y, width, height, radius
DrawCircle Draw a circle centerX, centerY, radius
DrawEllipse Draw an ellipse centerX, centerY, radiusX, radiusY
DrawLine Draw a line x1, y1, x2, y2
DrawRegularPolygon Draw a regular polygon sides, centerX, centerY, radius, rotation
Fill Fill current path none
Stroke Stroke current path none
SetLineWidth Set line width width
SetLineCap Set line cap style cap style