package main import ( "image/color" "log" "os" "strings" "gioui.org/app" "gioui.org/f32" "gioui.org/io/system" "gioui.org/layout" "gioui.org/op" "gioui.org/op/clip" "gioui.org/op/paint" "gioui.org/unit" ) func main() { go func() { w := app.NewWindow(app.Size(unit.Dp(200), unit.Dp(200))) if err := loop(w); err != nil { log.Fatal(err) } os.Exit(0) }() app.Main() } func loop(w *app.Window) error { var ops op.Ops // load the desired font goregular, err := opentype.Parse(goregular.TTF) if err != nil { return err } for e := range w.Events() { switch e := e.(type) { case system.DestroyEvent: return e.Err case system.FrameEvent: gtx := layout.NewContext(&ops, e) // draw black background paint.Fill(&ops, color.NRGBA{A: 255}) // draw red and white checkboard DrawCheckBoard(&ops, f32.Pt(10, 10), f32.Pt(150, 150), 10, color.NRGBA{R: 255, G: 255, B: 255, A: 255}, color.NRGBA{R: 255, A: 255}) // render the drawing e.Frame(gtx.Ops) } } return nil } func DrawCheckBoard(ops *op.Ops, pos, size f32.Point, sqSize float32, clr1, clr2 color.NRGBA) { var path clip.Path // offset origin at pos defer op.Offset(pos).Push(ops).Pop() // set the global clipping region and paint with clr1 path.Begin(ops) path.LineTo(f32.Pt(size.X, 0)) path.LineTo(size) path.LineTo(f32.Pt(0, size.Y)) path.Close() defer clip.Outline{Path: path.End()}.Op().Push(ops).Pop() paint.Fill(ops, clr1) // create the square path path.Begin(ops) path.LineTo(f32.Pt(sqSize, 0)) path.LineTo(f32.Pt(sqSize, sqSize)) path.LineTo(f32.Pt(0, sqSize)) path.Close() square := clip.Outline{Path: path.End()}.Op() // create macro painting a square with clr2 drawSquareRec := op.Record(ops) s := square.Push(ops) paint.Fill(ops, clr2) s.Pop() drawSquare := drawSquareRec.Stop() // create a macro painting a row of squares drawSquareRowRec := op.Record(ops) for x := float32(0); x < size.X+sqSize; x += 2 * sqSize { s := op.Offset(f32.Pt(x, 0)).Push(ops) drawSquare.Add(ops) paint.Fill(ops, clr2) s.Pop() } drawSquareRow := drawSquareRowRec.Stop() // draw all even rows for y := float32(0); y < size.Y+sqSize; y += 2 * sqSize { s := op.Offset(f32.Pt(0, y)).Push(ops) drawSquareRow.Add(ops) s.Pop() } // draw all odd rows for y := sqSize; y < size.Y+sqSize; y += 2 * sqSize { s := op.Offset(f32.Pt(sqSize, y)).Push(ops) drawSquareRow.Add(ops) s.Pop() } }