/
giocanvas.go
39 lines (35 loc) · 1009 Bytes
/
giocanvas.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Package giocanvas is a 2D canvas API built on gio
package giocanvas
import (
"image/color"
"gioui.org/app"
"gioui.org/font/gofont"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/text"
"gioui.org/widget/material"
)
// Canvas defines the Gio canvas
type Canvas struct {
Width, Height float32
Theme *material.Theme
TextColor color.NRGBA
Context layout.Context
}
// NewCanvas initializes a Canvas
func NewCanvas(width, height float32, e app.FrameEvent) *Canvas {
canvas := new(Canvas)
canvas.Width = width
canvas.Height = height
canvas.TextColor = color.NRGBA{0, 0, 0, 255}
theme := material.NewTheme()
theme.Shaper = text.NewShaper(text.NoSystemFonts(), text.WithCollection(gofont.Regular()))
canvas.Theme = theme
canvas.Context = app.NewContext(new(op.Ops), e)
iw, ih := int(width), int(height)
canvas.Context.Constraints.Min.X = iw
canvas.Context.Constraints.Min.Y = ih
canvas.Context.Constraints.Max.X = iw
canvas.Context.Constraints.Max.Y = ih
return canvas
}