Skip to content

Visualize Your Ideas With Code

docs: improve transitions guide (#360)

Jacob4d850dec44fb
diff --git a/packages/docs/docs/getting-started/transitions.mdx b/packages/docs/docs/getting-started/transitions.mdx
index 23c01eb1..cd8e465b 100644
--- a/packages/docs/docs/getting-started/transitions.mdx
+++ b/packages/docs/docs/getting-started/transitions.mdx
@@ -4,6 +4,7 @@ slug: /transitions
---
import ApiSnippet from '@site/src/components/Api/ApiSnippet';
+import AnimationPlayer from '@site/src/components/AnimationPlayer';
# Transitions
@@ -11,21 +12,63 @@ Transitions allow you to customize the way scenes transition from one into
another. A transition is an animation performed at the beginning of the scene.
It can modify the context of both the current and the previous scene.
+## Before we start
+
+<div className='row'>
+ <div className='col'>
+
+Make sure your project contains at least two scenes. In this example, we've
+prepared `firstScene.tsx` and `secondScene.tsx`, and configured our project to
+display one after the other. We'll be setting up our transitions in the second
+scene.
+
+Make sure to put something different in both scenes to easier see the
+transitions.
+
+ </div>
+ <div className='col'>
+
+```
+my-animation/
+└─ src/
+ ├─ scenes/
+ │ ├─ firstScene.tsx
+ │ └─ secondScene.tsx
+ └─ project.ts
+```
+
+ </div>
+</div>
+
## Pre-made transitions
Motion Canvas comes with a set of common transitions in a form of easy-to-use
-generator. To use them, yield the transition generator at the beggining of the
-scene:
+generators. To use them, `yield*` the transition generator at the beginning of
+the new scene:
-```ts
+```tsx title="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);
});
```
-The available transitions are:
+<AnimationPlayer small name={'transitions'} />
+
+:::caution
+
+Make sure to add nodes to the view before yielding the transition generator.
+Otherwise, your scene will remain empty until the transition ends.
+
+:::
+
+All available transitions are listed below:
### `slideTransition`
diff --git a/packages/examples/src/scenes/transitions-first.meta b/packages/examples/src/scenes/transitions-first.meta
new file mode 100644
index 00000000..d9d4e789
--- /dev/null
+++ b/packages/examples/src/scenes/transitions-first.meta
@@ -0,0 +1,4 @@
+{
+ "version": 1,
+ "seed": 2868148813
+}
\ No newline at end of file
diff --git a/packages/examples/src/scenes/transitions-first.tsx b/packages/examples/src/scenes/transitions-first.tsx
new file mode 100644
index 00000000..49569fc3
--- /dev/null
+++ b/packages/examples/src/scenes/transitions-first.tsx
@@ -0,0 +1,27 @@
+import {Rect, Text} from '@motion-canvas/2d/lib/components';
+import {makeScene2D} from '@motion-canvas/2d/lib/scenes';
+import {waitFor} from '@motion-canvas/core/lib/flow';
+
+export default makeScene2D(function* (view) {
+ view.add(
+ <Rect
+ width={'100%'}
+ height={'100%'}
+ fill={'lightseagreen'}
+ layout
+ alignItems={'center'}
+ justifyContent={'center'}
+ >
+ <Text
+ fontSize={160}
+ fontWeight={700}
+ fill={'#fff'}
+ fontFamily={'"JetBrains Mono", monospace'}
+ >
+ FIRST SCENE
+ </Text>
+ </Rect>,
+ );
+
+ yield* waitFor(1);
+});
diff --git a/packages/examples/src/scenes/transitions-second.meta b/packages/examples/src/scenes/transitions-second.meta
new file mode 100644
index 00000000..b337a552
--- /dev/null
+++ b/packages/examples/src/scenes/transitions-second.meta
@@ -0,0 +1,4 @@
+{
+ "version": 1,
+ "seed": 1065746214
+}
\ No newline at end of file
diff --git a/packages/examples/src/scenes/transitions-second.tsx b/packages/examples/src/scenes/transitions-second.tsx
new file mode 100644
index 00000000..6150b2f4
--- /dev/null
+++ b/packages/examples/src/scenes/transitions-second.tsx
@@ -0,0 +1,41 @@
+import {Rect, Text} from '@motion-canvas/2d/lib/components';
+import {makeScene2D} from '@motion-canvas/2d/lib/scenes';
+import {all, waitFor} from '@motion-canvas/core/lib/flow';
+import {slideTransition} from '@motion-canvas/core/lib/transitions';
+import {createRef} from '@motion-canvas/core/lib/utils';
+import {Direction} from '@motion-canvas/core/lib/types';
+
+export default makeScene2D(function* (view) {
+ const rect = createRef<Rect>();
+ const text = createRef<Text>();
+
+ view.add(
+ <Rect
+ ref={rect}
+ width={'100%'}
+ height={'100%'}
+ fill={'lightcoral'}
+ layout
+ alignItems={'center'}
+ justifyContent={'center'}
+ >
+ <Text
+ ref={text}
+ fontSize={160}
+ fontWeight={700}
+ fill={'#fff'}
+ fontFamily={'"JetBrains Mono", monospace'}
+ >
+ SECOND SCENE
+ </Text>
+ </Rect>,
+ );
+
+ yield* slideTransition(Direction.Left);
+
+ yield* waitFor(0.4);
+ yield* all(
+ rect().fill('lightseagreen', 0.6),
+ text().text('FIRST SCENE', 0.6),
+ );
+});
diff --git a/packages/examples/src/transitions.meta b/packages/examples/src/transitions.meta
new file mode 100644
index 00000000..0e33e3fe
--- /dev/null
+++ b/packages/examples/src/transitions.meta
@@ -0,0 +1,3 @@
+{
+ "version": 0
+}
\ No newline at end of file
diff --git a/packages/examples/src/transitions.ts b/packages/examples/src/transitions.ts
new file mode 100644
index 00000000..3c069bbf
--- /dev/null
+++ b/packages/examples/src/transitions.ts
@@ -0,0 +1,8 @@
+import {makeProject} from '@motion-canvas/core';
+
+import first from './scenes/transitions-first?scene';
+import second from './scenes/transitions-second?scene';
+
+export default makeProject({
+ scenes: [first, second],
+});
diff --git a/packages/examples/vite.config.ts b/packages/examples/vite.config.ts
index 51f80336..50bb3de4 100644
--- a/packages/examples/vite.config.ts
+++ b/packages/examples/vite.config.ts
@@ -22,6 +22,7 @@ export default defineConfig({
'./src/media-video.ts',
'./src/components.ts',
'./src/logging.ts',
+ './src/transitions.ts',
],
}),
],