跳到主要内容

转场

转场让你可以自定义场景之间切换的方式。 转场是在场景开始时执行的一段动画。 它可以修改当前场景和上一个场景的上下文。

开始之前​

请确保你的项目至少包含两个场景。在本例中, 我们准备了 firstScene.tsx 和 secondScene.tsx,并将项目配置为依次显示它们。 我们将在第二个场景中设置转场。

请确保两个场景中放置不同的内容, 以便更清楚地看到转场效果。

my-animation/
└─ src/
├─ scenes/
│ ├─ firstScene.tsx
│ └─ secondScene.tsx
└─ project.ts

预设转场​

Motion Canvas 提供了一组常用转场, 以易于使用的生成器形式呈现。要使用它们, 请在新场景开头对转场生成器执行 yield*:

src/scenes/secondScene.tsx
export 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​

public slideTransitiondirection: Directionduration?: number: ThreadGenerator
public slideTransitionorigin: Originduration?: number: ThreadGenerator

Perform a transition that slides the scene in the given direction.

Parameters

  • direction: Direction

    The direction in which to slide.

  • duration?: number

    The duration of the transition.


zoomInTransition​

public zoomInTransitionarea: BBoxduration: number = 0.6: ThreadGenerator

Perform a transition that zooms in on a given area of the scene.

Parameters

  • area: BBox

    The area on which to zoom in.

  • duration: number = 0.6

    The duration of the transition.


zoomOutTransition​

public zoomOutTransitionarea: BBoxduration: number = 0.6: ThreadGenerator

Perform a transition that zooms out from a given area of the scene.

Parameters

  • area: BBox

    The area from which to zoom out.

  • duration: number = 0.6

    The duration of the transition.


fadeTransition​

public fadeTransitionduration: number = 0.6: ThreadGenerator

Perform a transition that fades between the scenes.

Parameters

  • duration: number = 0.6

    The duration of the transition.


waitTransition​

public waitTransitionduration: number = 0.6previousOnTop: SignalValueboolean = true: ThreadGenerator

Perform a transition that doesn't do anything.

This is useful when you want to achieve a transition effect by animating objects in the scenes. It will overlay the scenes on top of each other for the duration of the transition.

Parameters

  • duration: number = 0.6

    The duration of the transition.

  • previousOnTop: SignalValueboolean = true

    Whether the previous scene should be rendered on top.


自定义转场​

你可以使用 useTransition 函数实现自定义转场。 它允许你指定两个回调函数, 分别用于修改当前场景和上一个场景的上下文。 它返回的值是一个回调函数,你需要在转场完成后调用它。

转场的模板如下所示:

// set up the transition
const endTransition = useTransition(
currentContext => {
// modify the context of the current scene
},
previousContext => {
// modify the context of the previous scene
},
);

// perform animations

// finish the transition
endTransition();

下面是实现一个简单的滑动转场的方法:

export function* slideTransition(
direction: Direction = Direction.Top,
duration = 0.6,
): ThreadGenerator {
const size = useScene().getRealSize();
const position = size.getOriginOffset(direction).scale(2);
const previousPosition = Vector2.createSignal();
const currentPosition = Vector2.createSignal(position);

// set up the transition
const endTransition = useTransition(
// modify the context of the current scene
ctx => ctx.translate(currentPosition.x(), currentPosition.y()),
// modify the context of the previous scene
ctx => ctx.translate(previousPosition.x(), previousPosition.y()),
);

// perform animations
yield* all(
previousPosition(position.scale(-1), duration),
currentPosition(Vector2.zero, duration),
);

// finish the transition
endTransition();
}

转场期间播放动画​

默认情况下,当当前场景的生成器执行到末尾时, Motion Canvas 才会切换到下一个场景。 在这种情况下,场景会在转场期间静止。 你可以使用 finishScene 函数提前触发转场,使动画在转场期间继续播放:

export default makeScene2D(function* (view) {
yield* animationOne();
// trigger the transition early:
finishScene();
// continue animating:
yield* animationTwo();
});