Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d56f9e8

Browse files
committed
Rework TimeDelta func into a simpler FPS func and remove FPS consts
1 parent 17bfa34 commit d56f9e8

File tree

3 files changed

+12
-18
lines changed

3 files changed

+12
-18
lines changed

‎examples/bubbletea/main.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
const (
16-
fps = time.Second / 60
16+
fps = 60
1717
spriteWidth = 12
1818
spriteHeight = 5
1919
frequency = 0.95
@@ -34,7 +34,7 @@ var (
3434
type frameMsg time.Time
3535

3636
func animate() tea.Cmd {
37-
return tea.Tick(fps, func(t time.Time) tea.Msg {
37+
return tea.Tick(time.Second/fps, func(t time.Time) tea.Msg {
3838
return frameMsg(t)
3939
})
4040
}
@@ -94,7 +94,7 @@ func (m model) View() string {
9494

9595
func main() {
9696
m := model{
97-
spring: harmonica.NewSpring(harmonica.TimeDelta(fps), frequency, damping),
97+
spring: harmonica.NewSpring(harmonica.FPS(fps), frequency, damping),
9898
}
9999

100100
if err := tea.NewProgram(m).Start(); err != nil {

‎examples/canvas/main.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func main() {
5050

5151
circ = Circle{
5252
// Setup a new spring.
53-
spring: harmonica.NewSpring(harmonica.TimeDelta(time.Second/fps), frequency, damping),
53+
spring: harmonica.NewSpring(harmonica.FPS(fps), frequency, damping),
5454

5555
// Set target radius.
5656
Radius: circleRadius,

‎harmonica.go‎

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
//
55
// Example usage:
66
//
7-
// // Run once to initialize. Note the FPS.
8-
// spring := NewSpring(FPS60, 0.8, 1.0)
7+
// // Run once to initialize.
8+
// spring := NewSpring(FPS(60), 0.8, 1.0)
99
//
1010
// // Update on every frame.
1111
// pos := 0.0
@@ -55,22 +55,16 @@ import (
5555
"time"
5656
)
5757

58-
// Standard time deltas that can be used when initializing a spring.
59-
const (
60-
FPS60 = float64(int64(time.Second/60)) / float64(int64(time.Second))
61-
FPS30 = float64(int64(time.Second/30)) / float64(int64(time.Second))
62-
)
63-
64-
// TimeDelta returns a time delta for a given FPS. This is usefaul for setting
65-
// the time delta when initializing a Spring.
58+
// FPS returns a time delta for a given number of frames per second. This value
59+
// can be used as the time delta when initializing a Spring.
6660
//
6761
// Example:
6862
//
69-
// delta := TimeDelta(time.Second/60)
70-
// spring := New(delta, 0.98, 0.8)
63+
// spring := NewSpring(FPS(60), 0.8, 0.98)
7164
//
72-
func TimeDelta(fps time.Duration) float64 {
73-
return float64(int64(fps)) / float64(int64(time.Second))
65+
func FPS(n int) float64 {
66+
d := time.Second / time.Duration(n)
67+
return float64(int64(d)) / float64(int64(time.Second))
7468
}
7569

7670
// In calculus ε is (in vague terms) an arbitrarily small positive number. In

0 commit comments

Comments
 (0)