-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathanimation.go
131 lines (111 loc) · 3.6 KB
/
animation.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package fyne
import "time"
// AnimationCurve represents an animation algorithm for calculating the progress through a timeline.
// Custom animations can be provided by implementing the "func(float32) float32" definition.
// The input parameter will start at 0.0 when an animation starts and travel up to 1.0 at which point it will end.
// A linear animation would return the same output value as is passed in.
type AnimationCurve func(float32) float32
// AnimationRepeatForever is an AnimationCount value that indicates it should not stop looping.
//
// Since: 2.0
const AnimationRepeatForever = -1
var (
// AnimationEaseInOut is the default easing, it starts slowly, accelerates to the middle and slows to the end.
//
// Since: 2.0
AnimationEaseInOut = animationEaseInOut
// AnimationEaseIn starts slowly and accelerates to the end.
//
// Since: 2.0
AnimationEaseIn = animationEaseIn
// AnimationEaseOut starts at speed and slows to the end.
//
// Since: 2.0
AnimationEaseOut = animationEaseOut
// AnimationLinear is a linear mapping for animations that progress uniformly through their duration.
//
// Since: 2.0
AnimationLinear = animationLinear
)
// Animation represents an animated element within a Fyne canvas.
// These animations may control individual objects or entire scenes.
//
// Since: 2.0
type Animation struct {
AutoReverse bool
Curve AnimationCurve
Duration time.Duration
RepeatCount int
Tick func(float32)
}
// NewAnimation creates a very basic animation where the callback function will be called for every
// rendered frame between [time.Now] and the specified duration. The callback values start at 0.0 and
// will be 1.0 when the animation completes.
//
// Since: 2.0
func NewAnimation(d time.Duration, fn func(float32)) *Animation {
return &Animation{Duration: d, Tick: fn}
}
// Start registers the animation with the application run-loop and starts its execution.
func (a *Animation) Start() {
CurrentApp().Driver().StartAnimation(a)
}
// Stop will end this animation and remove it from the run-loop.
func (a *Animation) Stop() {
CurrentApp().Driver().StopAnimation(a)
}
// InfiniteAnimation represents an animation that continues infinitely unless stopped. It has no duration
// or curve, and when started, the Tick function will be called every frame until Stop is invoked.
//
// Since: 2.6
type InfiniteAnimation struct {
Tick func()
isSetup bool
animation Animation
}
// NewInfiniteAnimation creates an infinite animation where the callback function will be called
// for every rendered frame once started, until stopped.
//
// Since: 2.6
func NewInfiniteAnimation(fn func()) *InfiniteAnimation {
return &InfiniteAnimation{Tick: fn}
}
// Start registers the animation with the application run-loop and starts its execution.
func (i *InfiniteAnimation) Start() {
i.setupAnimation()
i.animation.Start()
}
// Stop will end this animation and remove it from the run-loop.
func (i *InfiniteAnimation) Stop() {
i.setupAnimation()
i.animation.Stop()
}
func (i *InfiniteAnimation) setupAnimation() {
if i.isSetup {
return
}
i.animation = Animation{
Tick: func(_ float32) {
i.Tick()
},
Curve: AnimationLinear, // any curve will work
Duration: 1 * time.Second, // anything positive here will work
RepeatCount: AnimationRepeatForever,
}
i.isSetup = true
}
func animationEaseIn(val float32) float32 {
return val * val
}
func animationEaseInOut(val float32) float32 {
if val <= 0.5 {
return val * val * 2
}
return -1 + (4-val*2)*val
}
func animationEaseOut(val float32) float32 {
return val * (2 - val)
}
func animationLinear(val float32) float32 {
return val
}