Animation flow
Last updated
Last updated
Note: These docs were adopted from the original docs
Motion Canvas uses generator functions to describe animations.
A generator function is a function that can return multiple values:
When the yield
keyword is encountered, the execution of the function pauses, and resumes only when the caller requests another value. This is particularly useful when declaring animations - usually we want to change the things on the screen in incremental steps to create an illusion of movement. We also want to wait a constant amount of time between these updates so that our eyes can register what's happening. With generators, we can update things in-between the yield
keywords, and then wait for a bit whenever the function yields.
This is the fundamental idea of Motion Canvas. yield
means: "The current frame is ready, display it on the screen and come back to me later."
With that in mind, we can make a circle flicker on the screen using the following code:
Needless to say, it would be extremely cumbersome if we had to write all animations like that. Fortunately, JavaScript has another keyword for use within generators - yield*
. It allows us to delegate the yielding to another generator.
For instance, we could extract the flickering code from the above example to a separate generator and delegate our scene function to it:
The resulting animation is exactly the same, but now we have a reusable function that we can use whenever we need some flickering.
Motion Canvas provides a lot of useful generators like this. You may remember this snippet:
Another kind of generators are flow generators. They take one or more generators as their input and combine them together. We've mentioned the all()
generator in the quickstart section, there's a few more:
Run all tasks concurrently and wait for all of them to finish.
Examples
Parameters
...tasks: ThreadGenerator[]
A list of tasks to run.
Run all tasks concurrently and wait for any of them to finish.
Examples
Parameters
...tasks: ThreadGenerator[]
A list of tasks to run.
Run tasks one after another.
Examples
Parameters
...tasks: CallbackThreadGenerator[]
A list of tasks to run.
Run the given generator or callback after a specific amount of time.
Examples
Parameters
time: number
The delay in seconds
task: CallbackThreadGenerator
The task or callback to run after the delay.
Start all tasks one after another with a constant delay between.
The function doesn't wait until the previous task in the sequence has finished. Once the delay has passed, the next task will start even if the previous is still running.
Examples
Parameters
delay: number
The delay between each of the tasks.
...tasks: ThreadGenerator[]
A list of tasks to be run in a sequence.
Run the given generator in a loop.
Examples
Parameters
factory: LoopCallback
A function creating the generator to run. Because generators can't be reset, a new generator is created on each iteration.
There are many ways to animate multiple objects. Here are some examples. Try using them in the below editor.
This is one of the most elegant ways to do simple tweens, but requires nesting all
to do multiple tweens on an object since the map
callback must return a ThreadGenerator
.
This is similar to above, but uses a for
loop and an array of generators.
This is a bit of a cumbersome option because you have to figure out how long it would take for the generator in the loop to complete, but is useful in some situations.
It animates the fill color of the circle from its current value to #e6a700
over a span of one second. As you may guess, the result of calling fill('#e6a700', 1)
is another generator to which we can redirect our scene function. Generators like this are called tweens, because they animate between two values. You can read more about them in the section.
Each iteration waits until the previous one is completed. Because this loop never finishes it cannot be used in the main thread. Instead, use yield
or to run the loop concurrently.