vertical frame + 每层宽度递减。gap 4px 保持紧密。
layout: "vertical" + alignItems: "center"triangle(topWidth: 0),中间和底层用 trapezoid。严格的斜率算法(必须在脚本中实现): 要让金字塔的侧边形成一条完美的直线,宽度的增量必须与高度和 gap 严格挂钩。
- 设定整体宽度扩张系数
angleK(建议值 1.5 到 2.5,表示高度每增加1px,总宽度增加的像素数)。- 当前层的底宽公式:
width = topWidth + (height * angleK)- 下一层的顶宽公式(必须考虑 gap 带来的额外外扩):
nextTopWidth = width + (gap * angleK)
必须使用 node 运行脚本生成 JSON。
const fs = require('fs');
// 1. 配置基础参数
const GAP = 4;
const ANGLE_K = 2; // 斜率系数:高度每增加1px,宽度增加2px
const LAYER_HEIGHT = 80;
const data = [
{ text: "顶层核心", fillColor: "#1F2329", textColor: "#FFFFFF" },
{ text: "中间层 B", fillColor: "#DFF5E5", textColor: "#1F2329" },
{ text: "中间层 A", fillColor: "#EAE2FE", textColor: "#1F2329" },
{ text: "最底层基础", fillColor: "#F0F4FC", textColor: "#1F2329" }
];
let currentTopWidth = 0; // 顶层如果是尖角,初始为 0
const children = data.map((layer, index) => {
// 2. 根据公式计算当前层的底宽
const currentBottomWidth = currentTopWidth + (LAYER_HEIGHT * ANGLE_K);
const node = {
type: currentTopWidth === 0 ? "triangle" : "trapezoid",
width: currentBottomWidth,
topWidth: currentTopWidth,
height: LAYER_HEIGHT,
text: layer.text,
textAlign: "center",
fillColor: layer.fillColor,
borderColor: layer.fillColor,
borderWidth: 2,
fontSize: 16,
textColor: layer.textColor
};
// 3. 关键:计算下一层的顶宽。必须把 gap 的延伸也算进去!
currentTopWidth = currentBottomWidth + (GAP * ANGLE_K);
return node;
});
const output = {
version: 2,
nodes: [
{
type: "frame",
layout: "vertical",
alignItems: "center",
gap: GAP,
padding: 40,
children: children
}
]
};
fs.writeFileSync('diagram.json', JSON.stringify(output, null, 2));
angleK 公式计算。topWidth 只是简单等于上一层的 width,在有 gap 的情况下,衔接处会产生锯齿折角。必须加上 gap * angleK。\n 手动换行;长文案外置到金字塔旁边(外层套 horizontal frame,金字塔左侧,说明文字右侧)scenes/funnel.mdlayout: "horizontal" 的 frame,金字塔放左侧,说明文字(vertical 排列的 text 节点)放右侧