A Skia-style immediate-mode drawing API for Gio, providing GPU-accelerated 2D graphics rendering.
- Skia-style API: Familiar drawing interface inspired by Skia graphics library
- GPU-accelerated: All rendering is hardware-accelerated via Gio's renderer
- Path-based drawing: Build complex shapes using paths with Bézier curves
- Transformations: Full support for translate, scale, rotate, and matrix concatenation
- State management: Save/Restore stack for managing drawing state
- Advanced strokes: Configurable stroke width, caps, joins, miter limits, and dash patterns
- Fill and stroke modes: Switch between filled and stroked rendering
go get github.com/zodimo/gio-skiapackage main
import (
"image/color"
"gioui.org/app"
"gioui.org/op"
"gioui.org/op/paint"
"github.com/zodimo/gio-skia/skia"
)
func main() {
go func() {
w := app.NewWindow()
var ops op.Ops
for {
switch e := w.Event().(type) {
case app.FrameEvent:
ops.Reset()
// White background
paint.ColorOp{Color: color.NRGBA{R: 255, G: 255, B: 255, A: 255}}.Add(&ops)
paint.PaintOp{}.Add(&ops)
// Create canvas and draw
c := skia.NewCanvas(&ops)
p := skia.NewPath()
skia.PathAddCircle(p, 100, 100, 50)
skPaint := skia.NewPaintFill(color.NRGBA{R: 255, A: 255})
c.DrawPath(p, skPaint)
e.Frame(&ops)
}
}
}()
app.Main()
}The Canvas interface provides the main drawing context:
type Canvas interface {
// State management
Save() int // Push matrix onto stack, returns save count
Restore() // Pop state; restores matrix
Concat(matrix SkMatrix) // Multiply current transform matrix
Translate(dx, dy Scalar) // Translate by dx, dy
Scale(sx, sy Scalar) // Scale by sx, sy
Rotate(degrees Scalar) // Rotate by degrees (clockwise)
// Convenience methods (accept float32)
TranslateFloat32(x, y float32)
ScaleFloat32(x, y float32)
RotateFloat32(degrees float32)
// Drawing
DrawPath(path SkPath, paint SkPaint) // Render path with paint & transform
}Build complex shapes using SkPath and helper functions:
p := skia.NewPath() // Create new path
skia.PathMoveTo(p, x, y) // Move to point
skia.PathLineTo(p, x, y) // Draw line
skia.PathQuadTo(p, cx, cy, x, y) // Quadratic Bézier curve
skia.PathCubeTo(p, cx1, cy1, cx2, cy2, x, y) // Cubic Bézier curve
p.Close() // Close current contour
skia.PathAddRect(p, x, y, w, h) // Add rectangle
skia.PathAddCircle(p, cx, cy, r) // Add circleCreate and configure paint for drawing operations:
// Create paint with default settings
paint := skia.NewPaint()
// Create paint with color (defaults to fill style)
paint := skia.NewPaintWithColor(color.NRGBA{R: 255, G: 0, B: 0, A: 255})
// Create paint configured for filling
paint := skia.NewPaintFill(color.NRGBA{R: 255, G: 0, B: 0, A: 255})
// Create paint configured for stroking
paint := skia.NewPaintStroke(color.NRGBA{R: 255, G: 0, B: 0, A: 255}, 5.0)
// Configure stroke options
import "github.com/zodimo/gio-skia/pkg/stroke"
strokeOpts := stroke.StrokeOpts{
Width: 5.0, // Stroke width in pixels
Miter: 4.0, // Miter limit
Cap: stroke.RoundCap, // Cap style
Join: stroke.RoundJoin, // Join style
}
paint = skia.ConfigureStrokePaint(paint, strokeOpts)Paint Styles:
skia.PaintStyleFill- Fill mode (default)skia.PaintStyleStroke- Stroke modeskia.PaintStyleStrokeAndFill- Both fill and stroke
Cap Styles:
stroke.RoundCap- Round capsstroke.SquareCap- Square capsstroke.FlatCap- Flat caps (default)
Join Styles:
stroke.RoundJoin- Round joinsstroke.MiterJoin- Miter joins (default)stroke.BevelJoin- Bevel joins
See the examples/gpu/ directory for comprehensive examples:
basic_shapes/- Basic shapes and primitivestransformations/- Transformations and state managementstrokes/- Stroke styles and dash patternsbezier_curves/- Bézier curves and complex pathsanimated/- Animated graphics example
The library provides convenient type aliases for better developer experience:
skia.Scalar- Floating-point value for coordinates, dimensions, and anglesskia.SkPath- Path interface for building shapesskia.SkPaint- Paint interface for configuring drawing propertiesskia.SkMatrix- Transformation matrix interface
These allow you to use Skia-style types without importing the underlying packages.
SPDX-License-Identifier: Unlicense OR MIT