-
Notifications
You must be signed in to change notification settings - Fork 3
/
easing.go
58 lines (50 loc) · 1.15 KB
/
easing.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
package main
import (
"time"
"gioui.org/layout"
)
// Easing
type Easing struct {
initTime time.Time
duration time.Duration
delta time.Duration
}
// Progress calculates the time passed from the first invocation of the time.Now function.
func (e *Easing) Progress() float64 {
return float64(e.delta) / float64(e.duration)
}
// Update updates the time passed from the initial invocation of the time.Now
// function until the time set as duration is not reached.
func (e *Easing) Update(gtx layout.Context, isActive bool) float64 {
delta := gtx.Now.Sub(e.initTime)
e.initTime = gtx.Now
if isActive {
if e.delta < e.duration {
e.delta += delta
if e.delta > e.duration {
e.delta = e.duration
}
}
} else {
if e.delta > 0 {
e.delta -= delta
if e.delta < 0 {
e.delta = 0
}
}
}
return e.Progress()
}
// Animate applies the In-Out-Back easing formula to a specific event.
func (e *Easing) Animate(t float64) float64 {
s := 1.70158
t *= 2
if t < 1 {
s *= 1.525
return 0.5 * (t * t * ((s+1)*t - s))
} else {
t -= 2
s *= 1.525
return 0.5 * (t*t*((s+1)*t+s) + 2)
}
}