package main import ( "image" "image/color" "log" "os" "gioui.org/app" "gioui.org/f32" "gioui.org/io/system" "gioui.org/layout" "gioui.org/op" "gioui.org/op/clip" "gioui.org/op/paint" ) func main() { go func() { w := app.NewWindow() err := run(w) if err != nil { log.Fatal(err) } os.Exit(0) }() app.Main() } func run(w *app.Window) error { var ops op.Ops for { e := <-w.Events() switch e := e.(type) { case system.DestroyEvent: return e.Err case system.FrameEvent: gtx := layout.NewContext(&ops, e) // Using clip.Rect { stack := op.Affine(f32.Affine2D{}.Offset(f32.Pt(50.3, 50.3))).Push(&ops) r1 := clip.Rect{Max: image.Pt(50, 50)} paint.FillShape(&ops, color.NRGBA{0xFF, 0, 0, 0xFF}, r1.Op()) stack.Pop() } // Drawing our own rectangular path { stack := op.Affine(f32.Affine2D{}.Offset(f32.Pt(50.3, 150.3))).Push(&ops) r1 := FRect{Max: f32.Pt(50, 50)} paint.FillShape(&ops, color.NRGBA{0xFF, 0, 0, 0xFF}, r1.Op(&ops)) stack.Pop() } e.Frame(gtx.Ops) } } } type FRect struct { Min f32.Point Max f32.Point } func (r FRect) Path(ops *op.Ops) clip.PathSpec { var p clip.Path p.Begin(ops) p.MoveTo(r.Min) p.LineTo(f32.Pt(r.Max.X, r.Min.Y)) p.LineTo(r.Max) p.LineTo(f32.Pt(r.Min.X, r.Max.Y)) p.LineTo(r.Min) return p.End() } func (r FRect) Op(ops *op.Ops) clip.Op { return clip.Outline{Path: r.Path(ops)}.Op() }