场景层级
场景是动画中显示的节点集合。它们以树状 层级组织,场景视图位于根节点。这个概念类似于 用于表示 HTML 和 XML 文档的文档对象模型。
下面是一个简单场景层级及其对象 表示形式的示例:
view.add(
<>
<Circle />
<Layout>
<Rect />
<Txt>Hi</Txt>
</Layout>
</>,
);
每个节点都是扩展自基础 Node 类的类的实例。
为了让代码更易读,Motion Canvas 使用了自定义的
JSX 运行时。这样,我们
无需自行实例化节点,而是可以编写类似 XML 的标记。请注意,
Motion Canvas 不使用 React 本身,只使用 JSX。这里没有虚拟 DOM,
也没有协调机制,JSX 标签直接映射到 Node 实例。这两段代码
是等价的:
// JSX
view.add(
<>
<Circle />
<Layout>
<Rect />
<Txt>Hi</Txt>
</Layout>
</>,
);
// No JSX
view.add([
new Circle({}),
new Layout({
children: [
new Rect({}),
new Txt({text: 'Hi'}),
],
}),
]);
修改层级
层级创建完成后,仍然可以随时添加、删除和
重新排列节点。Node 类包含
children 和
parent 属性,可用于
遍历树 。但若要修改层级,建议使用
以下辅助方法:
Node.add
public addnode: ComponentChildren: Node
Add the given node(s) as the children of this node.
The nodes will be appended at the end of the children list.
Examples
Parameters
node: ComponentChildrenA node or an array of nodes to append.
Node.insert
Insert the given node(s) at the specified index in the children list.
Examples
Parameters
node: ComponentChildrenA node or an array of nodes to insert.
index: number = 0An index at which to insert the node(s).
Node.remove
public remove(): Node
Remove this node from the tree.
Node.reparent
public reparentnewParent: Node: Node
Change the parent of this node while keeping the absolute transform.
After performing this operation, the node will stay in the same place visually, but its parent will be changed.
Parameters
newParent: NodeThe new parent of this node.
Node.moveUp
public moveUp(): Node
Move the node up in relation to its siblings.
The node will exchange places with the sibling right above it (if any) and from then on will be rendered on top of it.
Node.moveDown
public moveDown(): Node
Move the node down in relation to its siblings.
The node will exchange places with the sibling right below it (if any) and from then on will be rendered under it.
Node.moveToTop
public moveToTop(): Node
Move the node to the top in relation to its siblings.
The node will be placed at the end of the children list and from then on will be rendered on top of all of its siblings.
Node.moveToBottom
public moveToBottom(): Node
Move the node to the bottom in relation to its siblings.
The node will be placed at the beginning of the children list and from then on will be rendered below all of its siblings.
Node.moveTo
public moveToindex: number: Node
Move the node to the provided position relative to its siblings.
If the node is getting moved to a lower position, it will be placed below the sibling that's currently at the provided index (if any). If the node is getting moved to a higher position, it will be placed above the sibling that's currently at the provided index (if any).
Parameters
index: numberThe index to move the node to.
Node.moveAbove
public moveAbovenode: NodedirectlyAbove: boolean = false: Node
Move the node above the provided node in the parent's layout.
The node will be moved above the provided node and from then on will be rendered on top of it. By default, if the node is already positioned higher than the sibling node, it will not get moved.
Parameters
node: NodeThe sibling node below which to move.
directlyAbove: boolean = falseWhether the node should be positioned directly above the sibling. When true, will move the node even if it is already positioned above the sibling.
Node.moveBelow
public moveBelownode: NodedirectlyBelow: boolean = false: Node
Move the node below the provided node in the parent's layout.
The node will be moved below the provided node and from then on will be rendered below it. By default, if the node is already positioned lower than the sibling node, it will not get moved.
Parameters
node: NodeThe sibling node below which to move.
directlyBelow: boolean = falseWhether the node should be positioned directly below the sibling. When true, will move the node even if it is already positioned below the sibling.
Node.removeChildren
public removeChildren(): Node
Remove all children of this node.
查询层级
有时,遍历层级并查找某些特定 节点很有用。在本文档中,我们将这一过程称为_查询_。 请看以下动画:
import ...
export default makeScene2D(function* (view) {
view.add(
<Layout layout gap={20} alignItems={'center'}>
<Txt fill={'white'}>Example</Txt>
<Rect fill={'#f3303f'} padding={20} gap={20}>
<Txt fill={'white'}>42</Txt>
<Circle size={60} fill={'#FFC66D'} />
<Txt fill={'white'}>!!!</Txt>
</Rect>
</Layout>,
);
const texts = view.findAll(is(Txt));
yield* all(...texts.map(text => text.fill('#FFC66D', 1).back(1)));
});
它包含多个文本节点,其颜色在白色和黄色之间
交替。为实现这一点,我们使用 view.findAll(is(Txt)) 搜索
视图节点的所有后代,并只选择 Txt 类型的节点。
传递给 findAll 方法的
第一个参数是所谓的 predicate(谓词)。它是一个接受节点的函数,
如果该节点正是我们要找的节点,则返回 true。
例如,如果想查找所有 x 轴缩放大于 1 的节点,
可以这样写:
const wideNodes = view.findAll(node => node.scale.x() > 1);
知道这一点后,我们可以尝试按如下方式查找所有 Txt 类型的节点:
const texts = view.findAll(node => node instanceof Txt);
但 Motion Canvas 提供了一个名为
is 的实用函数,可以为我们创建这个谓词:
import {is} from '@motion-canvas/2d';
// ...
const texts = view.findAll(is(Txt));
这些谓词可用于任何接受谓词的 JavaScript 函数。
findAll 方法的实现会遍历节点的所有后代,但
如果我们只想查询直接子节点,可以获取
children 数组,并使用我们的谓词
调用内置的 filter 方法:
const textChildren = someParent.children().filter(is(Txt));
根据需求,还可以使用其他几种方法 来查询层级:
Node.findAll
Find all descendants of this node that match the given predicate.
Type Parameters
Parameters
Node.findFirst
Find the first descendant of this node that matches the given predicate.
Type Parameters
Parameters
Node.findLast
Find the last descendant of this node that matches the given predicate.
Type Parameters
Parameters
Node.findAncestor
Find the closest ancestor of this node that matches the given predicate.