Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

docs: add a live flex render and anchor/position/unit diagrams (en + zh)

yhh037400deaf05
diff --git a/docs/astro/src/assets/guides/flex-live.png b/docs/astro/src/assets/guides/flex-live.png
new file mode 100644
index 000000000..60b782ce2
Binary files /dev/null and b/docs/astro/src/assets/guides/flex-live.png differ
diff --git a/docs/astro/src/assets/guides/ui-anchor.svg b/docs/astro/src/assets/guides/ui-anchor.svg
new file mode 100644
index 000000000..01ff61819
--- /dev/null
+++ b/docs/astro/src/assets/guides/ui-anchor.svg
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 378" width="400" height="378" role="img"><text x="122" y="20" fill="#8b91a0" font-size="12" font-weight="600" text-anchor="middle" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,sans-serif">Left</text><text x="232" y="20" fill="#8b91a0" font-size="12" font-weight="600" text-anchor="middle" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,sans-serif">Center</text><text x="342" y="20" fill="#8b91a0" font-size="12" font-weight="600" text-anchor="middle" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,sans-serif">Right</text><text x="62" y="68" fill="#8b91a0" font-size="12" font-weight="600" text-anchor="end" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,sans-serif">Top</text><text x="62" y="150" fill="#8b91a0" font-size="12" font-weight="600" text-anchor="end" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,sans-serif">Middle</text><text x="62" y="232" fill="#8b91a0" font-size="12" font-weight="600" text-anchor="end" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,sans-serif">Bottom</text><rect x="74" y="30" width="96" height="68" rx="6" fill="#14161c" stroke="#2b2f3a" stroke-width="1"/><rect x="81" y="37" width="30" height="22" rx="4" fill="#5b9cf0"/><rect x="184" y="30" width="96" height="68" rx="6" fill="#14161c" stroke="#2b2f3a" stroke-width="1"/><rect x="217" y="37" width="30" height="22" rx="4" fill="#5b9cf0"/><rect x="294" y="30" width="96" height="68" rx="6" fill="#14161c" stroke="#2b2f3a" stroke-width="1"/><rect x="353" y="37" width="30" height="22" rx="4" fill="#5b9cf0"/><rect x="74" y="112" width="96" height="68" rx="6" fill="#14161c" stroke="#2b2f3a" stroke-width="1"/><rect x="81" y="135" width="30" height="22" rx="4" fill="#5b9cf0"/><rect x="184" y="112" width="96" height="68" rx="6" fill="#14161c" stroke="#2b2f3a" stroke-width="1"/><rect x="217" y="135" width="30" height="22" rx="4" fill="#5b9cf0"/><rect x="294" y="112" width="96" height="68" rx="6" fill="#14161c" stroke="#2b2f3a" stroke-width="1"/><rect x="353" y="135" width="30" height="22" rx="4" fill="#5b9cf0"/><rect x="74" y="194" width="96" height="68" rx="6" fill="#14161c" stroke="#2b2f3a" stroke-width="1"/><rect x="81" y="233" width="30" height="22" rx="4" fill="#5b9cf0"/><rect x="184" y="194" width="96" height="68" rx="6" fill="#14161c" stroke="#2b2f3a" stroke-width="1"/><rect x="217" y="233" width="30" height="22" rx="4" fill="#5b9cf0"/><rect x="294" y="194" width="96" height="68" rx="6" fill="#14161c" stroke="#2b2f3a" stroke-width="1"/><rect x="353" y="233" width="30" height="22" rx="4" fill="#5b9cf0"/><text x="62" y="324" fill="#8b91a0" font-size="12" font-weight="600" text-anchor="end" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,sans-serif">Stretch</text><rect x="74" y="286" width="96" height="68" rx="6" fill="#14161c" stroke="#2b2f3a" stroke-width="1"/><rect x="81" y="309" width="82" height="22" rx="4" fill="#54c479"/><text x="122" y="369" fill="#8b91a0" font-size="11" font-weight="400" text-anchor="middle" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,sans-serif">H stretch</text><rect x="184" y="286" width="96" height="68" rx="6" fill="#14161c" stroke="#2b2f3a" stroke-width="1"/><rect x="217" y="293" width="30" height="54" rx="4" fill="#54c479"/><text x="232" y="369" fill="#8b91a0" font-size="11" font-weight="400" text-anchor="middle" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,sans-serif">V stretch</text></svg>
\ No newline at end of file
diff --git a/docs/astro/src/assets/guides/ui-position.svg b/docs/astro/src/assets/guides/ui-position.svg
new file mode 100644
index 000000000..59cfb9459
--- /dev/null
+++ b/docs/astro/src/assets/guides/ui-position.svg
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 458 166" width="458" height="166" role="img"><text x="12" y="18" fill="#cbd0db" font-size="13" font-weight="600" text-anchor="start" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,sans-serif">Relative — in the flex flow</text><rect x="12" y="26" width="200" height="110" rx="8" fill="#14161c" stroke="#2b2f3a" stroke-width="1"/><rect x="26" y="59" width="44" height="44" rx="5" fill="#e8615f"/><rect x="82" y="59" width="44" height="44" rx="5" fill="#54c479"/><rect x="138" y="59" width="44" height="44" rx="5" fill="#5b9cf0"/><text x="246" y="18" fill="#cbd0db" font-size="13" font-weight="600" text-anchor="start" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,sans-serif">Absolute — out of flow</text><rect x="246" y="26" width="200" height="110" rx="8" fill="#14161c" stroke="#2b2f3a" stroke-width="1"/><rect x="260" y="59" width="44" height="44" rx="5" fill="#e8615f"/><rect x="316" y="59" width="44" height="44" rx="5" fill="#54c479"/><rect x="388" y="38" width="46" height="30" rx="5" fill="#f0bd4f"/><text x="411" y="57" fill="#0d0f14" font-size="11" font-weight="700" text-anchor="middle" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,sans-serif">abs</text><text x="12" y="156" fill="#8b91a0" font-size="11" font-weight="400" text-anchor="start" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,sans-serif">siblings share the space</text><text x="246" y="156" fill="#8b91a0" font-size="11" font-weight="400" text-anchor="start" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,sans-serif">pinned by insets; ignores siblings</text></svg>
\ No newline at end of file
diff --git a/docs/astro/src/assets/guides/ui-units.svg b/docs/astro/src/assets/guides/ui-units.svg
new file mode 100644
index 000000000..ba31c9fc6
--- /dev/null
+++ b/docs/astro/src/assets/guides/ui-units.svg
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 428 172" width="428" height="172" role="img"><text x="8" y="38" fill="#cbd0db" font-size="13" font-weight="600" text-anchor="start" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,sans-serif">px(120)</text><text x="8" y="54" fill="#8b91a0" font-size="10" font-weight="400" text-anchor="start" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,sans-serif">fixed 120 px</text><rect x="96" y="20" width="320" height="40" rx="6" fill="#14161c" stroke="#2b2f3a" stroke-width="1"/><rect x="102" y="26" width="120" height="28" rx="4" fill="#e8615f"/><text x="162" y="44" fill="#0d0f14" font-size="11" font-weight="700" text-anchor="middle" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,sans-serif">120px</text><text x="8" y="90" fill="#cbd0db" font-size="13" font-weight="600" text-anchor="start" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,sans-serif">percent(60)</text><text x="8" y="106" fill="#8b91a0" font-size="10" font-weight="400" text-anchor="start" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,sans-serif">60% of parent</text><rect x="96" y="72" width="320" height="40" rx="6" fill="#14161c" stroke="#2b2f3a" stroke-width="1"/><rect x="102" y="78" width="192" height="28" rx="4" fill="#54c479"/><text x="198" y="96" fill="#0d0f14" font-size="11" font-weight="700" text-anchor="middle" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,sans-serif">60%</text><text x="8" y="142" fill="#cbd0db" font-size="13" font-weight="600" text-anchor="start" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,sans-serif">auto</text><text x="8" y="158" fill="#8b91a0" font-size="10" font-weight="400" text-anchor="start" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,sans-serif">content-sized</text><rect x="96" y="124" width="320" height="40" rx="6" fill="#14161c" stroke="#2b2f3a" stroke-width="1"/><rect x="102" y="130" width="74" height="28" rx="4" fill="#5b9cf0"/><text x="139" y="148" fill="#0d0f14" font-size="11" font-weight="700" text-anchor="middle" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,sans-serif">auto</text></svg>
\ No newline at end of file
diff --git a/docs/astro/src/content/docs/guides/ui-layout.mdx b/docs/astro/src/content/docs/guides/ui-layout.mdx
index 71fccaa63..fb830d203 100644
--- a/docs/astro/src/content/docs/guides/ui-layout.mdx
+++ b/docs/astro/src/content/docs/guides/ui-layout.mdx
@@ -25,6 +25,10 @@ interface. See [Screen & Resolution](/docs/core-concepts/screen/) and
## Dimensions: `px`, `percent`, `auto`
+![px is a fixed size, percent is a fraction of the parent, auto is content-sized](../../../assets/guides/ui-units.svg)
+
+*The three length units: `px` is a fixed design-pixel size, `percent` is a fraction of the parent's axis, and `auto` shrinks to content (or is stretched by the container).*
+
Every length field is a `Dimension` — `{ value, unit }` — built with three
helpers:
@@ -109,6 +113,10 @@ CSS-flexbox namesake — the engine runs Yoga, so if you know flexbox, you know
![The four FlexDirection values: Row, RowReverse, Column, ColumnReverse](../../../assets/guides/flex-direction.svg)
+![The same four boxes laid out live in the editor as a Row and as a Column](../../../assets/guides/flex-live.png)
+
+*The same four boxes rendered live in the editor — a `Row` container and a `Column` one. The diagrams here aren't an approximation: they're exactly what the engine draws.*
+
**`justifyContent`** — how children pack along the **main axis**, and how any leftover
space is shared out:
@@ -139,6 +147,10 @@ world.insert(panel, FlexContainer, {
## Absolute positioning
+![Relative nodes flow and share space; an Absolute node is pinned by insets and ignores its siblings](../../../assets/guides/ui-position.svg)
+
+*`Relative` nodes flow in the layout and share the space with their siblings; an `Absolute` node is lifted out of the flow and pinned by its insets, floating over the rest.*
+
`position: Absolute` takes a node out of the flex flow; the four insets place
it against the parent's box, exactly like CSS `left/top/right/bottom`:
@@ -187,6 +199,10 @@ const panel = spawnUIEntity({
## Anchor presets in code
+![The nine point anchors in a 3×3 grid (corners, edge midpoints, centre), plus per-axis Stretch](../../../assets/guides/ui-anchor.svg)
+
+*The anchor picker's nine point anchors — each pins the box to a corner, an edge midpoint, or the centre of its parent — plus a per-axis **Stretch** that makes the box fill that axis.*
+
The editor's anchor picker — and the same API in code — is a set of **shortcuts
that write the box fields above**. An anchor is *not* stored state: the
`position`/inset/margin fields remain the single source of truth, so a preset
diff --git a/docs/astro/src/content/docs/zh-cn/guides/ui-layout.mdx b/docs/astro/src/content/docs/zh-cn/guides/ui-layout.mdx
index 42ca036be..21d5fb6d5 100644
--- a/docs/astro/src/content/docs/zh-cn/guides/ui-layout.mdx
+++ b/docs/astro/src/content/docs/zh-cn/guides/ui-layout.mdx
@@ -20,6 +20,10 @@ UI 布局里的每个 `px` 都是**设计像素**——项目设计分辨率(Pro
## 维度:`px`、`percent`、`auto`
+![px 是固定尺寸,percent 是父项的比例,auto 由内容决定](../../../../assets/guides/ui-units.svg)
+
+*三种长度单位:`px` 是固定的设计像素尺寸,`percent` 是父项对应轴的比例,`auto` 收缩到内容大小(或被容器拉伸)。*
+
每个长度字段都是一个 `Dimension`——`{ value, unit }`——用三个助手构造:
```typescript
@@ -98,6 +102,10 @@ flex 容器沿一条**主轴**(由 `direction` 决定)排布子项,**交叉轴**
![FlexDirection 的四个值:Row、RowReverse、Column、ColumnReverse](../../../../assets/guides/flex-direction.svg)
+![同样四个盒子在编辑器里的真实渲染——一个 Row 容器和一个 Column 容器](../../../../assets/guides/flex-live.png)
+
+*同样四个盒子在编辑器里的真实渲染——一个 `Row` 容器、一个 `Column` 容器。上面的示意图不是近似:引擎画出来就是这样。*
+
**`justifyContent`** —— 子项沿**主轴**如何排布,以及多出来的空间如何分配:
![JustifyContent 的六个值:Start、Center、End、SpaceBetween、SpaceAround、SpaceEvenly](../../../../assets/guides/flex-justify.svg)
@@ -126,6 +134,10 @@ world.insert(panel, FlexContainer, {
## 绝对定位
+![Relative 节点参与排布、与兄弟共享空间;Absolute 节点由内偏移钉住、忽略兄弟](../../../../assets/guides/ui-position.svg)
+
+*`Relative` 节点参与排布、与兄弟共享空间;`Absolute` 节点被抬出排布流、由内偏移(inset)钉住,浮在其余内容之上。*
+
`position: Absolute` 使节点脱离 flex 流;四个内偏移相对父项的盒子放置它,与 CSS
`left/top/right/bottom` 完全一致:
@@ -168,6 +180,10 @@ const panel = spawnUIEntity({
## 代码里的锚点预设
+![九个点锚排成 3×3 网格(四角、边中点、中心),外加每轴的 Stretch](../../../../assets/guides/ui-anchor.svg)
+
+*锚点选择器的九个点锚——各自把盒子钉到父项的某个角、边中点或正中心——外加每轴一个 **Stretch**,让盒子沿该轴铺满。*
+
编辑器的锚点选择器——以及代码里的同一套 API——是一组**写入上述盒字段的快捷方式**。
锚点*不是*存储的状态:`position`/内偏移/外边距字段始终是唯一权威源,因此预设与实际
布局永远不会失同步。每个轴是四种模式之一: