animation.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // SPDX-License-Identifier: Unlicense OR MIT
  2. package fling
  3. import (
  4. "math"
  5. "runtime"
  6. "time"
  7. "gioui.org/unit"
  8. )
  9. type Animation struct {
  10. // Current offset in pixels.
  11. x float32
  12. // Initial time.
  13. t0 time.Time
  14. // Initial velocity in pixels pr second.
  15. v0 float32
  16. }
  17. const (
  18. // dp/second.
  19. minFlingVelocity = unit.Dp(50)
  20. maxFlingVelocity = unit.Dp(8000)
  21. thresholdVelocity = 1
  22. )
  23. // Start a fling given a starting velocity. Returns whether a
  24. // fling was started.
  25. func (f *Animation) Start(c unit.Metric, now time.Time, velocity float32) bool {
  26. min := float32(c.Dp(minFlingVelocity))
  27. v := velocity
  28. if -min <= v && v <= min {
  29. return false
  30. }
  31. max := float32(c.Dp(maxFlingVelocity))
  32. if v > max {
  33. v = max
  34. } else if v < -max {
  35. v = -max
  36. }
  37. f.init(now, v)
  38. return true
  39. }
  40. func (f *Animation) init(now time.Time, v0 float32) {
  41. f.t0 = now
  42. f.v0 = v0
  43. f.x = 0
  44. }
  45. func (f *Animation) Active() bool {
  46. return f.v0 != 0
  47. }
  48. // Tick computes and returns a fling distance since
  49. // the last time Tick was called.
  50. func (f *Animation) Tick(now time.Time) int {
  51. if !f.Active() {
  52. return 0
  53. }
  54. var k float32
  55. if runtime.GOOS == "darwin" {
  56. k = -2 // iOS
  57. } else {
  58. k = -4.2 // Android and default
  59. }
  60. t := now.Sub(f.t0)
  61. // The acceleration x''(t) of a point mass with a drag
  62. // force, f, proportional with velocity, x'(t), is
  63. // governed by the equation
  64. //
  65. // x''(t) = kx'(t)
  66. //
  67. // Given the starting position x(0) = 0, the starting
  68. // velocity x'(0) = v0, the position is then
  69. // given by
  70. //
  71. // x(t) = v0*e^(k*t)/k - v0/k
  72. //
  73. ekt := float32(math.Exp(float64(k) * t.Seconds()))
  74. x := f.v0*ekt/k - f.v0/k
  75. dist := x - f.x
  76. idist := int(dist)
  77. f.x += float32(idist)
  78. // Solving for the velocity x'(t) gives us
  79. //
  80. // x'(t) = v0*e^(k*t)
  81. v := f.v0 * ekt
  82. if -thresholdVelocity < v && v < thresholdVelocity {
  83. f.v0 = 0
  84. }
  85. return idist
  86. }