How the ZIM Master Prompt Solves AI Code Hallucinations for 2D Canvas

How the ZIM Master Prompt Solves AI Code Hallucinations for 2D Canvas

ZIM Master Prompt 如何解决 2D Canvas 开发中的 AI 代码幻觉问题

PROMPTED by Dr Abstract - organized by Gemini. If you have ever asked a Large Language Model (LLM) to write code for a specialized or evolving framework, you have likely encountered the “Legacy Fallback” trap: Instead of using the latest idioms, the AI defaults to outdated patterns it saw millions of times in older training data. For ZIM (the JavaScript canvas framework for creative coding), generic LLM queries often hallucinate obsolete Flash or raw CreateJS code—writing messy stage.addChild() calls, manual coordinate math, and unnecessary ticker loops. To fix this, the ZIM team created the ZIM Master Prompt at https://zimjs.com/prompt which is a structured system prompt backed by two lightweight, AI-optimized reference URLs: docs_ai.php and tips.html. Here is why this approach works so effectively and how it transforms AI code generation from broken boilerplate into clean, idiomatic canvas code.

由 Dr Abstract 提示,Gemini 整理。如果你曾要求大语言模型(LLM)为某个专业或不断演进的框架编写代码,你很可能遇到过“遗留回退”(Legacy Fallback)陷阱:AI 没有使用最新的惯用法,而是默认使用它在旧训练数据中见过数百万次的过时模式。对于 ZIM(用于创意编程的 JavaScript Canvas 框架)而言,通用的 LLM 查询经常会产生过时的 Flash 或原始 CreateJS 代码幻觉——写出混乱的 stage.addChild() 调用、手动坐标计算以及不必要的 ticker 循环。为了解决这个问题,ZIM 团队创建了 ZIM Master Prompt(网址:https://zimjs.com/prompt),这是一个结构化的系统提示词,并由两个轻量级、针对 AI 优化的参考链接提供支持:docs_ai.phptips.html。以下是这种方法为何如此有效,以及它如何将 AI 代码生成从破碎的样板文件转变为简洁、地道的 Canvas 代码。

1. The Core Challenge: Why LLMs Struggle with Canvas Frameworks

1. 核心挑战:为什么 LLM 在处理 Canvas 框架时会遇到困难

LLMs excel at standard HTML, CSS, and basic React because the web is flooded with examples. However, for specialized interactive frameworks, LLMs face three distinct hurdles:

  • Parameter Confusion: Display classes often take 5+ arguments. Without active documentation, LLMs guess parameter orders and default values.
  • Framework Drift: Because ZIM is built on top of CreateJS, an unguided AI defaults to raw createjs methods rather than ZIM’s high-level abstractions.
  • Context Window Bloat: Full HTML documentation contains thousands of lines of navigation menus, CSS styling, and DOM noise that overwhelm an AI’s context window.

LLM 在处理标准 HTML、CSS 和基础 React 时表现出色,因为网络上充斥着相关示例。然而,对于专业的交互式框架,LLM 面临三个明显的障碍:

  • 参数混淆: 显示类通常带有 5 个以上的参数。如果没有实时的文档支持,LLM 会猜测参数顺序和默认值。
  • 框架漂移: 由于 ZIM 是构建在 CreateJS 之上的,未经引导的 AI 会默认使用原始的 CreateJS 方法,而不是 ZIM 的高级抽象。
  • 上下文窗口臃肿: 完整的 HTML 文档包含数千行的导航菜单、CSS 样式和 DOM 噪音,这些会占用并淹没 AI 的上下文窗口。

2. The Solution: The Two AI Reference Pillars

2. 解决方案:两个 AI 参考支柱

The ZIM Master Prompt gives the AI immediate access to two distilled, machine-friendly resources:

  • 1. docs_ai.php (Machine-Readable API Map): Instead of feeding full web pages to the model, docs_ai.php delivers a dense, stripped-down summary of modules, classes, and methods:
    • Exact Parameter Signatures: Eliminates parameter guessing.
    • DUO Identification: Tells the AI when methods support configuration objects ({props, time, call}).
    • VEE Flags: Informs the AI which parameters accept dynamic lazy evaluations (e.g., random arrays [red, blue], series(), min/max ranges {min: 10, max: 50}, or functions).
  • 2. tips.html (Idiomatic Style Rules): Having API docs alone isn’t enough; the AI needs to know how to write code like the framework’s creator (Dr. Abstract):
    • Enforces chainable 3-letter helpers (.sca(), .rot(), .alp(), .loc()).
    • Replaces raw coordinate calculations with declarative layout methods (.centerReg(), .pos(0, 0, RIGHT, BOTTOM)).
    • Prevents unnecessary Ticker loops by letting animate(), drag(), and UI components manage stage updates automatically.
    • Mandates built-in convenience utilities (loop() over for, rand() over Math.random(), built-in color constants like red and blue).

ZIM Master Prompt 让 AI 能够即时访问两个精简的、机器友好的资源:

  • 1. docs_ai.php(机器可读的 API 映射): docs_ai.php 不会将整个网页喂给模型,而是提供了一份密集、精简的模块、类和方法摘要:
    • 精确的参数签名: 消除了参数猜测。
    • DUO 识别: 告诉 AI 哪些方法支持配置对象({props, time, call})。
    • VEE 标记: 告知 AI 哪些参数接受动态延迟求值(例如:随机数组 [red, blue]、series()、最小/最大范围 {min: 10, max: 50} 或函数)。
  • 2. tips.html(惯用风格规则): 仅有 API 文档是不够的;AI 需要知道如何像框架创建者(Dr. Abstract)那样编写代码:
    • 强制使用可链式调用的 3 字母辅助方法(.sca(), .rot(), .alp(), .loc())。
    • 用声明式布局方法(.centerReg(), .pos(0, 0, RIGHT, BOTTOM))取代原始坐标计算。
    • 通过让 animate()、drag() 和 UI 组件自动管理舞台更新,防止不必要的 Ticker 循环。
    • 强制使用内置的便捷工具(用 loop() 代替 for,用 rand() 代替 Math.random(),使用内置颜色常量如 red 和 blue)。

3. The Difference: Without vs. With the Master Prompt

3. 区别:使用与不使用 Master Prompt 的对比

Let’s look at what happens when you ask an AI to create a simple interactive test: Create a centered red rectangle, animate it to 2× scale, and place it at the bottom-right on click.

让我们看看当你要求 AI 创建一个简单的交互式测试时会发生什么:创建一个居中的红色矩形,将其缩放至 2 倍,并在点击时将其放置在右下角。

❌ Without Master Prompt (Generic Hallucinated Code) ❌ 不使用 Master Prompt(通用的幻觉代码) The AI falls back to generic canvas boilerplate, manual math, and outdated event loops: AI 回退到通用的 Canvas 样板代码、手动数学计算和过时的事件循环:

// Clunky, imperative, and misses built-in conveniences
var rect = new createjs.Shape();
rect.graphics.beginFill("red").drawRect(-50, -50, 100, 100);
rect.x = stage.canvas.width / 2;
rect.y = stage.canvas.height / 2;
stage.addChild(rect);
createjs.Tween.get(rect)
  .to({ scaleX: 2, scaleY: 2 }, 1000)
  .call(function() {
    rect.on("mousedown", function() {
      rect.x = stage.canvas.width - 100;
      rect.y = stage.canvas.height - 100;
      stage.update();
    });
  });
createjs.Ticker.on("tick", stage);

✅ With the ZIM Master Prompt (Idiomatic, Clean, Expressive) ✅ 使用 ZIM Master Prompt(地道、简洁、富有表现力) With the master prompt and rules active, the AI produces pure, chainable ZIM: 在启用 Master Prompt 和规则后,AI 生成了纯净、可链式调用的 ZIM 代码:

// Clean, declarative, and 65% shorter
new Rectangle(100, 100, red)
  .centerReg()
  .animate({
    props: { scale: 2 },
    time: 1,
    call: (target) => {
      target.on("mousedown", () => {
        target.pos(0, 0, RIGHT, BOTTOM);
        S.update();
      });
    }
  });

4. Key Takeaways for Framework Authors & Developers

4. 给框架作者和开发者的关键启示

The success of the ZIM Master Prompt reveals a powerful blueprint for any open-source library or framework:

  • Provide an /ai Documentation Endpoint: Create a plain-text, token-dense API cheat sheet (like docs_ai.php) specifically designed for LLMs to read in seconds.
  • Explicitly Teach Conventions: LLMs don’t just need to know what exists; they need strict rules telling them what NOT to do (e.g., “Do not use addChild(), use .center() or .pos()”).
  • Enforce Single-File Outputs: Instructing the AI to deliver ready-to-run, self-contained HTML templates eliminates setup friction for developers testing prototypes.

ZIM Master Prompt 的成功为任何开源库或框架揭示了一个强大的蓝图:

  • 提供 /ai 文档端点: 创建一个纯文本、Token 密度高的 API 速查表(如 docs_ai.php),专门设计供 LLM 在几秒钟内读取。
  • 明确教授约定: LLM 不仅需要知道存在什么,还需要严格的规则告诉它们“不要做什么”(例如:“不要使用 addChild(),请使用 .center() 或 .pos()”)。
  • 强制单文件输出: 指导 AI 提供可直接运行、自包含的 HTML 模板,消除了开发者测试原型时的设置阻力。

Try It Out

尝试一下

Want to test AI-assisted creative coding with ZIM?

想要测试 ZIM 的 AI 辅助创意编程吗?

Have you created AI prompts or machine-readable docs for your favorite libraries? Share your workflow in the comments below! 你是否为你最喜欢的库创建过 AI 提示词或机器可读文档?欢迎在下方评论区分享你的工作流程!