-
Notifications
You must be signed in to change notification settings - Fork 1
/
animatedcolor.go
52 lines (45 loc) · 1.01 KB
/
animatedcolor.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
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"image"
"image/color"
"math"
"time"
"gioui.org/app"
"gioui.org/io/system"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
)
func main() {
go func() {
w := app.NewWindow()
// START OMIT
ops := new(op.Ops) // HLops
for e := range w.Events() {
if e, ok := e.(system.FrameEvent); ok {
ops.Reset() // HLops
color := animateColor(e.Now)
square := image.Rectangle{
Min: image.Point{X: 50, Y: 50},
Max: image.Point{X: 500, Y: 500},
}
// Add draw operations.
paint.ColorOp{Color: color}.Add(ops) // HLops
clip.Rect(square).Add(ops) // HLops
paint.PaintOp{}.Add(ops) // HLops
// Request immediate redraw.
op.InvalidateOp{}.Add(ops) // HLops
// Submit operations.
e.Frame(ops) // HLops
}
}
// END OMIT
}()
app.Main()
}
var start = time.Now()
func animateColor(t time.Time) color.RGBA {
dt := t.Sub(start).Seconds()
green := math.Abs(math.Sin(dt))
return color.RGBA{A: 0xff, G: byte(green * 0xff)}
}