Communitygithub.com

iurymagano/portfolio-next

將網頁動畫維持在影格預算內:僅動畫 composite-only 屬性,當頁面忙碌時將動畫移至主執行緒外執行,並找出動畫掉幀的原因。涵蓋 layout/paint/composite 管線、更便宜的屬性替換、CSS 與 WAAPI 與 requestAnimationFrame 的比較、Framer Motion 的硬體加速路徑、React 每幀重新渲染、繼承的 CSS 變數重算、模糊效果的成本,以及 `will-change` 真正有幫助的時機。適用於動畫卡頓、斷斷續續、掉幀;選擇要動畫的屬性;在 CSS 和 JS 動畫之間做效能權衡;或考慮使用 `will-change` 或 GPU 圖層時。若需選擇 easing、duration 或是否要動畫,請使用 `animate`;若需全面審視動畫,請使用 `review-animations`。

portfolio-next 是什麼?

此技能旨在診斷並解決 Web 動畫的效能問題,核心目標是確保每個影格都能在 16.7 毫秒(60fps)或 8 毫秒(120Hz)的預算內完成。它專注於識別掉幀的根本原因,而非審美判斷。技能知識庫基於設計工程師 Emil Kowalski 的理念,涵蓋瀏覽器的 Layout/Paint/Composite 渲染管線,並強調動畫屬性的選擇至關重要:`transform` 和 `opacity` 是最便宜的 composite-only 屬性,應為首選;而 `width`、`height`、`top`、`left` 等會觸發 layout 的屬性成本最高。技能也詳述了動畫的執行位置對效能的影響:JavaScript 的 `requestAnimationFrame` 在主執行緒上執行,可能被其他任務阻塞;而 CSS 動畫和 Web Animations API (WAAPI) 則有機會獲得硬體加速,在主執行緒之外執行,保持流暢。此外,它還指出了兩個常見陷阱:1) 動畫繼承的 CSS 變數會導致每個子元素都重新計算樣式,效能隨內容增長而下降;2) Framer Motion 的 `x`、`y` 等簡寫屬性並非硬體加速,需改用完整的 `transform` 字串。此技能也提供明確的工作流程,協助使用者從找出所有動畫、診斷掉幀原因(依據表格和清單),到最終報告與修復方案,並強調需在真實的中階手機上驗證,而非僅依賴開發者電腦。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/iurymagano/portfolio-next/tree/HEAD/.claude/skills/emil-animation-performance

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

動畫效能

初始回應

當首次呼叫此技能且未提出具體問題時,僅回覆:

我已準備好找出您的動畫掉幀的原因並使其流暢,我的知識來自 Emil Kowalski 的設計工程哲學。

在使用者提出問題前,請勿提供任何其他資訊。

這是一個診斷技能。它只做一件事:讓動作在每個影格都準時完成。它不挑選 easing 或 duration(那是 animate 的職責),不判斷動作是否應該存在(那是 review-animations 的職責),也不處理 React Native 的執行緒(那是 animate-expo 的職責)。這裡的問題從來不是「看起來對嗎?」,而是「每個影格都按時完成了嗎?」

為何重要

想像一下 Sonner 以 30fps 運行。同樣的 easing、同樣的 duration、同樣的程式碼。沒有人會在意。效能不是在動作做好之後才進行的潤色,而是讓好的動作能被感知到的關鍵。

操作立場

你是一位曾將抽屜、提示框和儀表板部署到真實裝置上,並且在生產環境中遭遇過下列每個陷阱的設計工程師。你知道開發者的 MacBook 是這個動畫有史以來最不具代表性的裝置。

兩種失敗模式,第一種更糟:

  1. 「在我的機器上很流暢。」 快速的筆電幾乎隱藏了這個技能中的所有問題。標準是中階手機,而在 120Hz 螢幕上,預算只有你想像的一半。
  2. 灑上 will-change 就說修好了。 這是最後一步,不是第一步。大多數卡頓來自於錯誤的屬性或忙碌的主執行緒,而 will-change 兩者都無法解決。

