定位
Motion Canvas 使用笛卡尔坐标系。 其原点位于场景中心, x 轴向右延伸,y 轴向下延伸。
变换
所有节点都是相对于其父节点定位的。 这意味着施加在父节点上的任何变换也会施加到其子节点上。 每个节点的变换由以下属性组成:
Node.position
readonly public position: Vector2SignalNode
Represents the position of this node in local space of its parent.
Examples
Node.scale
readonly public scale: Vector2SignalNode
Represents the scale of this node in local space of its parent.
Examples
Node.rotation
readonly public rotation: SimpleSignalnumberNode
Represents the rotation (in degrees) of this node relative to its parent.
绝对变换
每个基本变换属性都有一个专门 在世界空间中操作的辅助方法。
例如,当我们需要让位于不同父节点下的两个节点的变换保持一致时, 这会很有帮助。请看以下示例:
const circleA = createRef<Node>();
const circleB = createRef<Node>();
view.add(
<>
<Node position={[200, 100]}>
<Circle
position={[0, 100]}
ref={circleA}
width={20}
height={20}
fill={'white'}
/>
</Node>
<Circle ref={circleB} width={10} height={10} fill={'red'} />
</>,
);
circleB().absolutePosition(circleA().absolutePosition());
我们获取 circleA 的绝对位置(即世界空间中的位置),
并将其赋给 circleB 作为绝对位置。
这会使 circleB 正好移动到 circleA 之上。
请注意,我们仍然需要设置 circleB
的 absolutePosition,而不仅仅是 position。由于 circleB
是场景视图的直接子节点,这看起来似乎有些多余。
但场景视图的局部空间并非世界空间。
所有可用的世界空间属性列出如下:
Node.absolutePosition
readonly public absolutePosition: SimpleVector2SignalNode
A helper signal for operating on the position in world space.
Retrieving the position using this signal returns the position in world space. Similarly, setting the position using this signal transforms the new value to local space.
If the new value is a function, the position of this node will be continuously updated to always match the position returned by the function. This can be useful to "pin" the node in a specific place or to make it follow another node's position.
Unlike position, this signal is not compound - it doesn't contain
separate signals for the x and y components.
Node.absoluteScale
readonly public absoluteScale: SimpleVector2SignalNode
A helper signal for operating on the scale in world space.
Retrieving the scale using this signal returns the scale in world space. Similarly, setting the scale using this signal transforms the new value to local space.
If the new value is a function, the scale of this node will be continuously updated to always match the position returned by the function.
Unlike scale, this signal is not compound - it doesn't contain
separate signals for the x and y components.
Node.absoluteRotation
readonly public absoluteRotation: SimpleSignalnumberNode
A helper signal for operating on the rotation in world space.
Retrieving the rotation using this signal returns the rotation in world space. Similarly, setting the rotation using this signal transforms the new value to local space.
If the new value is a function, the rotation of this node will be continuously updated to always match the rotation returned by the function.
矩阵
对于更高级的用途,节点暴露了将向量从一个空间映射到另一个空间所需的全部矩阵。
例如,上面描述的辅助属性可以用 worldToParent
和 localToWorld 矩阵重新实现:
// getting the absolute position:
node.absolutePosition();
// same as:
Vector2.zero.transformAsPoint(node.localToWorld());
// setting the absolute position:
node.absolutePosition(vector);
// same as:
node.position(vector.transformAsPoint(node.worldToParent()));
可用的矩阵包括:
Node.localToWorld
public localToWorld(): DOMMatrix
Get the local-to-world matrix for this node.
This matrix transforms vectors from local space of this node to world space.
Examples
Node.worldToLocal
public worldToLocal(): DOMMatrix
Get the world-to-local matrix for this node.
This matrix transforms vectors from world space to local space of this node.
Examples
Node.localToParent
public localToParent(): DOMMatrix
Get the local-to-parent matrix for this node.
This matrix transforms vectors from local space of this node to local space of this node's parent.
Node.parentToWorld
public parentToWorld(): DOMMatrix
Get the parent-to-world matrix for this node.
This matrix transforms vectors from local space of this node's parent to world space.
Node.worldToParent
public worldToParent(): DOMMatrix
Get the world-to-parent matrix for this node.
This matrix transforms vectors from world space to local space of this node's parent.