转场
转场让你可以自定义场景之间切换的方式。 转场是在场景开始时执行的一段动画。 它可以修改当前场景和上一个场景的上下文。
开始之前
请确保你的项目至少包含两个场景。在本例中,
我们准备了 firstScene.tsx
和 secondScene.tsx,并将项目配置为依次显示它们。
我们将在第二个场景中设置转场。
请确保两个场景中放置不同的内容, 以便更清楚地看到转场效果。
my-animation/
└─ src/
├─ scenes/
│ ├─ firstScene.tsx
│ └─ secondScene.tsx
└─ project.ts
预设转场
Motion Canvas 提供了一组常用转场,
以易于使用的生成器形式呈现。要使用它们,
请在新场景开头对转场生成器执行 yield*:
src/scenes/secondScene.tsxexport default makeScene2D(function* (view) {
// set up the scene:
view.add(/* your nodes here */);
// perform a slide transition to the left:
yield* slideTransition(Direction.Left);
// proceed with the animation
yield* waitFor(3);
});
请务必在 yield 转场生成器之前将节点添加到视图中。 否则,在转场结束之前,你的场景将一直保持空白。
所有可用的转场列出如下:
slideTransition
Perform a transition that slides the scene in the given direction.
Parameters
direction: DirectionThe direction in which to slide.
duration?: numberThe duration of the transition.
zoomInTransition
Perform a transition that zooms in on a given area of the scene.