跪拜 Guibai
← Back to the summary

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

Cover image of going from Figma design to page implementation, screenshot comparison, and correction re-verification

I've been quietly using this Figma page restoration Skill for almost half a year.

To be honest, most people now use AI to write pages, but often they've just switched from "slice cutter" to "AI supervisor" — the code is indeed generated by AI, but in the end we still have to stare at the screen ourselves, switching between the browser on the left and the design mock on the right to find faults, then type humbly in the dialog: "Bro, the title is off by 2px down", "The button background is stretched and deformed, change it again".

After using this Skill, I feel that frontend brothers can finally graduate from this kind of work: just throw the Figma node and local page address to the AI, and it can complete the whole pipeline by itself — from reading the design, slicing images, writing styles, to opening the browser to take screenshots for comparison, checking the DOM to fix deviations.

What I normally feed it is just two lines: a Figma link for the target module, plus a local page address that can be opened.

Usage example of providing Figma node and local acceptance address to AI, links are placeholders

In short, whether you write it yourself or have AI generate the first draft, the most torturous part is always the final "eye-measuring pixels". So I simply bound "writing pages + screenshot acceptance + finding reasons and rewriting" into one pipeline: let AI first read the Figma nodes, distinguish which parts should be exported as images and which should be written as code; after writing, it launches a browser to take a screenshot, does pixel comparison with the design, finds where things are off, then inspects the DOM box model to identify the root cause and fixes it.

As for the scripts and rules inside, you don't need to ask me for the source code. Current AI has evolved to the stage of "you explain the rules clearly, it can write the tools itself". As long as you think through each step, what input to give, and how to judge success, just let AI build a set along your own project, run it on real pages for two rounds and it'll be usable. So this article doesn't ship a code package; it specifically breaks down the workings of this workflow.

In daily use, it's basically just throwing a link and you can go get some water. Of course, AI isn't a god: the precondition is that it has permission to read the Figma node and its parent canvas, and the local page can open successfully and stay at the state to be compared; if you only give it a blurry image, or the page gets stuck on the login page, or you can't even figure out the design width, it will honestly stop and ask you for more info.

First look at the conclusion: how does the whole workflow run?

One sentence: connect "read design → split layers, write page → browser screenshot comparison → find cause, change code", letting the Agent correct based on comparison results, then screenshot for re-verification.

To make this loop work, the first step is to connect Figma MCP to the AI, allowing it to directly read the structure and asset information of the target design node, rather than staring at a screenshot and guessing blindly.

From Figma MCP reading design, to page restoration, screenshot diff, and correction re-verification

Figure: From design node, through implementation, browser screenshot, diff localization, back to code correction.

Generating the page is only the first step. Next, the workflow takes screenshots under fixed conditions, compares the page with the design, then investigates and modifies based on deviations; after modifying, it screenshots again, repeating as needed until acceptance requirements are met.

AI repeatedly accepts by browser screenshot, comparing design, locating deviation and modifying code

Figure: Re-screenshot after each modification so the next round of adjustments has actual page effect as a basis.

Step What the workflow does
Screenshot Fix viewport, DPR, fonts, and page state; take screenshot after the target page is ready
Comparison Align the generated page screenshot with the corresponding design area, generate overlay and diff images
Investigation Combine diff, DOM box model, assets and fonts to check the source of deviation
Correction & re-verification Modify code, then screenshot and compare again; when info is insufficient or results don't converge, request more info and check environment

To keep AI from straying in these four steps, the key is not to write more top, left values, but to set four rules in advance:

  1. Entry only requires selecting one Figma node: Just select the page or module node to be restored in Figma; no need to manually slice images or measure dimensions.
  2. Calculate layout scaling and image export multipliers separately: CSS size is based on the project's layout baseline, and the export multiplier is based on the asset display scenario; don't mix the two accounts.
  3. Dynamic content leaves DOM, complex visuals consider assets: Text that changes and areas to click should use real DOM; complex layers then use assets plus CSS.
  4. Diff only reports position, check the cause before changing code: Pixel mismatches could be layout, assets, fonts, or rendering environment; measure in the browser first before acting.

What tools do you need to run this workflow?

You only need four things: a coding Agent as the "general contractor", a Figma reading tool, a browser that can take screenshots, and an image comparison script.