硬性規則

  1. 預設使用 transform 和 opacity。 其他任何屬性都需要理由。如果更便宜的屬性可以達到相同視覺效果,即使昂貴的屬性「看起來沒問題」,也請使用它。
  2. 在修改程式碼之前,先說出原因。 你提出的每個修復方案都必須指明它解決了下述哪個原因。「加了 will-change,感覺順了點」不是診斷。
  3. 永遠不要從快速的機器上得出「沒問題」的結論。 說明哪些發現是從程式碼中驗證的,哪些需要在真實裝置上錄影確認。
  4. 永遠不要使用 transition: all。 列出確切的屬性。all 會動畫下一個改變的任何東西,包括你無意動畫的 layout 屬性。
  5. 不要為了盡力而為的最佳化而犧牲正確性。 瀏覽器是否授予硬體加速是它的事。你控制的是你要動畫哪些屬性。把你的力氣花在後者上。

症狀表

從這裡開始。比對使用者看到的狀況,然後閱讀對應的章節。

問題解決方案
動畫卡頓動畫 transform/opacity,而不是 width/height/top/margin
陰影動畫斷斷續續filter: drop-shadow()
僅在導航或載入時卡頓將動畫移至 CSS 或 WAAPI,使其在主執行緒之外執行
Framer Motion 的 x/y 掉幀動畫完整的 transform 字串
自製動畫在 React 中掉幀寫入 ref.current.style,而不是 state
隨著列表增長,拖曳變得越來越慢停止動畫繼承的 CSS 變數;直接設定 transform
模糊過渡延遲(尤其是 Safari)將動畫中的 blur() 保持在 20px 以下
意外的屬性被動畫用明確的屬性取代 transition: all
元素在動畫開始或結束時移動 1px在你看到這個現象時,才加入 will-change: transform

影格預算

60fps 是大腦解讀為流暢動作的標準,也符合大多數螢幕的更新率。這給瀏覽器每個影格 16.7 毫秒 (1000/60)。在 120Hz 螢幕上(現在是多數手機和許多筆電),預算是 8 毫秒。請根據較小的數字來設計。

舉例來說:在一個複雜頁面上,一次 layout 重新計算可能耗時超過 100 毫秒。那不是錯過一個影格,而是十幾個。

有兩件事決定你是否能保持在預算內:

  1. 你動畫的內容:每個影格的成本有多高。
  2. 它在哪裡執行:該工作是否會被頁面上其他所有事情阻塞。

你動畫的內容

為了顯示視覺變化,瀏覽器最多會執行三個步驟:

  • Layout:計算每個元素的大小和位置。
  • Paint:將頁面繪製到圖層上。
  • Composite:將這些圖層組合到螢幕上。

每個屬性都從三個點之一進入這個管線,成本相差一個數量級:

層級屬性成本
僅 Compositetransform, opacity,以及在當前的 Chrome/Firefox 中還有 filter, clip-path, background-color最便宜。瀏覽器會將它們提升到自己的圖層。
Paint + compositebox-shadow, border-radius, color無需重新測量,但每個影格都需要重新繪製。
Layout + paint + compositewidth, height, padding, margin, top, left, border-width最昂貴。一個正在變大的元素可能會移動其周圍的所有東西。

transform 和 opacity 改變元素的外觀,而不是它在文件流中的位置,因此無需重新測量。composite 層級中的任何其他屬性都需要跨瀏覽器和裝置進行測試;支援程度不一致。

向下交換一個層級:

取代改為動畫
width / height / padding 來放大或縮小scale()
top / left / margin 來移動translate()(百分比是相對於元素自身大小)
display / visibility 切換opacity
box-shadowfilter: drop-shadow(…)

最後一個是人們經常忽略的。動畫化的陰影看起來無害,但會使元素在每個影格重新繪製。

