动画流程控制
Motion Canvas 使用生成器函数来描述动画。
生成器函数是可以返回多个值的函数:
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;
当遇到 yield 关键字时,函数的执行会暂停,
只有在调用方请求下一个值时才会继续。这在声明动画时
尤其有用——通常我们希望以增量步骤改变屏幕上的内容,
以制造运动的错觉。我们还希望在这些更新之间
等待固定的时间,以便眼睛能够
感知到发生了什么。借助生成器,我们可以在 yield 关键字
之间更新内容,并在函数每次 yield 时稍作等待 。
这是 Motion Canvas 的基本理念。yield 意味着:“当前帧
已准备好,将其显示在屏幕上,稍后再回到我这里。”
有了这个概念,我们可以使用以下代码让屏幕上的圆 闪烁:
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;
});
不用说,如果所有动画都要这样编写,将会极其
繁琐。幸运的是,JavaScript 在生成器内还提供了另一个关键字——
yield*。它允许我们将 yield
委托给另一个生成器。
例如,我们可以将上例中的闪烁代码提取到一个单独的 生成器中,并让场景函数委托给它:
import {createRef, ThreadGenerator} from '@motion-canvas/core';
import {makeScene2D, Circle} from '@motion-canvas/2d';
export default makeScene2D(function* (view) {
const circle = createRef<Circle>();
view.add(<Circle ref={circle} width={100} height={100} />);
yield* flicker(circle());
});
function* flicker(circle: Circle): ThreadGenerator {
circle.fill('red');
yield;
circle.fill('blue');
yield;
circle.fill('red');
yield;
}
最终得到的动画完全相同,但现在我们有了一个可复用的函数, 可以在需要闪烁效果时随时使用。
Motion Canvas 提供了许多类似的有用生成器。你可能还记得 快速入门中的这段代码:
yield * myCircle().fill('#e6a700', 1);
它会在一秒内将圆的填充颜色从当前值动画过渡到 #e6a700。
如你所想,调用 fill('#e6a700', 1) 的结果是另一个
生成器,我们可以将场景函数重定向到它。这样的生成器
称为补间(tween),因为它们会在两个值之间
(between)制作动画。你可以在
补间动画一节中了解更多。
流程控制生成器
另一种生成器是 流程控制生成器。它们接受一个或多个
生成器作为输入,并将它们组合在一起。我们在快速入门一节中提到过 all()
生成器,此外还有几个:
all
public all...tasks: ThreadGenerator[]: ThreadGenerator
Run all tasks concurrently and wait for all of them to finish.
Examples
Parameters
...tasks: ThreadGenerator[]A list of tasks to run.
any
public any...tasks: ThreadGenerator[]: ThreadGenerator
Run all tasks concurrently and wait for any of them to finish.
Examples
Parameters
...tasks: ThreadGenerator[]A list of tasks to run.
chain
public chain...tasks: CallbackThreadGenerator[]: ThreadGenerator
Run tasks one after another.
Examples
Parameters
...tasks: CallbackThreadGenerator[]A list of tasks to run.
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
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
public loopfactory: LoopCallback: ThreadGenerator
public loopiterations: numberfactory: LoopCallback: ThreadGenerator
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.
循环
为多个对象制作动画有很多种方法。以下是一些示例。 请在下面的编辑器中尝试使用它们。
import ...
export default makeScene2D(function* (view) {
const rects: Rect[] = [];
// Create some rects
view.add(
range(5).map(i => (
<Rect
ref={makeRef(rects, i)}
width={100}
height={100}
x={-250 + 125 * i}
fill="#88C0D0"
radius={10}
/>
)),
);
yield* waitFor(1);
// Animate them
yield* all(
...rects.map(rect => rect.position.y(100, 1).to(-100, 2).to(0, 1)),
);
});
结合使用 Array.map 与 all
这是进行简单补间的优雅方法之一,但如果要在同一对象上执行多个补间,
则需要嵌套 all,因为 map 回调必须返 回一个
ThreadGenerator。
yield *
all(
...rects.map(rect =>
// No yield or anything; we return this generator and deal with it outside
rect.position.y(100, 1).to(-100, 2).to(0, 1),
),
);
结合使用 for 循环与 all
这与上面的方法类似,但使用 for 循环和生成器数组。
const generators = [];
for (const rect of rects) {
// No yield here, just store the generators.
generators.push(rect.position.y(100, 1).to(-100, 2).to(0, 1));
}
// Run all of the generators.
yield * all(...generators);
使用 for 循环
这是一种稍显繁琐的选择,因为你必须算出循环中 的生成器需要多长时间才能完成,但在某些情况下 非常有用。
for (const rect of rects) {
// Note the absence of a * after this yield
yield rect.position.y(100, 1).to(-100, 2).to(0, 1);
}
// Wait for the duration of the above generators
yield * waitFor(4);