Agent calls Figma MCP to read design, uses EGO Lite for screenshots, then compares and fixes

Figure: Agent reads design and implements page, compares design reference image with page screenshot; after modification, screenshots again for acceptance.

Here are the tools and options used in this workflow; you can swap them based on your environment:

I. Read design and calculate sizes: first find the artboard baseline, then calculate CSS and slices separately

After getting the Figma node, don't rush to write styles; first understand the design scope, artboard size, and the project's layout conventions, then decide how to write CSS and export slices.

Previously when manually slicing, the easiest things to miss were outer glow, mask cropping, or transparent margins. With Figma MCP, these inventory tasks are completely handled automatically by the AI model and MCP in the background; during normal development you don't need to worry about it (you only need to write the Skill rules telling the AI to check these four things via MCP before acting):

1. Prerequisite: why mobile slicing uses "2x images"?

Those who do mobile H5 know that current phone screens are basically high-DPI (DPR at least 2). If there's an icon on the page, you write 40px × 40px in CSS, but you also only export a 1x image of 40×40 pixels to stuff in; on the phone screen it will be scaled up and appear blurry. To be sharp, the actual image placed should have 80×80 physical pixels — the so-called 2x image.

Schematic of the effect of using different pixel counts for the same CSS display size at DPR 2

Figure: In this DPR 2 example, CSS size is the same, but image pixel counts differ.

2. Using 360px dev baseline as example: design at 360 or 720 decides how to write CSS and export images

Now that you understand why 2x images are needed, look at the baseline for frontend code.

For example, our team uses 360px screen width as the development baseline, but designers have different habits: some draw 360px wide (1x design), some draw 720px wide (already scaled 2x). The Figma links we typically give to AI are often just a button or modal inside the page; looking at just the local node you can't tell the artboard scale. So AI's first step must be to trace outward from the node to confirm whether the full page artboard is 360px or 720px:

Using 360px dev baseline as example, comparing CSS and export sizes for different design widths

Figure: Design size, CSS size, and export pixel are calculated separately; exact baseline and multipliers follow project conventions.

Once this rule is clearly explained to AI, it will no longer make mistakes like "directly copying 80px from a 720px design into CSS, making the button twice as big" or "still exporting at 2x from a 720px design"; if it doesn't have permission to read the outer artboard, or you only gave a screenshot where it can't tell 360px from 720px, it will stop and ask you first.

II. Splitting layers: which to export as slice, which to write as DOM and CSS?

Layer triage uses three criteria: whether content changes, whether it needs interaction, and whether pure CSS can reproduce it well.

Selection schematic for dynamic content, styles, assets, and hybrid implementation layers

Figure: Choose DOM, CSS, assets, or combination based on content and visual characteristics.

Without rules, AI tends to go to two extremes when writing pages: either lazily compress the whole module into one big image, or suddenly develop strange fighting spirit, insisting on pure CSS or hand-written SVG to draw complex illustrations. We directly give it a triage table in the rules:

Content type Implementation Reason
Titles, descriptions, amounts, countdowns, rankings Real DOM Text changes, needs API data, multi-language or responsive states
Background colors, gradients, rounded corners, borders, simple shadows CSS Rules are clear, can naturally scale with container size
Illustrations, characters, complex textures, light effects, particles Separate assets or hybrid Depends on visual complexity, reuse needs, and asset format
Complex background + dynamic text Asset background + DOM Background keeps visual details, upper DOM keeps text and click interactions

For example, a "Claim Now" button with a metallic gold background can be exported as an image, and the Go Now text on top is overlaid as real DOM; for a reward card, keep the complex card background as slice, then arrange icons, quantities, and names with DOM on top.

Comparison of DOM text and image background layers, plus transparent edges and background color being carried in

Figure: Keep dynamic text and click areas in DOM, and check whether slice edges carry background color.

Here are two details that are easy to step on; they must be written into the rules to remind AI:

III. Visual acceptance: first lock browser conditions, then run screenshot diff

First lock viewport, DPR, fonts, and page state, then do screenshot diff; only then are comparison results valid.

Before screenshot, confirm viewport, DPR, fonts, assets, data, and page state

Figure: Fix screenshot conditions, and confirm that the design and page screenshot correspond to the same area.

If the environment isn't locked, screenshots from the same CSS are like opening a mystery box: slight changes in window width, wrong DPR, web fonts still loading, lazy-loaded images not scrolled into view, or modals and business states not opened — all comparison results become miscarriages of justice.