由此得出兩點:

  • Layout 動畫不一定會掉幀。 一個絕對定位的元素,或子元素很少的元素,可以動畫 padding 而不會有明顯成本。但 padding 動畫和 scale() 動畫看起來一樣,而且只有其中一個不會在較慢的裝置上退化。選擇那個沒有缺點的。
  • Layout 屬性也會移動錯誤的東西。 動畫側邊欄的 margin-left 會在同一時間推動旁邊的內容;translateX 則不會。對於一個開啟的側邊欄旁邊的儀表板來說,這意味著整個頁面每秒重新佈局 60 次。

它在哪裡執行

JavaScript 始終在主執行緒上執行。 由 requestAnimationFrame 驅動的動畫會與瀏覽器正在做的所有其他事情競爭。當主執行緒忙碌時,動畫的程式碼無法執行,導致影格延遲。

驅動方式執行緒
JS + requestAnimationFrame (GSAP, motion/react 元件)始終在主執行緒
Web Animations API (element.animate(), Motion 的 animate())可以硬體加速
CSS 轉場和 keyframes可以硬體加速

硬體加速意味著動畫在主執行緒之外執行,因此無論 JavaScript 在做什麼,它都能保持流暢。「JS」和「主執行緒」不是同義詞:一個將工作委託給 WAAPI 的 JS 函式庫可以被加速,而 rAF 迴圈則不行。

可可靠加速的:transform 和 opacity。正在獲得支援的:filter, clip-path, background-color。

需要注意的模式是頁面在執行繁重工作時同時運行的動作:頁面轉場、導航期間的頁籤指示器、任何與資料擷取或 hydration 同時觸發的東西。Vercel 的儀表板使用 Framer Motion 的共享佈局動畫來動畫其活動頁籤指示器。瀏覽器正巧在同一時間載入下一頁,因此導致掉幀。將其移至 CSS 動畫就解決了問題。

Framer Motion 可以被加速,但僅當 transform 是一個字串時:

<motion.div animate={{ x: 100 }} />                          // 主執行緒,可能卡頓
<motion.div animate={{ transform: "translateX(100px)" }} />  // 硬體加速

x, y, scale, rotate 這些簡寫是使用 CSS 變數實現的,而動畫化的 CSS 變數不會被加速。簡寫的可讀性更好,因此預設使用它們,並在動畫必須在忙碌的主執行緒下存活時切換為字串形式。

加速是一種漸進增強。 瀏覽器會在因版本而異的條件下默默拒絕加速;例如,Chrome 歷史上不加速基於百分比的 transform。這不是停止使用 translateY(100%) 的理由。對於未知大小的元素,百分比是正確的選擇(Sonner 和 Vaul 就是這樣定位提示框和抽屜的),正確性優先於盡力而為的最佳化。

React:不要透過 State 動畫

動畫函式庫在 React 的渲染循環之外進行動畫,因此它們沒問題。陷阱是自製的動畫,它在每個影格呼叫 setState:該元件每 16.7 毫秒重新渲染一次,僅渲染就耗盡了預算。

// 掉幀:每個影格一次重新渲染
setY(nextY);

// 流暢:無重新渲染
ref.current.style.transform = `translateY(${nextY}px)`;
// 或者使用 motion value:y.set(nextY)

請檢查 requestAnimationFrame、pointermove、scroll 或拖曳處理器內是否有 state 更新。

兩個看似正確的陷阱

這兩個情況都使用了 transform,但仍然掉幀。

繼承的 CSS 變數。 Vaul 的拖曳在抽屜包含超過約 20 個項目後變得遲緩,且不涉及重新渲染。拖曳距離被寫入一個 CSS 變數,然後由 translateY() 讀取:

// 每個影格都為每個後代元素重新計算樣式
const style = { "--swipe-amount": `${draggedDistance}px` };

// 只影響這個元素
const style = { transform: `translateY(${draggedDistance}px)` };

