跳到主要内容

布局

布局让你可以用 Flexbox 排列节点。 任何继承自 Layout 的节点都可以成为布局的一部分, 包括但不限于 Rect、 Circle 和 Img。

布局根​

布局是一项可选功能,也就是说需要显式启用。 启用方法是在要作为布局根的节点上设置 layout 属性:

// ↓ layout root
<Rect layout>
{/* ↓ layout child */}
<Circle width={320} height={320} />
</Rect>

在上面的示例中,我们将 <Rect> 标记为布局根。 这将使 Flexbox 控制其后代节点的位置和大小 (在本例中只有一个有效的后代:<Circle>)。 布局根本身的处理方式与其子节点不同——其大小由 Flexbox 控制, 但位置不受影响。

仅仅设置 layout 属性并不总能将节点变成布局根。 如果该节点已经是布局的一部分, 它将被当作其他后代节点一样处理:

// ↓ layout root
<Rect layout>
{/* ↓ layout child, NOT a layout root */}
<Rect layout>
{/* ↓ layout child */}
<Circle width={320} height={320} />
</Rect>
</Rect>

大小与偏移​

除了位置、旋转和缩放之外,任何继承自 Layout 类的节点还具有 size 和 offset 属性:

Layout.size​

readonly public size: Vector2LengthSignalLayout

Represents the size of this node.

A size is a two-dimensional vector, where x represents the width, and y represents the height.

The value of both x and y is of type Length which is either:

  • number - the desired length in pixels
  • ${number}% - a string with the desired length in percents, for example '50%'
  • null - an automatic length

When retrieving the size, all units are converted to pixels, using the current state of the layout. For example, retrieving the width set to '50%', while the parent has a width of 200px will result in the number 100 being returned.

When the node is not part of the layout, setting its size using percents refers to the size of the entire scene.

Examples


Layout.offset​

readonly public offset: Vector2SignalLayout

Represents the offset of this node's origin.

By default, the origin of a node is located at its center. The origin serves as the pivot point when rotating and scaling a node, but it doesn't affect the placement of its children.

The value is relative to the size of this node. A value of 1 means as far to the right/bottom as possible. Here are a few examples of offsets:

  • [-1, -1] - top left corner
  • [1, -1] - top right corner
  • [0, 1] - bottom edge
  • [-1, 1] - bottom left corner

基本方位​

布局节点提供了一组辅助属性, 可让你根据自身的边缘/角点来定位。 在下面的示例中,我们用它们将两个方块放在一个灰色矩形的 左右两侧。黄色方块的定位使其右边缘与矩形的左边缘重合。 同时,红色方块的放置使其左下角与矩形的右下角对齐。 所有可能的方位包括: middle、 top、 bottom、 left、 right、 topLeft、 topRight、 bottomLeft 以及 bottomRight。

Press play to preview the animation
import ...

export default makeScene2D(function* (view) {
const rect = createRef<Rect>();

view.add(
<>
<Rect
ref={rect}
width={200}
height={100}
rotation={-10}
fill={'#333333'}
/>
<Rect
size={50}
fill={'#e6a700'}
rotation={rect().rotation}
// Try changing "right" to "top"
right={rect().left}
/>
<Rect
size={100}
fill={'#e13238'}
rotation={10}
bottomLeft={rect().bottomRight}
/>
</>,
);

yield* rect().rotation(10, 1).to(-10, 1);
});

Flexbox 配置​

CSS 中可用的大多数 Flexbox 属性都可作为 Layout 属性 使用。 你可以参阅这份 Flexbox 指南以更好地理解它们的工作原理。 下面列出了最常用的属性:

Layout.padding​

readonly public padding: SpacingSignalLayout

Layout.margin​

readonly public margin: SpacingSignalLayout

Layout.gap​

readonly public gap: Vector2LengthSignalLayout

Layout.direction​

readonly public direction: SimpleSignalFlexDirectionLayout

Layout.alignItems​

readonly public alignItems: SimpleSignalFlexItemsLayout

Layout.justifyContent​

readonly public justifyContent: SimpleSignalFlexContentLayout

组​

不继承自 Layout 类的节点(例如 Node 本身)不受布局影响, 会被当作不存在一样对待。这使你可以对布局节点应用滤镜和变换, 而不会影响层级结构。

从布局的角度来看,下面示例中的所有 <Rect> 都是同级节点:

<Layout direction={'column'} width={960} gap={40} layout>
<Node opacity={0.1}>
<Rect height={240} fill={'#ff6470'} />
<Rect height={240} fill={'#ff6470'} />
</Node>
<Rect height={240} fill={'#ff6470'} />
</Layout>