Figma 设计稿还原精度提升方案(Design-to-Code 平台)
一、平台现状与核心瓶颈
在现有实现中,平台已经具备「Figma → DSL →(AI/规则引擎)→ Vue 3 + Tailwind」的完整流水线,但还原精度受限于以下几点:
1. DSL 信息严重不完整(最核心问题)
figmaParser.ts 在从 Figma JSON 到 DSL 的过程中丢弃了大量关键字段:
-
视觉相关
-
opacity/blendMode:透明度、混合模式丢失 -
effects(DROP_SHADOW/INNER_SHADOW/LAYER_BLUR等):阴影、毛玻璃、模糊无法还原 - 图片类型
fills:背景图/图片节点缺乏清晰标记
-
-
布局与尺寸相关
-
primaryAxisSizingMode/counterAxisSizingMode:HUG/FILL/FIXED未保留,所有尺寸退化为固定像素 -
layoutGrow:flex-grow行为缺失,子元素无法按设计拉伸 -
layoutAlign:子元素在交叉轴上的对齐(如STRETCH)丢失 -
layoutWrap/counterAxisSpacing:无法生成flex-wrap和多行间距 -
clipsContent:是否需要overflow: hidden缺失 -
minWidth/maxWidth等约束信息缺失
-
-
边框与描边
-
strokeWeight/strokeAlign/strokes:border 宽度、位置、颜色无法重建
-
-
文本排版
-
lineHeightPx/lineHeightUnit:行高信息未充分利用 -
fontFamily/italic/textDecoration:字体族、斜体、下划线等缺失
-
-
设计系统相关
-
variables(Figma Variables):设计 Token 无法映射到 CSS 变量 / Tailwind Token
-
目前使用 GET /v1/files/:fileKey 拉整文件,对复杂文件既低效又噪声多,更适合改为:
-
GET /v1/nodes/:fileKey?ids=:nodeId:只拉取目标节点及其子树 → 更精准、更轻量。
2. AI Prompt 工程过于简单
现有 DEFAULT_SYSTEM_PROMPT 主要是风格与输出格式约束,缺少:
- DSL 字段的语义说明(AI 不知道每个数值字段真正含义与用法)
- few-shot 示范(典型卡片、导航栏、表单等模式的“DSL → 代码”示例)
- 对
layoutGrow、clipsContent、effects、sizing mode 等关键字段的明确使用规则 - 更高的上下文容量(
max_tokens仅 4096,对复杂页面偏紧)
3. 规则引擎覆盖面窄,更多只是兜底
当前 vueGenerator.ts 仅支持 Auto Layout → Flex 的简单映射:
- 非 Auto Layout(绝对定位)节点缺乏支持
- 未覆盖阴影、渐变、图片填充、描边等效果
- 节点一律
<div>/<span>,缺少语义标签(button/nav/header/main/section等)
整体来看,架构路径是对的,但输入信号(DSL + Prompt)远没有把 Figma 设计中的关键信息“喂”充分。
二、改进建议(按优先级排序)
1. 视觉引用 + 多模态 AI(最高 ROI)
目标:让模型既能“看图”,又能“读数据”。
当前 AI 仅接收 DSL JSON,对复杂视觉效果(层级关系、对比度、留白、视觉节奏)很难仅凭数字精确还原。建议:
- 从 Figma 同时获取:
-
GET /v1/nodes/:fileKey?ids=:nodeId→ 精准节点 DSL(树) -
GET /v1/images/:fileKey?ids=:nodeId→ 节点截图 PNG(或整帧截图)
-
- 后端调用多模态模型(如 GPT-4o、Claude 3.5 Sonnet 等)时:
- system:详细生成规范 + DSL 字段说明
- user(image):Figma 节点截图,让模型“看到”真实设计
- user(text):DSL JSON,让模型“读懂”精确尺寸与属性
这可以显著提升:
- 层叠关系、阴影强度、圆角/留白的整体节奏
- 文本与图标的对齐细节
- 复杂卡片、Dashboard、表格类 UI 的整体布局
这是在不大改整体架构前提下,单点收益最大的提升手段。
2. 修复与扩充 DSL 信息提取
在 figmaParser.ts 中扩展 toDSLNode,尽量无损保留与视觉/布局强相关的字段,例如(伪代码):
// 尺寸模式(HUG / FILL / FIXED) if (node.primaryAxisSizingMode) dsl.primaryAxisSizingMode = node.primaryAxisSizingMode if (node.counterAxisSizingMode) dsl.counterAxisSizingMode = node.counterAxisSizingMode if (node.layoutGrow !== undefined) dsl.layoutGrow = node.layoutGrow if (node.layoutAlign) dsl.layoutAlign = node.layoutAlign
// 透明度与效果 if (node.opacity !== undefined && node.opacity !== 1) dsl.opacity = node.opacity if (node.effects?.length) dsl.effects = node.effects if (node.clipsContent) dsl.clipsContent = true
// 描边 if (node.strokes?.length && node.strokeWeight) { dsl.strokeWeight = node.strokeWeight dsl.strokeAlign = node.strokeAlign }
// 文本扩展 if (node.type === 'TEXT' && node.style) { dsl.textStyle = { ...dsl.textStyle, lineHeightPx: node.style.lineHeightPx, lineHeightUnit: node.style.lineHeightUnit, fontFamily: node.style.fontFamily, italic: node.style.italic, textDecoration: node.style.textDecoration, } }
// 布局换行 if (node.layoutWrap) dsl.layoutWrap = node.layoutWrap if (node.counterAxisSpacing) dsl.counterAxisSpacing = node.counterAxisSpacing