me2 beats
me2 beats

Reputation: 824

Kivy custom animation functions

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?

Upvotes: 2

Views: 390

Answers (1)

eyllanesc
eyllanesc

Reputation: 243965

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()

Upvotes: 3

Related Questions