animation.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package fyne
  2. import "time"
  3. // AnimationCurve represents an animation algorithm for calculating the progress through a timeline.
  4. // Custom animations can be provided by implementing the "func(float32) float32" definition.
  5. // The input parameter will start at 0.0 when an animation starts and travel up to 1.0 at which point it will end.
  6. // A linear animation would return the same output value as is passed in.
  7. type AnimationCurve func(float32) float32
  8. // AnimationRepeatForever is an AnimationCount value that indicates it should not stop looping.
  9. //
  10. // Since: 2.0
  11. const AnimationRepeatForever = -1
  12. var (
  13. // AnimationEaseInOut is the default easing, it starts slowly, accelerates to the middle and slows to the end.
  14. //
  15. // Since: 2.0
  16. AnimationEaseInOut = animationEaseInOut
  17. // AnimationEaseIn starts slowly and accelerates to the end.
  18. //
  19. // Since: 2.0
  20. AnimationEaseIn = animationEaseIn
  21. // AnimationEaseOut starts at speed and slows to the end.
  22. //
  23. // Since: 2.0
  24. AnimationEaseOut = animationEaseOut
  25. // AnimationLinear is a linear mapping for animations that progress uniformly through their duration.
  26. //
  27. // Since: 2.0
  28. AnimationLinear = animationLinear
  29. )
  30. // Animation represents an animated element within a Fyne canvas.
  31. // These animations may control individual objects or entire scenes.
  32. //
  33. // Since: 2.0
  34. type Animation struct {
  35. AutoReverse bool
  36. Curve AnimationCurve
  37. Duration time.Duration
  38. RepeatCount int
  39. Tick func(float32)
  40. }
  41. // NewAnimation creates a very basic animation where the callback function will be called for every
  42. // rendered frame between time.Now() and the specified duration. The callback values start at 0.0 and
  43. // will be 1.0 when the animation completes.
  44. //
  45. // Since: 2.0
  46. func NewAnimation(d time.Duration, fn func(float32)) *Animation {
  47. return &Animation{Duration: d, Tick: fn}
  48. }
  49. // Start registers the animation with the application run-loop and starts its execution.
  50. func (a *Animation) Start() {
  51. CurrentApp().Driver().StartAnimation(a)
  52. }
  53. // Stop will end this animation and remove it from the run-loop.
  54. func (a *Animation) Stop() {
  55. CurrentApp().Driver().StopAnimation(a)
  56. }
  57. func animationEaseIn(val float32) float32 {
  58. return val * val
  59. }
  60. func animationEaseInOut(val float32) float32 {
  61. if val <= 0.5 {
  62. return val * val * 2
  63. }
  64. return -1 + (4-val*2)*val
  65. }
  66. func animationEaseOut(val float32) float32 {
  67. return val * (2 - val)
  68. }
  69. func animationLinear(val float32) float32 {
  70. return val
  71. }