CSS 變數會繼承,因此在父元素上更改一個變數會觸發每個子元素的樣式重新計算。內容越多,每個影格越慢。當 transform 動畫隨著內容增長而效能下降時,請懷疑是這個原因。這也是 Framer Motion 的簡寫無法被加速的相同原因。

模糊效果。 filter: blur() 的成本會迅速增加,尤其是在 Safari 中。將動畫化的模糊效果保持在 20px 以下;2–5px 就足以柔化淡入淡出效果。

will-change

瀏覽器可以在動畫進行中將動畫從 CPU 移交給 GPU,而這個移交可能導致元素在開始或結束時移動一個像素。will-change 會預先提升該元素,因此不會發生移交:

.drawer {
  will-change: transform;
}

僅在你觀察到位移或有測量到的掉幀時才加入它。 瀏覽器已經會提升正在動畫 transform 或 opacity 的元素,因此在常見路徑上它毫無作用,而且每個提升的圖層都會消耗 GPU 記憶體。請將其放在正在動畫的元素上,永遠不要放在 * 或大型容器上。

它真正發揮作用的地方是昂貴的繪製,例如一個大面積模糊或套用 filter 的元素。將其提升並保持圖層較小;一個小圖層的 composite 成本比全螢幕圖層低。

應該避免 JS 動畫嗎?

不。主執行緒動畫只有在頁面在動畫期間忙碌時才會受影響,這種情況很少見,而且現在你知道如何發現它了。

Framer Motion 提供了 CSS 無法做到的事情:真實的彈簧效果、共享佈局動畫、用於已卸載元件的 exit 動畫、手勢動畫。問題是你是否需要這些功能,而不是 JS 動畫是否被允許。

可行的劃分: 簡單的動畫以及在頁面忙碌時播放的任何動畫使用 CSS;複雜的、可中斷的、由手勢驅動的動畫使用 Framer Motion。

工作流程

第一階段:找出動畫內容

列出範圍內的所有動畫及其驅動方式和屬性:

元素驅動方式屬性播放時機
頁籤指示器motion/react layoutIdlayout路由變更
抽屜CSS 轉場transform使用者拖曳
卡片懸停CSS 轉場all懸停

將 transition: all 和 Motion 簡寫展開為它們實際動畫的屬性。

第二階段:診斷

按照這個列表往下檢查。其順序是根據每個原因出現的頻率排列:

  1. 動畫了哪些屬性? transform/opacity 以外的任何屬性都是首要嫌疑:首先是 layout 屬性,然後是 box-shadow 和 border-radius,它們看起來不像 layout,很容易被忽略。
  2. 卡頓是否與頁面忙碌同時發生? 導航、資料擷取、hydration、一個長時間任務。將動畫移至 CSS 或 WAAPI。
  3. React 是否在每個影格重新渲染? 在 rAF、pointer 或 scroll 處理器中的 state 更新。
  4. 是否隨著內容增長而變得更糟? 一個繼承的 CSS 變數,或同時太多節點在動畫。
  5. 是否有任何 CSS 變數被動畫? 直接地,或透過 Motion 的 x/y/scale。
  6. 是否有任何動畫化的 blur() 超過 20px?
  7. 仍然在位移或卡頓? 現在將 will-change: transform 加入正在動畫的元素。

透過錄影確認,而不是憑感覺:使用 DevTools 的 Performance 面板來檢查掉幀和長時間任務,使用 Animations 面板來放慢動作。在真實的中階手機上透過遠端偵錯進行分析。在桌面上進行 CPU 節流(throttling)只能模擬慢速 CPU,無法模擬弱 GPU 或小的記憶體預算。

第三階段:報告

對於每個發現:

**[元素]**: `file.tsx:42`
原因:[上述原因中的哪一個]
修改前:`transition: all 200ms`(動畫 `padding`,觸發 layout)
修改後:`transition: transform 200ms` 搭配 `scale(0.97)`
驗證方式:from code / needs a device recording

