← 返回
未分类 中文

Figma Plugin Writer

自动生成和更新 Figma 插件的 code.js 以创建和修改设计元素,用户更新代码后需手动运行插件执行。
自动生成和更新 Figma 插件的 code.js 以创建和修改设计元素,用户更新代码后需手动运行插件执行。
zerozlw zerozlw 来源
未分类 clawhub v1.0.0 1 版本 100000 Key: 无需
★ 0
Stars
📥 693
下载
💾 159
安装
1
版本
#design#figma#latest#plugin

概述

Figma Plugin Writer

Write plugin code (code.js) to automate design in Figma files. After updating the code, notify the user to run the plugin.

Prerequisites

The user must provide:

  1. Plugin directory — folder containing code.js and manifest.json
  2. Code file — typically code.js
  3. Target Figma file — optional, for context

Workflow

  1. Receive design requirements from the user
  2. Write plugin code to code.js
  3. Notify user: Plugins → Development →
  4. Iterate based on feedback

API Reference

Font Loading (required before creating text)

await figma.loadFontAsync({ family: "Inter", style: "Regular" });
await figma.loadFontAsync({ family: "Inter", style: "Medium" });
await figma.loadFontAsync({ family: "Inter", style: "Semi Bold" });
await figma.loadFontAsync({ family: "Inter", style: "Bold" });

Frame (Container)

var frame = figma.createFrame();
frame.name = "MyFrame";
frame.resize(width, height);
frame.cornerRadius = 12;
frame.fills = [{ type: "SOLID", color: { r: 1, g: 1, b: 1 } }];
frame.x = 0;
frame.y = 0;

Text

var text = figma.createText();
text.characters = "Hello World";
text.fontSize = 16;
text.fontName = { family: "Inter", style: "Regular" };
text.fills = [{ type: "SOLID", color: { r: 0, g: 0, b: 0 } }];

Rectangle

var rect = figma.createRectangle();
rect.resize(100, 50);
rect.fills = [{ type: "SOLID", color: { r: 0.9, g: 0.2, b: 0.2 } }];
rect.cornerRadius = 8;

Nesting Nodes

frame.appendChild(text);
parentPage.appendChild(frame);

Drop Shadow

frame.effects = [{
  type: "DROP_SHADOW",
  color: { r: 0, g: 0, b: 0, a: 0.1 },
  offset: { x: 0, y: 4 },
  radius: 12,
  spread: 0,
  visible: true,
  blendMode: "NORMAL",
}];

Stroke

frame.strokes = [{ type: "SOLID", color: { r: 0.8, g: 0.8, b: 0.8 } }];
frame.strokeWeight = 1;

Text Alignment

text.textAlignHorizontal = "CENTER"; // LEFT | CENTER | RIGHT | JUSTIFIED
text.textAlignVertical = "CENTER";   // TOP | CENTER | BOTTOM

Auto Layout (inside Frame)

frame.layoutMode = "VERTICAL"; // or "HORIZONTAL"
frame.primaryAxisAlignItems = "CENTER";   // MIN | CENTER | MAX | SPACE_BETWEEN
frame.counterAxisAlignItems = "CENTER";
frame.paddingTop = 16;
frame.paddingBottom = 16;
frame.paddingLeft = 16;
frame.paddingRight = 16;
frame.itemSpacing = 8;

Page Navigation

var pages = figma.root.children;
var targetPage = pages[pages.length - 1];
await figma.setCurrentPageAsync(targetPage);

Clear Page Content

var old = targetPage.children.slice();
for (var i = 0; i < old.length; i++) {
  old[i].remove();
}

Viewport

figma.viewport.scrollAndZoomIntoView([frame]);

Notifications

figma.notify("Done!", { timeout: 3000 });
figma.notify("ERROR: " + e.message, { timeout: 10000 });

Page Info

var pages = figma.root.children;
var count = pages.length;
var pageName = pages[0].name;

Code Template

async function main() {
  try {
    // 1. Load fonts
    await figma.loadFontAsync({ family: "Inter", style: "Regular" });
    await figma.loadFontAsync({ family: "Inter", style: "Bold" });

    // 2. Get target page
    var pages = figma.root.children;
    var target = pages[pages.length - 1];
    await figma.setCurrentPageAsync(target);

    // 3. Clear old content
    var old = target.children.slice();
    for (var i = 0; i < old.length; i++) old[i].remove();

    // 4. Create design...
    var frame = figma.createFrame();
    frame.name = "Screen";
    frame.resize(375, 812);
    frame.fills = [{ type: "SOLID", color: { r: 1, g: 1, b: 1 } }];
    frame.x = 0;
    frame.y = 0;
    target.appendChild(frame);

    // 5. Done
    figma.viewport.scrollAndZoomIntoView([frame]);
    figma.notify("Design complete!", { timeout: 3000 });

  } catch (e) {
    figma.notify("ERROR: " + e.message, { timeout: 10000 });
  }
}

main();

Pitfalls & Gotchas

documentAccess: "dynamic-page" Mode

If manifest.json has "documentAccess": "dynamic-page":

  • figma.currentPage = page → ✅ await figma.setCurrentPageAsync(page)
  • figma.getNodeById() → ✅ await figma.getNodeByIdAsync()
  • figma.closePlugin() → ✅ await figma.closePluginAsync()

Error Handling

  • Always wrap code in try-catch
  • Show errors via figma.notify("ERROR: " + e.message, { timeout: 10000 })
  • Do not use emoji in text content (font may not support the glyph)
  • Do not call figma.closePlugin() automatically (let user close manually)

Free Tier Limits

  • Max 3 Pages — do not create new Pages
  • Work within existing Pages (clear and rebuild)

Fonts

  • Default: Inter (built into Figma)
  • Supported styles: "Regular", "Medium", "Semi Bold", "Bold"
  • Colors use { r, g, b } format with 0-1 range

Iteration Pattern

On each design iteration:

  1. Clear old elements: page.children.slice().forEach(c => c.remove())
  2. Recreate design with changes
  3. Notify user to re-run the plugin

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-03-30 02:22 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

dev-programming

YouTube

byungkyu
使用托管OAuth集成YouTube Data API,支持搜索视频、管理播放列表、获取频道数据及评论互动,适用于用户需要时使用此技能。
★ 142 📥 42,170
dev-programming

Mcporter

steipete
使用 mcporter CLI 直接列出、配置、认证及调用 MCP 服务器/工具(支持 HTTP 或 stdio),涵盖临时服务器、配置编辑及 CLI/类型生成功能。
★ 198 📥 68,306
dev-programming

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 687 📥 331,426