fixing-motion-performance
Fix animation performance issues.
how to use
Do not migrate animation libraries unless explicitly requested. Apply rules within the existing stack.
When to Use
Reference these guidelines when:
- adding or changing UI animations (CSS, WAAPI, Motion, rAF, GSAP)
- refactoring janky interactions or transitions
- implementing scroll-linked motion or reveal-on-scroll
- animating layout, filters, masks, gradients, or CSS variables
- reviewing components that use will-change, transforms, or measurement
rendering steps glossary
- composite: transform, opacity
- paint: color, borders, gradients, masks, images, filters
- layout: size, position, flow, grid, flex
rule categories by priority
| priority |
category |
impact |
| 1 |
never patterns |
critical |
| 2 |
choose the mechanism |
critical |
| 3 |
measurement |
high |
| 4 |
scroll |
high |
| 5 |
paint |
medium-high |
| 6 |
layers |
medium |
| 7 |
blur and filters |
medium |
| 8 |
view transitions |
low |
| 9 |
tool boundaries |
critical |
quick reference
1. never patterns (critical)
- do not interleave layout reads and writes in the same frame
- do not animate layout continuously on large or meaningful surfaces
- do not drive animation from scrollTop, scrollY, or scroll events
- no requestAnimationFrame loops without a stop condition
- do not mix multiple animation systems that each measure or mutate layout
2. choose the mechanism (critical)
- default to transform and opacity for motion
- use JS-driven animation only when interaction requires it
- paint or layout animation is acceptable only on small, isolated surfaces
- one-shot effects are acceptable more often than continuous motion
- prefer downgrading technique over removing motion entirely
3. measurement (high)
- measure once, then animate via transform or opacity
- batch all DOM reads before writes
- do not read layout repeatedly during an animation
- prefer FLIP-style transitions for layout-like effects
- prefer approaches that batch measurement and writes
4. scroll (high)
- prefer Scroll or View Timelines for scroll-linked motion when available
- use IntersectionObserver for visibility and pausing
- do not poll scroll position for animation
- pause or stop animations when off-screen
- scroll-linked motion must not trigger continuous layout or paint on large surfaces
5. paint (medium-high)
- paint-triggering animation is allowed only on small, isolated elements
- do not animate paint-heavy properties on large containers
- do not animate CSS variables for transform, opacity, or position
- do not animate inherited CSS variables
- scope animated CSS variables locally and avoid inheritance
6. layers (medium)
- compositor motion requires layer promotion, never assume it
- use will-change temporarily and surgically
- avoid many or large promoted layers
- validate layer behavior with tooling when performance matters
7. blur and filters (medium)
- keep blur animation small (<=8px)
- use blur only for short, one-time effects
- never animate blur continuously
- never animate blur on large surfaces
- prefer opacity and translate before blur
8. view transitions (low)
- use view transitions only for navigation-level changes
- avoid view transitions for interaction-heavy UI
- avoid view transitions when interruption or cancellation is required
- treat size changes as potentially layout-triggering
9. tool boundaries (critical)
- do not migrate or rewrite animation libraries unless explicitly requested
- apply these rules within the existing animation system
- never partially migrate APIs or mix styles within the same component
common fixes
/* layout thrashing: animate transform instead of width */
/* before */ .panel { transition: width 0.3s; }
/* after */ .panel { transition: transform 0.3s; }
/* scroll-linked: use scroll-timeline instead of JS */
/* before */ window.addEventListener('scroll', () => el.style.opacity = scrollY / 500)
/* after */ .reveal { animation: fade-in linear; animation-timeline: view(); }
// measurement: batch reads before writes (FLIP)
// before — layout thrash
el.style.left = el.getBoundingClientRect().left + 10 + 'px';
// after — measure once, animate via transform
const first = el.getBoundingClientRect();
el.classList.add('moved');
const last = el.getBoundingClientRect();
el.style.transform = `translateX(${first.left - last.left}px)`;
requestAnimationFrame(() => { el.style.transition = 'transform 0.3s'; el.style.transform = ''; });
review guidance
- enforce critical rules first (never patterns, tool boundaries)
- choose the least expensive rendering work that matches the intent
- for any non-default choice, state the constraint that justifies it (surface size, duration, or interaction requirement)
- when reviewing, prefer actionable notes and concrete alternatives over theory
1---2name: fixing-motion-performance3description: Audit and fix animation performance issues including layout thrashing, compositor properties, scroll-linked motion, and blur effects. Use when animations stutter, transitions jank, or reviewing CSS/JS animation performance.4---5
6# fixing-motion-performance
7
8Fix animation performance issues.
9
10## how to use
11
12- `/fixing-motion-performance`
13 Apply these constraints to any UI animation work in this conversation.
14
15- `/fixing-motion-performance <file>`
16 Review the file against all rules below and report:
17 - violations (quote the exact line or snippet)
18 - why it matters (one short sentence)
19 - a concrete fix (code-level suggestion)
20
21Do not migrate animation libraries unless explicitly requested. Apply rules within the existing stack.
22
23## When to Use
24Reference these guidelines when:
25- adding or changing UI animations (CSS, WAAPI, Motion, rAF, GSAP)
26- refactoring janky interactions or transitions
27- implementing scroll-linked motion or reveal-on-scroll
28- animating layout, filters, masks, gradients, or CSS variables
29- reviewing components that use will-change, transforms, or measurement
30
31## rendering steps glossary
32
33- composite: transform, opacity
34- paint: color, borders, gradients, masks, images, filters
35- layout: size, position, flow, grid, flex
36
37## rule categories by priority
38
39| priority | category | impact |
40|----------|----------|--------|
41| 1 | never patterns | critical |
42| 2 | choose the mechanism | critical |
43| 3 | measurement | high |
44| 4 | scroll | high |
45| 5 | paint | medium-high |
46| 6 | layers | medium |
47| 7 | blur and filters | medium |
48| 8 | view transitions | low |
49| 9 | tool boundaries | critical |
50
51## quick reference
52
53### 1. never patterns (critical)
54
55- do not interleave layout reads and writes in the same frame
56- do not animate layout continuously on large or meaningful surfaces
57- do not drive animation from scrollTop, scrollY, or scroll events
58- no requestAnimationFrame loops without a stop condition
59- do not mix multiple animation systems that each measure or mutate layout
60
61### 2. choose the mechanism (critical)
62
63- default to transform and opacity for motion
64- use JS-driven animation only when interaction requires it
65- paint or layout animation is acceptable only on small, isolated surfaces
66- one-shot effects are acceptable more often than continuous motion
67- prefer downgrading technique over removing motion entirely
68
69### 3. measurement (high)
70
71- measure once, then animate via transform or opacity
72- batch all DOM reads before writes
73- do not read layout repeatedly during an animation
74- prefer FLIP-style transitions for layout-like effects
75- prefer approaches that batch measurement and writes
76
77### 4. scroll (high)
78
79- prefer Scroll or View Timelines for scroll-linked motion when available
80- use IntersectionObserver for visibility and pausing
81- do not poll scroll position for animation
82- pause or stop animations when off-screen
83- scroll-linked motion must not trigger continuous layout or paint on large surfaces
84
85### 5. paint (medium-high)
86
87- paint-triggering animation is allowed only on small, isolated elements
88- do not animate paint-heavy properties on large containers
89- do not animate CSS variables for transform, opacity, or position
90- do not animate inherited CSS variables
91- scope animated CSS variables locally and avoid inheritance
92
93### 6. layers (medium)
94
95- compositor motion requires layer promotion, never assume it
96- use will-change temporarily and surgically
97- avoid many or large promoted layers
98- validate layer behavior with tooling when performance matters
99
100### 7. blur and filters (medium)
101
102- keep blur animation small (<=8px)
103- use blur only for short, one-time effects
104- never animate blur continuously
105- never animate blur on large surfaces
106- prefer opacity and translate before blur
107
108### 8. view transitions (low)
109
110- use view transitions only for navigation-level changes
111- avoid view transitions for interaction-heavy UI
112- avoid view transitions when interruption or cancellation is required
113- treat size changes as potentially layout-triggering
114
115### 9. tool boundaries (critical)
116
117- do not migrate or rewrite animation libraries unless explicitly requested
118- apply these rules within the existing animation system
119- never partially migrate APIs or mix styles within the same component
120
121## common fixes
122
123```css
124/* layout thrashing: animate transform instead of width */
125/* before */ .panel { transition: width 0.3s; }
126/* after */ .panel { transition: transform 0.3s; }
127
128/* scroll-linked: use scroll-timeline instead of JS */
129/* before */ window.addEventListener('scroll', () => el.style.opacity = scrollY / 500)
130/* after */ .reveal { animation: fade-in linear; animation-timeline: view(); }
131```
132
133```js
134// measurement: batch reads before writes (FLIP)
135// before — layout thrash
136el.style.left = el.getBoundingClientRect().left + 10 + 'px';
137// after — measure once, animate via transform
138const first = el.getBoundingClientRect();
139el.classList.add('moved');
140const last = el.getBoundingClientRect();
141el.style.transform = `translateX(${first.left - last.left}px)`;
142requestAnimationFrame(() => { el.style.transition = 'transform 0.3s'; el.style.transform = ''; });
143```
144
145## review guidance
146
147- enforce critical rules first (never patterns, tool boundaries)
148- choose the least expensive rendering work that matches the intent
149- for any non-default choice, state the constraint that justifies it (surface size, duration, or interaction requirement)
150- when reviewing, prefer actionable notes and concrete alternatives over theory