-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene.go
59 lines (46 loc) · 1.26 KB
/
scene.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
53
54
55
56
57
58
59
package sinebobs
import (
"context"
"math"
"github.com/lucasepe/doodlekit"
)
func Scene() doodlekit.Scene {
return &scene{}
}
type scene struct {
w, h int
time float64
cols, rows int
spacingX, spacingY int
offsetX, offsetY int
}
func (s *scene) Init(ctx context.Context) {
gc := doodlekit.Canvas(ctx)
s.w, s.h = gc.Width(), gc.Height()
// Number of spheres in each direction
s.cols, s.rows = 8, 6
// Small margin to avoid cutting spheres at the edges
colsMargin, rowsMargin := 4, 10
// Spacing between spheres
s.spacingX = (s.w - 2*colsMargin) / (s.cols - 1)
s.spacingY = (s.h - 2*rowsMargin) / (s.rows - 1)
s.offsetX = (s.w - (s.cols-1)*s.spacingX) / 2
s.offsetY = (s.h - (s.rows-1)*s.spacingY) / 2
}
func (s *scene) Update(ctx context.Context, dt float64) {
s.time += 4 * dt
}
func (s *scene) Draw(ctx context.Context) {
gc := doodlekit.Canvas(ctx)
for l := 0.0; l < float64(s.cols); l++ {
x := int(l*float64(s.spacingX)) + s.offsetX
for n := 0.0; n < float64(s.rows); n++ {
y := int(n*float64(s.spacingY)) + s.offsetY
p := math.Sin(s.time + l/2 - n/1.5*math.Sin(s.time+n/8))
gc.Color(3 + int(s.time))
gc.CircFill(x, y, int(p+2))
gc.Color(9 + int(s.time))
gc.CircFill(x, y, int(p+1))
}
}
}