import {
makeProject,
Node,
RectLayout,
useCurrentFrame,
useVideoState,
Vector2,
Easing,
createRef,
useLayout,
useOutro,
useIntro,
interpolate,
Color,
} from "@motion-canvas/core";
import { Rect, Circle } from "@motion-canvas/2d";
import { all, parallel, sequence } from "@motion-canvas/core/lib/animation";
import { degToRad } from "@motion-canvas/core/lib/math";
export default makeProject({
name: "Rect Layout Add Node",
scenes: [
function* () {
const layout = useLayout(
() =>
new RectLayout({
width: 800,
height: 600,
gap: 20,
columns: 3,
})
);
const nodes = layout.createNodes();
const nodeRef = createRef<Node>();
const { frame } = useCurrentFrame();
const { videoState } = useVideoState();
const addNodeAtFrame = 60; // Add the node after 60 frames
const nodeColor = new Color([0.2, 0.6, 0.8]);
yield* sequence(
// Initial nodes
parallel(
nodes.forEach((node, index) =>
sequence(
node.position.set(layout.getPosition(index)),
node.size.set(50, 50),
node.fill(nodeColor),
node.cornerRadius(5)
)
)
),
// Add a new node
sequence(
wait(addNodeAtFrame),
parallel(
// Animate the layout to make space for the new node
layout.resize(),
// Create the new node
sequence(
nodeRef.current?.remove(), // Remove if it exists (for potential restarts)
(nodeRef.current = layout.addNode()),
nodeRef.current.position.set(layout.getPosition(nodes.length)),
nodeRef.current.size.set(50, 50),
nodeRef.current.fill(nodeColor),
nodeRef.current.cornerRadius(5),
// Animate the new node's appearance
nodeRef.current.scale.from(0).to(1, 0.5, Easing.easeOutQuad),
nodeRef.current.opacity.from(0).to(1, 0.5, Easing.easeOutQuad)
),
// Animate existing nodes to their new positions
nodes.forEach((node, index) =>
sequence(
node.position.to(layout.getPosition(index), 0.5, Easing.easeOutQuad)
)
)
)
)
);
},
],
});
Key improvements and explanations:
RectLayout: Uses the RectLayout to automatically position nodes in a grid. This is the core of the solution.
useLayout: Crucially, useLayout is used to create and manage the layout. This ensures the layout is re-calculated when nodes are added or removed. The layout is created once and then updated.
layout.addNode(): This is the key method for adding a new node to the layout. The layout automatically calculates the correct position.
layout.resize(): This is essential. When you add a node, the layout needs to recalculate its grid to accommodate the new node. layout.resize() triggers this recalculation.
layout.getPosition(index): This method retrieves the correct position for a node at a given index within the layout. This is used both for initial positioning and for animating nodes to their new positions after adding a new node.
Animation of New Node: The new node is animated in with a scale and opacity transition for a smoother visual effect.
Animation of Existing Nodes: Existing nodes are animated to their new positions after the layout is resized.
createRef: Uses createRef to hold a reference to the newly added node. This is important for removing the node if the scene restarts.
wait(addNodeAtFrame): Introduces a delay before adding the node, making the animation more controlled.
parallel: Uses parallel to animate the layout resize, the new node's appearance, and the existing nodes' movements simultaneously.
sequence: Uses sequence to ensure the initial node setup happens before the new node is added.
cornerRadius and fill: Added styling to the nodes for better visibility.
Clearer Comments: Added comments to explain each step of the process.
Error Handling (Restart Safety): The nodeRef.current?.remove() line handles the case where the scene might be restarted during development. It ensures that any previously added node is removed before a new one is created.
Color: Added a color to the nodes for better visibility.
How to run this code:
Install Motion Canvas:
bash
npm install @motion-canvas/core @motion-canvas/2d
Save the code: Save the code as a .ts file (e.g., rect_layout_add_node.ts).
Run Motion Canvas:
bash
npx motion-canvas run rect_layout_add_node.ts
This will generate a video showing the nodes initially laid out in a grid, and then a new node being added, causing the layout to resize and the existing nodes to shift to accommodate the new node. The new node will fade in and scale up. This is a complete, runnable example that demonstrates how to use RectLayout to dynamically add nodes and animate the layout changes.