-
Notifications
You must be signed in to change notification settings - Fork 4
/
elastic.go
113 lines (88 loc) · 2.42 KB
/
elastic.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
package easing
import (
"github.com/gravestench/mathlib"
"math"
)
const (
defaultAmplitude = 0.1
defaultPeriod = 0.1
)
var _ EaseFunctionProvider = &ElasticOutEaseProvider{}
var _ EaseFunctionProvider = &ElasticInEaseProvider{}
var _ EaseFunctionProvider = &ElasticInOutEaseProvider{}
type ElasticOutEaseProvider struct{}
func (*ElasticOutEaseProvider) New(params []float64) func(float64) float64 {
params = ensureElasticParams(params)
amplitude, period := params[0], params[1]
elastic := func(v float64) float64 {
if math.Abs(0-v) < math.SmallestNonzeroFloat64 {
return 0
} else if math.Abs(1-v) < math.SmallestNonzeroFloat64 {
return 1
}
s := period / 4
if amplitude < 1 {
amplitude = 1
} else {
s = period * math.Asin(1/amplitude) / mathlib.PI2
}
return amplitude*math.Pow(2, -10*v)*math.Sin((v-s)*mathlib.PI2/period) + 1
}
return elastic
}
type ElasticInEaseProvider struct{}
func (*ElasticInEaseProvider) New(params []float64) func(float64) float64 {
params = ensureElasticParams(params)
amplitude, period := params[0], params[1]
elastic := func(v float64) float64 {
if math.Abs(0-v) < math.SmallestNonzeroFloat64 {
return 0
} else if math.Abs(1-v) < math.SmallestNonzeroFloat64 {
return 1
}
s := period / 4
if amplitude < 1 {
amplitude = 1
} else {
s = period * math.Asin(1/amplitude) / mathlib.PI2
}
return -(amplitude * math.Pow(2, 10*(v-1)) * math.Sin((v-s)*mathlib.PI2/period))
}
return elastic
}
type ElasticInOutEaseProvider struct{}
func (*ElasticInOutEaseProvider) New(params []float64) func(float64) float64 {
params = ensureElasticParams(params)
amp, period := params[0], params[1]
elastic := func(v float64) float64 {
if math.Abs(0-v) < math.SmallestNonzeroFloat64 {
return 0
} else if math.Abs(1-v) < math.SmallestNonzeroFloat64 {
return 1
}
s := period / 4
if amp < 1 {
amp = 1
} else {
s = period * math.Asin(1/amp) / mathlib.PI2
}
v *= 2
if v < 1 {
return -0.5 * (amp * math.Pow(2, 10*(v-1)) * math.Sin((v-s)*mathlib.PI2/period))
}
return amp*math.Pow(2, -10*(v-1))*math.Sin((v-s)*mathlib.PI2/period)*0.5 + 1
}
return elastic
}
func ensureElasticParams(params []float64) []float64 {
if params == nil {
params = []float64{defaultAmplitude, defaultPeriod}
}
switch len(params) {
case 0:
params = []float64{defaultAmplitude, defaultPeriod}
case 1:
params = append(params, defaultPeriod)
}
return params
}