跪拜 Guibai
← All articles
Frontend · Backend · Agent

From Figma to pixel-perfect: an AI agent that restores pages and reviews its own work

By 不一样的少年_ ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

For teams doing AI-assisted frontend work, this closes the last loop that usually stays manual, the pixel-level acceptance check. The cost of visual regression is no longer a developer staring between two screens, but an agent looping until the diff converges.

Summary

A Chinese frontend developer has been using a self-built AI Skill that replaces the last manual chore of web page restoration: visually comparing the browser result against the Figma design and adjusting pixel offsets. The Skill takes a single Figma node and a local page URL, then runs a closed loop: read the design structure through Figma MCP, generate the page code, open the page in a controllable browser, screenshot it under fixed viewport and DPR conditions, align the screenshot with the design reference using Sharp, generate overlay and diff images, then inspect the DOM box model and element occlusion to find the real cause before changing code.

The workflow codifies several frontend heuristics into rules for the AI. Design baseline and slicing multipliers are computed separately, so a 360px or 720px artboard produces correct CSS sizes and 2x/1x exports. Layers are split by whether content changes, needs interaction, or is purely decorative: dynamic text and clickable areas go into real DOM, while complex visuals become image assets. Diff images mark few-pixel deviations as red patches, which a vision-based AI can actually perceive, and the agent is told to debug like a human would, using getBoundingClientRect, overflow checks, and elementsFromPoint, rather than blindly nudging margins.

The author deliberately ships no code. They argue that current AI models can write the required scripts and rules themselves once you describe each step, so teams can rebuild the same pipeline against their own tech stack. The article details the four required components, common failure modes like black edges from exporting canvas backgrounds, and the stop-loss rule that forces the agent to stop grinding after two failed fix rounds.

Takeaways
— The Skill requires four components: a coding agent (Codex, Claude Code, or Cursor), Figma MCP, a browser that can screenshot with login state (EGO Lite), and Sharp plus a comparison script.
— The workflow reads a Figma node, writes the page, screenshots in a fixed viewport and DPR, aligns the screenshot with the design using Sharp, generates overlay and diff images, inspects the DOM for root causes, then fixes and re-screenshots until the diff converges.
— CSS dimensions and export multipliers must be calculated from separate baselines: a 360px design exports at 2x for high-DPI screens, while a 720px design uses 1x exports and divides CSS measurements by 2.
— Layer splitting rule: dynamic text and clickable areas must be real DOM; only decorative visuals that never change or receive interactions should be exported as a full-module image.
— A common bug is exporting from Figma with the dark canvas background baked into the image, which produces black edges on rounded corners that CSS border-radius cannot remove.
— Directly presenting two separate images to a vision AI is unreliable for sub-5px deviations; overlaying and highlighting differences in red makes the mismatch visible.
— The agent should debug like a human: measure with getBoundingClientRect(), trace overflow: hidden ancestors, and run elementsFromPoint() to find occluding layers before touching styling.
— Stop-loss rule: after two consecutive fixing rounds without convergence, the agent must stop and check the design size, font loading, or page state instead of continuing pixel chasing.
— No source code is published because current AI models can write the tools and rules themselves if the developer describes the steps clearly.
Conclusions

The most time-consuming part of AI-assisted page development is shifting from writing code to verifying code; this Skill treats visual acceptance as an automated loop, which is a more mature use of AI than one-shot prompt generation.

Relying on AI's direct image comparison fails for sub-pixel deviations, so the workflow reintroduces a deterministic image-processing step with Sharp; the hybrid of deterministic diffing and AI-driven diagnosis is what makes the loop reliable.

The workflow turns known frontend debugging heuristics into explicit agent rules, which suggests that prompt engineering for UI work is mostly encoding human inspection patterns, not inventing new ones.

The 'no code package' stance reflects a practical observation: agent tooling has advanced enough that rules and scripts are just artifacts an AI can generate from a description, making the workflow more about process design than implementation.

Concepts & terms
Figma MCP
A Model Context Protocol server that lets an AI agent read the structure, artboard dimensions, layer properties, and assets of a Figma design node, instead of guessing from a static screenshot.
Screenshot diffing
A technique where two images are aligned to the same dimensions and overlaid, with mismatched pixels highlighted in a diff image, making sub-pixel visual differences detectable even by a vision-based AI.
DPR (device pixel ratio)
The ratio between physical device pixels and CSS pixels on a screen. At DPR 2, an icon displayed at 40 CSS pixels needs an 80×80 physical pixel image to stay sharp.
EGO Lite
A Chrome-based browser tool that retains a persistent login state so an AI agent can open authenticated pages and screenshot them without disrupting the developer's own browser session.
elementsFromPoint()
A browser API that returns all elements at a given screen coordinate, used by the agent to detect invisible overlays or z-index issues that block clicks.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