From Figma to pixel-perfect: an AI agent that restores pages and reviews its own work
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.
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.
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.
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:
- 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.
- 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.
- 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.
- 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.
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:
- Coding Agent (general contractor dispatch): Codex, Claude Code, Cursor all work. Configure rules and tools according to what it supports, let it string the steps together, and modify code along your project's existing tech stack; no need to refactor the project to accommodate it.
- Figma MCP (read design + export slices): Responsible for going into the node tree to read structure, artboard width, and layer properties; when slicing is needed, export real assets on demand.
- EGO Lite browser (screenshot with login state): Many business pages require login to access. Tools like Playwright can't easily reuse existing login state, while directly controlling your daily Chrome will mess up the tabs you're using. EGO Lite is also based on the Chrome kernel; it can keep login state for AI to automatically enter pages and screenshot, without disturbing your active browser.
- Sharp + comparison script (pixel-level fault finder): In short, take a photo of the page written by AI, then use Sharp (based on
libvips) to unify dimensions and channels with the Figma design, stack them for comparison, generate overlay and diff images; once a mismatch is found, AI goes back to change code, take another photo, compare, and loop until restoration meets requirements.
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):
- Which page artboard does this node belong to? How wide is the artboard?
- What are the actual boundaries of the component, parent cropping and mask relationships?
- Which complex icons or illustrations need to be exported as minimal complete nodes?
- Are there reusable components or style variables in the project?
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.
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:
- If the design is
360pxwide: The artboard width matches our dev baseline. A40pxicon measured in the design can be written directly as40pxin CSS at1:1; but when exporting slices, you need Figma to export at2x(2x image) to get80pxhigh-res image. - If the design is
720pxwide: The designer has already scaled the artboard by 2x. The same icon measures80pxin the design; when writing CSS, divide by 2 to get40px; and when exporting slices, export at1x(1x image) and you get80px, which natively serves as a 2x image under the360pxbaseline.
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.
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.
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:
- Don't be lazy and compress the entire module into a single static image: For example, exporting a button along with background and text as one image. At first glance it looks right, but new text from backend API can't be inserted, click events and analytics have nowhere to attach, and once you switch to multi-language long text it will break out. So wherever text changes or there's a click point, honestly use "slice as background + real DOM for text"; only purely decorative images that neither need clicks nor text changes warrant full-module export.
- When exporting slices, watch the "transparent background", don't capture the canvas background color: Sometimes irregular buttons or cards on a page mysteriously get black edges at the corners. This isn't a CSS problem; it's that when exporting from Figma, you selected the wrong layer, carrying the dark canvas background into the image (like the right side of the image above). Once the black background is welded into the image, adding more
border-radiusin CSS won't wash it off. So the rules must tell AI: when exporting, only select clean graphic nodes, and after exporting, double-check the four corners are truly transparent.
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.
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:
- 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.
- 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:
- AI looking at two separate images is a "nearsighted person": If you let AI look at the design on the left and the screenshot on the right, when the title is off by
2pxdown or the line-height is slightly off, it can't see it at all; it will confidently lie to you saying "already identical". Only by superimposing the two images (generating a ghosted overlay) and marking the unmatched pixels in glaring red (generating a diff image), turning a few pixels of deviation into a visible red patch, can AI see where it's off. - If the two images have different sizes, they can't be directly overlaid: To stack two images pixel by pixel, their width and height must be identical. But a reference exported from Figma might be
720pxwide, while a browser screenshot might be360pxwide, or the height might differ by1pxdue to rounding. If sizes differ, stacking either errors out or the entire image shifts and blushes red. Sharp's job here is a "tailor": before comparing, scale both images to exactly the same width, height, and format so they can be tightly overlaid.
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.
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.
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.