v2 / examples / gg / bezier_anim.v
66 lines · 54 sloc · 1.14 KB · bbb61ab3687afe512a1fa12492c876d011626107
Raw
1module main
2
3import gg
4
5const rate = f32(1) / 60 * 10
6
7struct App {
8mut:
9 gg &gg.Context = unsafe { nil }
10 anim &Anim = unsafe { nil }
11}
12
13struct Anim {
14mut:
15 time f32
16 reverse bool
17}
18
19fn (mut anim Anim) advance() {
20 if anim.reverse {
21 anim.time -= 1 * rate
22 } else {
23 anim.time += 1 * rate
24 }
25 // Use some arbitrary value that fits 60 fps
26 if anim.time > 80 * rate || anim.time < -80 * rate {
27 anim.reverse = !anim.reverse
28 }
29}
30
31fn main() {
32 mut app := &App{
33 anim: &Anim{}
34 }
35 app.gg = gg.new_context(
36 bg_color: gg.rgb(174, 198, 255)
37 width: 600
38 height: 400
39 window_title: 'Animated cubic Bézier curve'
40 frame_fn: frame
41 user_data: app
42 )
43 app.gg.run()
44}
45
46fn frame(mut app App) {
47 time := app.anim.time
48
49 p1_x := f32(200.0)
50 p1_y := f32(200.0) + (10 * time)
51
52 p2_x := f32(400.0)
53 p2_y := f32(200.0) + (10 * time)
54
55 ctrl_p1_x := f32(200.0) + (40 * time)
56 ctrl_p1_y := f32(100.0)
57 ctrl_p2_x := f32(400.0) + (-40 * time)
58 ctrl_p2_y := f32(100.0)
59
60 points := [p1_x, p1_y, ctrl_p1_x, ctrl_p1_y, ctrl_p2_x, ctrl_p2_y, p2_x, p2_y]
61
62 app.gg.begin()
63 app.gg.draw_cubic_bezier(points, gg.blue)
64 app.gg.end()
65 app.anim.advance()
66}
67