2

Is there a way to combine various built-in functions for animations or even create custom functions?

I like the in_out_cubic, in_out_quad, in_out_sine functions, but I want to make something like in_cubic_out_sine and see if it would be ok.

It's also interesting to experiment with other math functions to create various effects.

How can this be done in Kivy?

Juraj Fiala
  • 170
  • 1
  • 12
me2 beats
  • 774
  • 1
  • 8
  • 24

1 Answers1

3

What you point out may have several interpretations, so I'm going to show you the different possibilities:

  • Use the in_cubic animation from p1 to p2 and out_sine from p2 to the final point p3.

    from kivy.animation import Animation
    from kivy.app import App
    from kivy.uix.button import Button
    
    class TestApp(App):
        def animate(self, instance):
            animation = Animation(pos=(200, 200), t='in_cubic')
            animation += Animation(pos=(400, 400), t='out_sine')
            animation.start(instance)
    
        def build(self):
            button = Button(size_hint=(None, None), text='plop',
                            on_press=self.animate)
            return button
    
    if __name__ == '__main__':
        TestApp().run()
    
  • Apply in 50% of the advance in_cubic and in the other out_sine, for this we create a new function:

    from kivy.animation import Animation, AnimationTransition
    from kivy.app import App
    from kivy.uix.button import Button
    
    
    def in_cubic_out_sine(progress):
        return AnimationTransition.in_cubic(progress) if progress < 0.5 else AnimationTransition.out_sine(progress)
    
    class TestApp(App):
        def animate(self, instance):
            animation = Animation(pos=(200, 200), t=in_cubic_out_sine)
            animation.start(instance)
    
        def build(self):
            button = Button(size_hint=(None, None), text='plop',
                            on_press=self.animate)
            return button
    
    if __name__ == '__main__':
        TestApp().run()
    

And in general you can implement your own function, the only thing to keep in mind that progress takes values ​​from 0 to 1:

from kivy.animation import Animation
from kivy.app import App
from kivy.uix.button import Button

from math import cos, sin, pi, exp

def custom_animation(progress):
    return 1 - exp(-progress**2)*cos(progress*pi)**3

class TestApp(App):
    def animate(self, instance):
        animation = Animation(pos=(200, 200), t=custom_animation)
        animation.start(instance)

    def build(self):
        button = Button(size_hint=(None, None), text='plop',
                        on_press=self.animate)
        return button

if __name__ == '__main__':
    TestApp().run()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • wow! almost what I need! almost, because I need something like your first example, but 1) with the parameters pos = (200, 200) for the first animation and pos = (400, 400) for the second animation (one-way movement); 2) the whole animation should be smooth, there should be no jumps in the middle of the animation. I somehow see a jump in the first example and a very big jump in the second example, I'm testing on Ubuntu. Could you check it on your system? – me2 beats Aug 08 '18 at 20:54
  • ok, but the question with jumping remained. it turns out I can not use either the first or second examples, if I want the animation to be smooth. I checked it using uniform motion (without "t=..." ) - even in this case I see jump between animations – me2 beats Aug 08 '18 at 21:39
  • I have used animations as in the first example from @eyllanesc, without seeing "jumping". I have seen what might appear as "jumping" when the two animations don't match well. In the plots of the transition functions in the [Animation](https://kivy.org/docs/api-kivy.animation.html) documentation, the slope of the line indicates velocity. If the ending velocity of the first animation doesn't match the starting velocity of the second, you can see what might be described as "jumping". – John Anderson Aug 09 '18 at 03:30