CKTIS / CSS
Zmogljivost izrisa v DevTools: posnetki, FPS, Paint flashing in Layers
Posnetki kažejo videz, FPS podaja hitrost izrisa sličic, Paint flashing označi izrisana območja, Layers pa pokaže sloje kompozicije. Noben od teh signalov ne pomeni, da lahko zmogljivost zagotovimo zgolj s CSS-jem.
Različica: Browser DevTools workflow; UI and feature availability vary by browser and version
Sintaksa
Performance, Screenshots, FPS, Paint flashing and Layers are DevTools diagnostics, not CSS syntax. Their names, availability and interfaces depend on the browser and its version; CSS specifications define the animation and media query used in the example, not the diagnostic tools. The example uses `animation: move-left 2s linear infinite alternate;` inside `@media (prefers-reduced-motion: no-preference)`. Replace `move-left` with `move-transform` in that declaration for the comparison.Snemanje v Performance ustvari časovnico, na kateri lahko poiščeš dogodke izračuna slogov, postavitve (layout) in risanja (paint). Možnost Screenshots, če je na voljo, doda zaporedje posnetkov strani ob izbranih trenutkih. Poveži jih z dogodki na časovnici: posnetki ne zajamejo vsake sličice in ne merijo stroškov izrisa.
FPS pomeni število sličic na sekundo. Preveri tudi čase posameznih sličic: dobro povprečje lahko skrije občasno zatikanje. Pri 60 Hz je med osvežitvama zaslona približno 16,7 ms; to ni splošna časovna omejitev. Paint flashing poudari območja, ki se pravkar rišejo ali na novo izrisujejo, ne meri pa trajanja risanja. Tudi brez poudarkov lahko kompozitor sestavlja že izrisano vsebino.
Layers prikaže sloje, ki jih uporablja kompozitor, ne slojev kaskade CSS (@layer) ali kontekstov zlaganja. Vrednost transform, ki ni none, za običajno polje CSS ustvari kontekst zlaganja, vendar ne zagotovi ločenega sloja kompozicije. will-change je namig za optimizacijo, ne ukaz za ustvarjanje sloja; dodatni sloji lahko porabljajo pomnilnik.
Primerjaj enako gibanje z left in transform. Sprememba left lahko zahteva postavitev in risanje, animacija transform pa se lahko izvede samo s kompozicijo. To sta hipotezi za preverjanje, ne zagotovili CSS-ja. Ponovi meritve v enakih pogojih in preveri rezultate na ciljnih napravah. Imena in razpoložljivost orodij so odvisni od brskalnika in njegove različice.
Primeri
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Rendering performance comparison</title>
<style>
#square {
position: relative;
left: 0;
width: 48px;
height: 48px;
background: #2563eb;
}
@media (prefers-reduced-motion: no-preference) {
#square {
animation: move-left 2s linear infinite alternate;
}
}
@keyframes move-left {
from { left: 0; }
to { left: 120px; }
}
@keyframes move-transform {
from { transform: translateX(0); }
to { transform: translateX(120px); }
}
</style>
</head>
<body>
<div id="track"><div id="square"></div></div>
<p><output id="observation"></output></p>
<script>
const track = document.querySelector('#track');
const square = document.querySelector('#square');
const observation = document.querySelector('#observation');
const motion = matchMedia('(prefers-reduced-motion: no-preference)');
const variants = ['move-left', 'move-transform'];
const origin = track.getBoundingClientRect();
const { width, height } = square.getBoundingClientRect();
const { animationName, backgroundColor } = getComputedStyle(square);
const close = (a, b) => Math.abs(a - b) < 0.01;
function check(condition, message) {
if (!condition) throw new Error(message);
}
// Sample the actual CSS animation at known times, before profiling it.
function measure(name) {
square.style.animationName = name;
const [animation] = square.getAnimations();
check(animation?.animationName === name, `Missing animation: ${name}`);
animation.pause();
const { duration, direction, iterations } = animation.effect.getTiming();
check(duration === 2000 && direction === 'alternate' && iterations === Infinity,
`Unexpected timing: ${name}`);
const offsets = [0, 0.5, 1, 1.5, 2].map(step => {
animation.currentTime = step * duration;
const rect = square.getBoundingClientRect();
check(close(rect.top, origin.top), `Unexpected vertical movement: ${name}`);
return rect.left - origin.left;
});
check(offsets.every((x, i) => close(x, [0, 60, 120, 60, 0][i])),
`Unexpected horizontal movement: ${name}`);
return { distance: Math.round(offsets[2] - offsets[0]), seconds: duration / 1000 };
}
try {
check(width === 48 && height === 48 && backgroundColor === 'rgb(37, 99, 235)',
'Unexpected square size or color');
if (motion.matches) {
check(variants.includes(animationName), 'The media query did not activate a comparison variant');
let measured;
try {
measured = measure(animationName);
measure(variants.find(name => name !== animationName));
} finally {
// Remove the paused test animation and let the stylesheet start a fresh one.
square.style.animationName = 'none';
square.getAnimations();
square.style.removeProperty('animation-name');
}
const { distance, seconds } = measured;
observation.textContent = `When the media query matches, a blue ${width} × ${height} CSS-pixel square moves ${distance} CSS pixels to the right in ${seconds} seconds and back in ${seconds} seconds, repeating in either variant. Otherwise it stays at the starting position. No specific FPS, paint pattern, timing cost or layer configuration is expected across browsers and devices.`;
} else {
const rect = square.getBoundingClientRect();
check(animationName === 'none' && square.getAnimations().length === 0 &&
close(rect.left, origin.left) && close(rect.top, origin.top),
'The square is not static at its starting position');
observation.textContent = `The media query does not match. The blue ${width} × ${height} CSS-pixel square stays at the starting position.`;
}
} catch (error) {
observation.textContent = error.message;
throw error;
}
</script>
</body>
</html>Rezultat
When the media query matches, a blue 48 × 48 CSS-pixel square moves 120 CSS pixels to the right in 2 seconds and back in 2 seconds, repeating in either variant. Otherwise it stays at the starting position. No specific FPS, paint pattern, timing cost or layer configuration is expected across browsers and devices.
Spremeni samo ime animacije iz `move-left` v `move-transform`. Preveri, ali kvadrat v enakem času prepotuje enako razdaljo, nato primerjaj posnetka meritev. Če se pogoj `prefers-reduced-motion: no-preference` ne ujema, kvadrat miruje.
Povezane lekcije
- CSS · Lekcija 29 · Napredna · Različica: 2