compositing合成
比较合成友好属性、硬件加速提示与布局触发属性的渲染成本。
预览
合成会把已绘制的图层组合成最终画面;独立图层上的 transform 或 opacity 更新可跳过布局,并可能跳过重绘。
实现「合成」:比较合成友好属性、硬件加速提示与布局触发属性的渲染成本。 参数设为演示时长 520ms、位移 80px、对比属性 transform、曲线 linear。在状态发生变化时触发一次,并允许重新播放。使用语义化 HTML,CSS 作用域限定在 .motion-demo--compositing。为 prefers-reduced-motion 和 .force-reduced-motion 提供等价的低动态方案。
<div class="motion-demo motion-demo--compositing" data-motion="compositing" aria-label="合成 示例">
<article class="motion-surface">
<strong>合成</strong>
<span class="motion-line"></span>
<span class="motion-line"></span>
</article>
</div>.motion-demo.motion-demo--compositing {
box-sizing: border-box;
position: relative;
display: grid;
place-items: center;
width: min(100%, 34rem);
min-height: 15rem;
padding: 2rem;
overflow: hidden;
color: #18181b;
background: #fafafa;
border: 1px solid #e4e4e7;
border-radius: 16px;
isolation: isolate;
}
.motion-demo.motion-demo--compositing *, .motion-demo.motion-demo--compositing *::before, .motion-demo.motion-demo--compositing *::after {
box-sizing: border-box;
}
.motion-demo.motion-demo--compositing .motion-surface {
position: relative;
display: grid;
gap: 0.65rem;
width: min(100%, 18rem);
padding: 1.25rem;
background: #ffffff;
border: 1px solid #e4e4e7;
border-radius: 16px;
box-shadow: none;
}
.motion-demo.motion-demo--compositing .motion-surface strong { font-weight: 500; }
.motion-demo.motion-demo--compositing .motion-line {
display: block;
width: 100%;
height: 0.5rem;
background: #e4e4e7;
border-radius: 999px;
}
.motion-demo.motion-demo--compositing .motion-line:last-child { width: 62%; }
.motion-demo.motion-demo--compositing .motion-button {
position: relative;
min-height: 2.75rem;
padding: 0.75rem 1.1rem;
color: white;
font: inherit;
font-weight: 700;
background: #18181b;
border: 0;
border-radius: 999px;
cursor: pointer;
}
.motion-demo.motion-demo--compositing .motion-button:focus-visible { outline: 3px solid rgb(37 99 235 / 32%); outline-offset: 3px; }
.motion-demo.motion-demo--compositing .motion-action-row {
display: flex;
justify-content: center;
gap: 0.5rem;
margin-top: 0.85rem;
}
.motion-demo.motion-demo--compositing .motion-replay-scene {
width: min(100%, 22rem);
display: grid;
place-items: center;
}
.motion-demo.motion-demo--compositing .motion-pausable-scene {
width: min(100%, 22rem);
display: grid;
place-items: center;
}
.motion-demo.motion-demo--compositing button.motion-surface {
color: inherit;
font: inherit;
text-align: start;
cursor: pointer;
}
.motion-demo.motion-demo--compositing .motion-replay-button,
.motion-demo.motion-demo--compositing .motion-undo-button,
.motion-demo.motion-demo--compositing .motion-dismiss-button,
.motion-demo.motion-demo--compositing .motion-pause-button {
min-height: 2.75rem;
padding: 0.65rem 0.9rem;
color: #18181b;
font: 650 0.78rem/1 ui-sans-serif, system-ui, sans-serif;
background: #ffffff;
border: 1px solid #d4d4d8;
border-radius: 8px;
cursor: pointer;
}
.motion-demo.motion-demo--compositing .motion-replay-button:focus-visible,
.motion-demo.motion-demo--compositing .motion-undo-button:focus-visible,
.motion-demo.motion-demo--compositing .motion-dismiss-button:focus-visible,
.motion-demo.motion-demo--compositing .motion-pause-button:focus-visible,
.motion-demo.motion-demo--compositing [data-reorder-item]:focus-visible,
.motion-demo.motion-demo--compositing [data-swipe-target]:focus-visible {
outline: 3px solid rgb(37 99 235 / 32%);
outline-offset: 3px;
}
.motion-demo.motion-demo--compositing .motion-status {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
.motion-demo.motion-demo--compositing .motion-ripple-ink {
position: absolute;
width: 12px;
height: 12px;
pointer-events: none;
background: rgb(255 255 255 / 72%);
border-radius: 50%;
transform: translate(-50%, -50%) scale(0.95);
opacity: 0;
}
.motion-demo.motion-demo--compositing .motion-state-stack { position: relative; width: min(100%, 18rem); height: 9rem; }
.motion-demo.motion-demo--compositing .motion-state { position: absolute; inset: 0; display: grid; place-items: center; padding: 1rem; background: #ffffff; border: 1px solid #e4e4e7; border-radius: 16px; }
.motion-demo.motion-demo--compositing .motion-state--to { background: #18181b; color: white; }
.motion-demo.motion-demo--compositing .motion-scroll-track > span { color: #71717a; font: 600 0.72rem/1.2 ui-monospace, monospace; letter-spacing: 0.08em; text-transform: uppercase; }
.motion-demo.motion-demo--compositing .motion-orbit-item { display: grid; place-items: center; width: 2.5rem; height: 2.5rem; color: white; background: #2563eb; border-radius: 50%; }
.motion-demo.motion-demo--compositing .motion-text-stack { position: relative; width: min(100%, 18rem); min-height: 4rem; }
.motion-demo.motion-demo--compositing svg { width: min(100%, 20rem); height: auto; }
.motion-demo.motion-demo--compositing .motion-surface { position: relative; animation: motion-compositing 520ms linear infinite alternate; }
@keyframes motion-compositing { from { transform: translateX(-80px); } to { transform: translateX(80px); } }
@keyframes motion-compositing-reduced-fade {
from { opacity: 0.84; }
to { opacity: 1; }
}
.force-reduced-motion .motion-demo.motion-demo--compositing {
animation: motion-compositing-reduced-fade 160ms cubic-bezier(0.23, 1, 0.32, 1) both !important;
}
.force-reduced-motion .motion-demo.motion-demo--compositing *,
.force-reduced-motion .motion-demo.motion-demo--compositing *::before,
.force-reduced-motion .motion-demo.motion-demo--compositing *::after {
animation: none !important;
animation-delay: 0ms !important;
animation-iteration-count: 1 !important;
transition-property: opacity, color, background-color, border-color !important;
transition-duration: 160ms !important;
transition-delay: 0ms !important;
scroll-behavior: auto !important;
}
.force-reduced-motion .motion-demo.motion-demo--compositing .motion-button,
.force-reduced-motion .motion-demo.motion-demo--compositing [data-reorder-item],
.force-reduced-motion .motion-demo.motion-demo--compositing [data-swipe-target],
.force-reduced-motion .motion-demo.motion-demo--compositing [data-spring-target] {
transform: none !important;
}
.force-reduced-motion .motion-demo.motion-demo--compositing .motion-path { stroke-dashoffset: 0 !important; }
.force-reduced-motion .motion-demo.motion-demo--compositing .motion-word--from { opacity: 0 !important; visibility: hidden; }
.force-reduced-motion .motion-demo.motion-demo--compositing .motion-word:not(.motion-word--from) { opacity: 1 !important; filter: none !important; transform: none !important; }
@media (prefers-reduced-motion: reduce) {
.motion-demo.motion-demo--compositing {
animation: motion-compositing-reduced-fade 160ms cubic-bezier(0.23, 1, 0.32, 1) both !important;
}
.motion-demo.motion-demo--compositing *, .motion-demo.motion-demo--compositing *::before, .motion-demo.motion-demo--compositing *::after {
animation: none !important;
animation-delay: 0ms !important;
animation-iteration-count: 1 !important;
transition-property: opacity, color, background-color, border-color !important;
transition-duration: 160ms !important;
transition-delay: 0ms !important;
scroll-behavior: auto !important;
}
.motion-demo.motion-demo--compositing .motion-button,
.motion-demo.motion-demo--compositing [data-reorder-item],
.motion-demo.motion-demo--compositing [data-swipe-target],
.motion-demo.motion-demo--compositing [data-spring-target] {
transform: none !important;
}
.motion-demo.motion-demo--compositing .motion-path { stroke-dashoffset: 0 !important; }
.motion-demo.motion-demo--compositing .motion-word--from { opacity: 0 !important; visibility: hidden; }
.motion-demo.motion-demo--compositing .motion-word:not(.motion-word--from) { opacity: 1 !important; filter: none !important; transform: none !important; }
}深入了解
术语与辨析
compositing合成
CompositingCompositing assembles painted layers into the final frame and can update transforms or opacity without repeating layout.
合成会把已绘制的图层组合成最终画面;独立图层上的 transform 或 opacity 更新可跳过布局,并可能跳过重绘。
will-changewill-change
will-changeThe will-change property warns the browser about an upcoming property change so it can prepare relevant rendering resources.
一种 CSS 渲染提示,告知浏览器某项属性即将变化,以便浏览器按需提前准备优化。
will-change 是浏览器优化提示;合成是浏览器把图层交给 GPU 处理的渲染阶段。
layout-thrashing布局抖动
Layout thrashingRepeated interleaving of layout reads and writes forces synchronous recalculation and consumes the frame budget.
交替读取布局信息并写入样式,会反复触发同步布局计算并耗尽帧预算。
布局抖动由反复触发布局计算造成;合成友好的 transform 和 opacity 可以绕开这类工作。
hardware-acceleration硬件加速
Hardware accelerationHardware acceleration uses compositor and GPU resources for eligible visual updates, helping frames meet display deadlines.
符合条件的视觉更新可利用合成线程和 GPU 资源,帮助画面赶上显示刷新期限。
硬件加速描述利用 GPU 保持动画流畅的策略;合成是浏览器把独立图层交给 GPU 处理的渲染阶段。
实现前检查
- 目的
- 比较合成友好属性与触发布局或绘制的属性,选择稳定的渲染路径。
- 频率
- 在新动效实现、性能回归和复杂页面集成时检查。
- 触发
- 由属性选择或性能实验触发,同时记录主线程、布局、绘制和合成活动。
- 进入 / 离开
- 实验对象直接切换属性方案,指标变化保持可读,避免附加过渡污染结果。
- 可打断性
- 测试可中断并重置图层状态,will-change 只在需要前短暂启用。
- 手势与键盘
- 手势测试按同样距离和速度驱动两种方案,确保比较公平。
- 减弱动效策略
- 低动态模式仍优先 transform 和 opacity,并减少持续图层占用。
可访问性
低动态模式仍优先 transform 和 opacity,并减少持续图层占用。
参数参考
| 参数 | 范围 / 选项 | 默认值 | 作用 |
|---|---|---|---|
duration | 300ms–1200ms | 520ms | 演示使用较长时长以便观察完整变化;生产界面的即时反馈应收敛到 300ms 内。 |
distance | 0px–180px | 80px | 控制元素移动的空间距离。 |
property | Transform / Left / Opacity | Transform | 选择对比属性。 |
ease | 柔和 / 清脆 / 平稳 / 匀速 | 匀速 | 控制速度随时间变化的方式。 |
交付前检查
- 区分 Compositing、will-change 与 Layout thrashing。
- 检查图层数量、内存和完成后的清理。