Animation flow
Note: These docs were adopted from the original Motion Canvas docs
Motion Canvas uses generator functions to describe animations.
A generator function is a function that can return multiple values:
function* example() {
yield 1;
yield 2;
yield 3;
}
const generator = example();
console.log(generator.next().value); // 1;
console.log(generator.next().value); // 2;
console.log(generator.next().value); // 3;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:
export default makeScene2D(function* (view) {
const circle = createRef<Circle>();
view.add(<Circle ref={circle} width={100} height={100} />);
circle().fill('red');
yield;
circle().fill('blue');
yield;
circle().fill('red');
yield;
});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:
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 tweening section.
Flow Generatorsβ
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:
allβ
allβRun all tasks concurrently and wait for all of them to finish.
Examples
Parameters
...tasks: ThreadGenerator[]A list of tasks to run.
anyβ
anyβRun all tasks concurrently and wait for any of them to finish.
Examples
Parameters
...tasks: ThreadGenerator[]A list of tasks to run.
chainβ
chainβRun tasks one after another.
Examples
Parameters
...tasks: CallbackThreadGenerator[]A list of tasks to run.
delayβ
delayβRun the given generator or callback after a specific amount of time.
Examples
Parameters
time: numberThe delay in seconds
task: CallbackThreadGeneratorThe task or callback to run after the delay.
sequenceβ
sequenceβ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: numberThe delay between each of the tasks.
...tasks: ThreadGenerator[]A list of tasks to be run in a sequence.
loopβ
loopβRun the given generator in a loop.
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 spawn to run the loop concurrently.
Examples
Parameters
factory: LoopCallbackA function creating the generator to run. Because generators can't be reset, a new generator is created on each iteration.
Loopingβ
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.
Using a for loopβ
for loopβ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.
Last updated