So automatic acceptance is fixed in two steps:

  1. Wait until the page is fully ready, then screenshot: The browser tool opens the target page with the required viewport and DPR; when login is needed, configure or reuse a valid login state and switch to the target state, patiently wait for fonts, images, and the target module to render stably, then capture the corresponding area.
  2. Align sizes and run pixel diff: First confirm that both images frame the same area, then use Sharp to process them to comparable sizes, run the comparison logic to compute differences, and generate overlay and diff images.

Why can't you just hand both images directly to AI, why run through Sharp first?

Many might ask: can't current AI already see images? Why not just feed "design mock" and "browser screenshot" to it and let it find differences itself? That really won't work, stuck on two real problems:

Also, a preventive note to AI: text rendered by browsers naturally has a subtle edge fuzz compared to Figma's text. So the red-patch image is to help it find "which area is off", but don't obsess over "zero error" for that text fuzz.

IV. How to fix deviations: don't let AI blindly adjust top, check the real DOM in the console like a human first

After getting the red-patch comparison image, the most taboo thing is to let AI stare at the red areas and directly hard-adjust top, left, or margin — it must first go to the browser console to measure the actual DOM, find the root cause, then cut.

Without this rule, AI's style fixing is basically playing "whack-a-mole": for example, seeing the title is off by 4px up in the diff image, it immediately shoves margin-top: 4px onto the title; the title lines up, but the whole row of cards below gets pushed down by 4px, and the more it changes, the more diffs appear.

Actually, when frontend developers adjust pages ourselves, we don't blindly change numbers when we see something wrong; we first open F12 console and look at the box model. So we directly wrote this debugging experience into the rules, letting AI follow the "look at the image for symptoms → inspect DOM in console" approach:

What you see on the comparison image The real cause often is (where AI should check)
The whole module is off, showing a large block of ghosting Outer container's starting point, width/height are wrong, or the 360/720 scaling ratio isn't converted correctly
The frame is aligned, but only the text inside has ghosting or wraps differently Fonts haven't loaded yet, or line-height isn't fixed and the browser default stretches it
An icon or badge suddenly missing a part, like cut off by a knife Don't redraw the icon! Trace up the parent elements; eight times out of ten some ancestor has overflow: hidden or a mask cropping the content
The button looks fine, but clicking does nothing Run elementsFromPoint() at the button's coordinates to see if some transparent overlay or higher z-index is covering it
Rounded corners have black edges Check the slice file again; it's usually because the dark canvas background was captured when exporting from Figma

In short, before modifying code, let AI run a few scripts in the browser (equivalent to us opening F12): use getBoundingClientRect() to measure the actual rendered width, height, and coordinates of that element, trace up the parent nodes to see who added overflow: hidden, and use elementsFromPoint() to see who's on top.

Layout diagnosis path from diff ghosting to box model, cropping, and layer root causes

Figure: First look at the phenomenon in the comparison image, then measure box model, check parent cropping and occlusion in the browser, confirm root cause, then modify code.

Finally, tie a "no-stubbornness" stop-loss rope for AI: after the first round of comparison, first investigate all misplacements at once and fix them in batches; if after two consecutive rounds of fixes it still doesn't converge, immediately stop and go back to check whether the design size was misread, fonts didn't load, or the page isn't switched to the corresponding state — don't let it grind against top: 1px, top: 2px in place.

How to let AI build your own restoration workflow?

Directly feed AI the rules about "how to confirm design baseline, how to split layers, under what conditions to screenshot, after diff how to investigate", let it build along your project's existing tech stack, try a simple module for one round.

From writing rules to module trial run and re-verification iteration, build your own workflow

Figure: First write clear rules and trial run, then adjust based on project conditions.

During the trial run, if you hit problems like not being able to read the design baseline, unclear page state, or two rounds of fixes still don't match, let it stop and ask you for info or check the environment. Once the flow runs smoothly, gradually add your project's common components, slice storage paths, and acceptance details to accumulate into your own Skill or rule file.

This workflow has been running in my own hands for almost half a year; when moving to other projects, just fine-tune according to the actual design habits, tools at hand, and page states — after all, letting AI watch the design for us is to finish work early and go home, not to create a new pit for ourselves.