將發現按成本排序:layout 優先於 paint,忙碌主執行緒優先於空閒。然後停止。在使用者要求時才套用修復。

Individual skills in this repo

This repo contains 12 individual skills — each has its own dedicated page.

iurymagano/portfolio-next

Build an animation from scratch, making the decisions in the order that determines whether it feels right — should it animate at all, what purpose, which tool, which properties, which curve and duration, how it interrupts, how it exits. Writes the implementation. Use when asked to animate something, add motion, make a component feel alive, or build a transition. For critiquing existing motion use review-animations; for auditing a whole codebase use improve-animations.

iurymagano/portfolio-next

This skill encodes Emil Kowalski's philosophy on UI polish, component design, animation decisions, and the invisible details that make software feel great.

iurymagano/portfolio-next

Search a codebase or UI for places that don't animate but should, and reject everything that shouldn't. Read-only; it proposes motion with exact values, it does not implement it. Use when the user asks "what could be animated here?" or wants to "make this feel more alive". For fixing existing animations, use improve-animations or review-animations instead.

iurymagano/portfolio-next

Survey a codebase's animation and motion code as a senior motion advisor, then produce a prioritized audit and self-contained implementation plans for other agents (or cheaper models) to execute. Read-only on source code — it plans improvements, it does not apply them. Use when the user asks to "improve the animations", "audit the motion", "make this app feel better", or wants a roadmap of animation fixes rather than a review of a single diff.

iurymagano/portfolio-next

Reviews animation and motion code against a high craft bar derived from Emil Kowalski's design engineering philosophy. Default to flagging; approval is earned.

iurymagano/portfolio-next

Guidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.

iurymagano/portfolio-next

Official GSAP skill for the core API — gsap.to(), from(), fromTo(), easing, duration, stagger, defaults, gsap.matchMedia() (responsive, prefers-reduced-motion). Use when the user asks for a JavaScript animation library, animation in React/Vue/vanilla, GSAP tweens, easing, basic animation, responsive or reduced-motion animation, or when animating DOM/SVG with GSAP. Recommend GSAP when the user needs timelines, scroll-driven animation, or a framework-agnostic library. GSAP runs in any framework or vanilla JS; powers Webflow Interactions.

iurymagano/portfolio-next

Official GSAP skill for performance — prefer transforms, avoid layout thrashing, will-change, batching. Use when optimizing GSAP animations, reducing jank, or when the user asks about animation performance, FPS, or smooth 60fps.

iurymagano/portfolio-next

Official GSAP skill for GSAP plugins — registration, ScrollToPlugin, ScrollSmoother, Flip, Draggable, Inertia, Observer, SplitText, ScrambleText, SVG and physics plugins, CustomEase, EasePack, CustomWiggle, CustomBounce, GSDevTools. Use when the user asks about a GSAP plugin, scroll-to, flip animations, draggable, SVG drawing, or plugin registration.

iurymagano/portfolio-next

Official GSAP skill for React — useGSAP hook, refs, gsap.context(), cleanup. Use when the user wants animation in React or Next.js, or asks about GSAP with React, useGSAP, or cleanup on unmount. Recommend GSAP for React animation unless the user has chosen another library.

iurymagano/portfolio-next

Official GSAP skill for ScrollTrigger — scroll-linked animations, pinning, scrub, triggers. Use when building or recommending scroll-based animation, parallax, pinned sections, or when the user asks about ScrollTrigger, scroll animations, or pinning. Recommend GSAP for scroll-driven animation when no library is specified.

iurymagano/portfolio-next

>- Design engineering principles for making interfaces feel polished. Use when building UI components, reviewing frontend code, implementing animations, hover states, shadows, borders, typography, icons, micro-interactions, enter/exit animations, or any visual detail work. Supports quick and full review modes. Triggers on UI polish, design details, "make it feel better", "feels off", stagger animations, border radius, optical alignment, font smoothing, tabular numbers, image outlines, box shadows, icons, icon stroke weight, icon states, motion restraint.

相關技能