Giter Club home page Giter Club logo

koreography's Introduction

Koreography

Choreograph your Compose Animation ๐Ÿ’ƒ๐Ÿ•บ

Maven Central

Github Followers Twitter Follow


A lightweight Compose Animation utility library to choreograph low-level Animation API (https://developer.android.com/jetpack/compose/animation#animation) through Kotlin DSL. It does the heavy lifting of dealing with coroutines under the hood so that you can focus on your animation choreography.

Including in your project

Koreography is available on mavenCentral()

implementation 'io.github.sagar-viradiya:koreography:0.3.0'

Usage

Creating choreography is the process of recording moves on Animatable, A low level compose animation API. Moves could be either parallel or sequential. A choreography is a composition of such moves that you can declare through clean and concise Kotlin DSL as shown below.

For example, let's say you want to sequentially animate alpha first and then scale of an image then you can declare the following choreography. You would then apply animatable values that you are animating in choreography in the Image composable as shown below.

Choreographying sequential animation

val alphaAnimatable = remember {
    Animatable(0f)
}

val scaleAnimatable = remember {
    Animatable(0f)
}

val koreography = rememberKoreography {
    move(
        animatable = alphaAnimatable,
        targetValue = 1f,
        animationSpec = tween(500)
    )
    
    move(
        animatable = scaleAnimatable,
        targetValue = 2f,
        animationSpec = tween(500)
    )
}
.
.
.
Image(
    modifier = Modifier.graphicsLayer {
        alpha = alphaAnimatable.value
        scaleX = scaleAnimatable.value
    }, 
    painter = painterResource(id = R.drawable.image)
)

Choreographying parallel animation

Let's say you want to parallelly animate alpha and scale of an image then you can declare the following choreography.

val alphaAnimatable = remember {
    Animatable(0f)
}

val scaleAnimatable = remember {
    Animatable(0f)
}

val koreography = rememberKoreography {
    parallelMoves {
        move(
            animatable = alphaAnimatable,
            targetValue = 1f,
            animationSpec = tween(500)
        )
        
        move(
            animatable = scaleAnimatable,
            targetValue = 2f,
            animationSpec = tween(500)
        )
    }
}
.
.
.
Image(
    modifier = Modifier.graphicsLayer {
        alpha = alphaAnimatable.value
        scaleX = scaleAnimatable.value
    }, 
    painter = painterResource(id = R.drawable.image)
)

Complex choreography

You can have a nested hierarchy of moves to create complex choreography. The example below has three animations running parallelly and out of them, the last one has two animations within running sequentially.

val alphaAnimatable = remember {
    Animatable(0f)
}

val scaleAnimatable = remember {
    Animatable(0f)
}

val rotationAnimatable = remember {
    Animatable(0f)
}

val translationXAnimatable = remember {
    Animatble(0f)
}

val koreography = rememberKoreography {
    parallelMoves {
        move(
            animatable = alphaAnimatable,
            targetValue = 1f,
            animationSpec = tween(500)
        )
        
        move(
            animatable = scaleAnimatable,
            targetValue = 2f,
            animationSpec = tween(500)
        )
        
        sequentialMoves {
            move(
                animatable = rotationAnimatable,
                targetValue = 20f,
                animationSpec = tween(500)
            )
            
            move(
                animatable = translationXAnimatable,
                targetValue = 200f,
                animationSpec = tween(500)
            )
        }
    }
}
.
.
.
Image(
    modifier = Modifier.graphicsLayer {
        alpha = alphaAnimatable.value
        scaleX = scaleAnimatable.value
        translationX = translationXAnimatable.value
        rotationZ = rotationAnimatable.value
    }, 
    painter = painterResource(id = R.drawable.image)
)

Executing choreography

Once choreography is ready it's time to dance! ๐Ÿ’ƒ๐Ÿ•บ.

One time dance

You can execute the choreography once by calling dance function. If you wish to get callback after execution of choreography then you can pass trailing lambda.

koreography.dance(scope = coroutineScope) {
    // onDanceFinished : Optional trailing lambda
}

Repeat dance

You can execute the choreography multiple times by calling repeatDance function. Following call executes choreography three times.

koreography.repeatDance(count = 3, scope = coroutineScope) {
    // onDanceFinished : Optional trailing lambda
}

Infinite dance

You can execute the choreography forever until composition is alive.

koreography.danceForever(scope = coroutineScope){
    // onDanceFinished : Optional trailing lambda
}

Please note the coroutineScope should be obtained through rememberCoroutineScope(). Make sure you pass coroutine scope which will get cleared once you exit composition.

Executing choreography based on state change ๐Ÿš€

Executing choreography based on state change is also supported. This API is similar to LaunchedEffect API of compose side effects.

In the following example, we are executing choreography passed in the trailing lambda of LaunchKoreography composable on a state value change. Choreography contains two animations that will be executed sequentially.

val alphaAnimatable = remember {
    Animatable(0f)
}

val scaleAnimatable = remember {
    Animatable(0f)
}

LaunchKoreography(state) {
    move(
        animatbale = alphaAnimatable,
        targetValue = 1f,
        animationSpec = tween(500)
    )
    
    move(
        animatbale = scaleAnimatable,
        targetValue = 2f,
        animationSpec = tween(500)
    )
}
.
.
.
Image(
    modifier = Modifier.graphicsLayer {
        alpha = alphaAnimatable.value
        scaleX = scaleAnimatable.value
    }, 
    painter = painterResource(id = R.drawable.image)
)

Samples

There is endless possibilities with power of coroutines and compose animation API! Here are some free lottie animations recreated using koreography. You can find the source code in the sample app.

screen-20230611-113322.2.mov
screen-20230611-113358.3.mp4
screen-20230611-113420.2.mp4
screen-20230611-113444.2.mov
meditaion_animation.mov

Contribution

This is the early preview and unfortunately it is not ready to accept any contribution yet. Once this is stable enough contribution guidelines will be updated here. Meanwhile feel free to start GitHub Discussions for feature request and improvements.

License

Copyright 2022 Koreography Contributors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

koreography's People

Contributors

sagar-viradiya avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.