animation.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package canvas
  2. import (
  3. "image/color"
  4. "time"
  5. "fyne.io/fyne/v2"
  6. )
  7. const (
  8. // DurationStandard is the time a standard interface animation will run.
  9. //
  10. // Since: 2.0
  11. DurationStandard = time.Millisecond * 300
  12. // DurationShort is the time a subtle or small transition should use.
  13. //
  14. // Since: 2.0
  15. DurationShort = time.Millisecond * 150
  16. )
  17. // NewColorRGBAAnimation sets up a new animation that will transition from the start to stop Color over
  18. // the specified Duration. The colour transition will move linearly through the RGB colour space.
  19. // The content of fn should apply the color values to an object and refresh it.
  20. // You should call Start() on the returned animation to start it.
  21. //
  22. // Since: 2.0
  23. func NewColorRGBAAnimation(start, stop color.Color, d time.Duration, fn func(color.Color)) *fyne.Animation {
  24. r1, g1, b1, a1 := start.RGBA()
  25. r2, g2, b2, a2 := stop.RGBA()
  26. rStart := int(r1 >> 8)
  27. gStart := int(g1 >> 8)
  28. bStart := int(b1 >> 8)
  29. aStart := int(a1 >> 8)
  30. rDelta := float32(int(r2>>8) - rStart)
  31. gDelta := float32(int(g2>>8) - gStart)
  32. bDelta := float32(int(b2>>8) - bStart)
  33. aDelta := float32(int(a2>>8) - aStart)
  34. return &fyne.Animation{
  35. Duration: d,
  36. Tick: func(done float32) {
  37. fn(color.RGBA{R: scaleChannel(rStart, rDelta, done), G: scaleChannel(gStart, gDelta, done),
  38. B: scaleChannel(bStart, bDelta, done), A: scaleChannel(aStart, aDelta, done)})
  39. }}
  40. }
  41. // NewPositionAnimation sets up a new animation that will transition from the start to stop Position over
  42. // the specified Duration. The content of fn should apply the position value to an object for the change
  43. // to be visible. You should call Start() on the returned animation to start it.
  44. //
  45. // Since: 2.0
  46. func NewPositionAnimation(start, stop fyne.Position, d time.Duration, fn func(fyne.Position)) *fyne.Animation {
  47. xDelta := float32(stop.X - start.X)
  48. yDelta := float32(stop.Y - start.Y)
  49. return &fyne.Animation{
  50. Duration: d,
  51. Tick: func(done float32) {
  52. fn(fyne.NewPos(scaleVal(start.X, xDelta, done), scaleVal(start.Y, yDelta, done)))
  53. }}
  54. }
  55. // NewSizeAnimation sets up a new animation that will transition from the start to stop Size over
  56. // the specified Duration. The content of fn should apply the size value to an object for the change
  57. // to be visible. You should call Start() on the returned animation to start it.
  58. //
  59. // Since: 2.0
  60. func NewSizeAnimation(start, stop fyne.Size, d time.Duration, fn func(fyne.Size)) *fyne.Animation {
  61. widthDelta := float32(stop.Width - start.Width)
  62. heightDelta := float32(stop.Height - start.Height)
  63. return &fyne.Animation{
  64. Duration: d,
  65. Tick: func(done float32) {
  66. fn(fyne.NewSize(scaleVal(start.Width, widthDelta, done), scaleVal(start.Height, heightDelta, done)))
  67. }}
  68. }
  69. func scaleChannel(start int, diff, done float32) uint8 {
  70. return uint8(start + int(diff*done))
  71. }
  72. func scaleVal(start float32, delta, done float32) float32 {
  73. return start + delta*done
  74. }