Part 01 · How the Browser Works
Layout, Paint and Composite
Render tree এর পরের তিনটা ধাপ: box এর মাপ বের করা, pixel ভরা আর GPU তে layer সাজানো, আর প্রতিটার খরচ কত।
⏱ 6 মিনিট · Station 4 / 125
The problem
Designer চায় ShopLite এর "deal of the day" card গুলো বাম দিক থেকে slide করে আসুক। প্রথম version এ left animate করা হলো। Designer এর MacBook এ smooth, Redmi phone এ আটকে আটকে চলে। একজন teammate শুধু এক লাইন বদলে transform animate করল, আর এখন সব জায়গায় smooth।
দেখতে একই রকম, কিন্তু খরচ একদম আলাদা। পার্থক্যটা হলো, কোন change browser এর pixel pipeline এর কোন step গুলো আবার চালাতে বাধ্য করে, তাও প্রতিটা frame এ।
The idea
Render tree তৈরি হওয়ার পর প্রতিটা frame pixel pipeline এর ভেতর দিয়ে যায়:
- Style: কোন element এ কোন CSS rule apply হবে, আর তাদের final value কী।
- Layout: প্রতিটা box এর size আর position (reflow ও বলে)।
- Paint: প্রতিটা element এর জন্য কী আঁকতে হবে সেটা record করা: text, color, border, image।
- Composite: page কে layer এ ভাগ করা, সেগুলো rasterise করা, আর GPU দিয়ে layer গুলো সাজিয়ে final image বানানো।
একটা change pipeline আবার চালায় শুধু সেই step থেকে, যেটায় সে প্রথম হাত দেয়। Geometry change শুরু হয় layout থেকে, color change paint থেকে, আর transform/opacity change সরাসরি composite এ চলে যেতে পারে।
How it works
- Style. কোনো class, inline style বা DOM বদলালে browser element গুলোকে dirty mark করে আর তাদের style আবার হিসাব করে। Element যত বেশি আর selector যত জটিল, খরচ তত বেশি।
- Layout. Box গুলো একে অপরের উপর depend করে। একটা card এর width depend করে grid এর উপর, height depend করে তার text এর উপর। তাই
left,width,height,margin,topবা font size বদলালে অন্য box ও সরে যেতে পারে। Browser tree এর affected অংশ আবার হিসাব করে। - Pre-paint আর paint. Browser layout হওয়া tree টা ঘুরে দেখে, কোন element কোন layer এ পড়বে ঠিক করে, আর display list record করে (drawing command, এখনো pixel না)।
color,backgroundবাbox-shadowবদলালে affected element গুলোর জন্য এটা আবার চলে। - Raster. Display list থেকে pixel হয়, tile ধরে ধরে, raster thread এ (অনেক সময় GPU তে)। Main thread এ না।
- Composite. Compositor thread layer গুলোর position ঠিক করে, আর GPU কে frame আঁকতে বলে। যে element এর নিজের layer আছে, সেটাকে সরানো (
transform), fade করা (opacity) বা scroll করা যায় শুধু layer টা কোথায় বসবে সেটা বদলে। কোনো style নেই, layout নেই, paint নেই। - Composite-only animation এ main thread লাগেই না. CSS
transformanimation compositor thread কে দিয়ে দেওয়া হয়। তাই main thread এ JavaScript busy থাকলেও animation smooth চলতে থাকে।
60 টা card 2 second ধরে আসা-যাওয়া করছে, CPU 4 গুণ slow। কোন version main thread কে সবচেয়ে বেশি busy রাখবে?
Visual
What each change costs
| তুমি বদলাও | Style | Layout | Paint | Composite | উদাহরণ |
|---|---|---|---|---|---|
| geometry | হ্যাঁ | হ্যাঁ | হ্যাঁ | হ্যাঁ | left, top, width, height, margin, padding, font-size |
| appearance | হ্যাঁ | না | হ্যাঁ | হ্যাঁ | color, background, box-shadow, border-radius |
| transform / opacity | হ্যাঁ (JS) বা না (CSS anim) | না | না | হ্যাঁ | transform: translate/scale/rotate, opacity |
| scroll | না | না | না | হ্যাঁ | compositor এর হাতে থাকা layer scroll করা |
Layer free না। প্রতিটা layer GPU memory খায় (2x density তে 400 x 300 এর একটা layer প্রায় 1.9 MB)। will-change: transform আগে থেকেই একটা layer বানিয়ে রাখে। এটা দাও শুধু যে কয়টা জিনিস animate হয় সেগুলোতে, সবকিছুতে না।
See it live
Measure করা হয়েছে Intel i3-10110U laptop এ (4 thread), 7 GB RAM, Ubuntu 26.04, Playwright 1.64.1 দিয়ে Chrome 151 headless, CPU 4 গুণ throttle করা, lab একই machine এ Docker এ।
animate.html 60 টা product card আঁকে, তারপর সেগুলো সরায় বা রং বদলায়। হয় JavaScript দিয়ে (একটা requestAnimationFrame loop প্রতিটা card এ style set করে), নয়তো CSS animation দিয়ে:
<!doctype html>
<html>
<head>
<title>layout, paint, composite</title>
<style>
body { margin: 0; font-family: system-ui; }
.card { position: relative; left: 0; width: 160px; height: 40px; margin: 4px; background: #ffcf70; border-radius: 6px; }
/* ?mode=left moves the cards by changing `left`: layout + paint every frame
?mode=color changes the background colour: paint every frame
?mode=transform moves them with `transform`: composite only */
@keyframes left { from { left: 0 } to { left: 400px } }
@keyframes color { from { background: #ffcf70 } to { background: #7cc4ff } }
@keyframes transform { from { transform: translateX(0) } to { transform: translateX(400px) } }
/* for the practice */
@keyframes opacity { from { opacity: 1 } to { opacity: 0.3 } }
@keyframes blur { from { transform: translateX(0); filter: blur(0) } to { transform: translateX(400px); filter: blur(1px) } }
</style>
</head>
<body>
<script>
const q = new URLSearchParams(location.search);
const mode = q.get("mode") ?? "left";
const cards = [];
for (let i = 0; i < 60; i++) {
const d = document.createElement("div");
d.className = "card";
d.textContent = "Product " + (i + 1);
// ?driver=css: a CSS animation. ?driver=js: JavaScript sets the property every frame.
if (q.get("driver") !== "js") d.style.animation = `${mode} 1s linear infinite alternate`;
document.body.append(d);
cards.push(d);
}
if (q.get("driver") === "js") {
const set = {
left: (d, p) => (d.style.left = p * 400 + "px"),
color: (d, p) => (d.style.background = `hsl(${40 + p * 160} 90% 70%)`),
transform: (d, p) => (d.style.transform = `translateX(${p * 400}px)`),
}[mode];
const tick = (now) => {
const p = Math.abs(((now / 1000) % 2) - 1);
for (const d of cards) set(d, p);
requestAnimationFrame(tick);
};
requestAnimationFrame(tick);
}
</script>
</body>
</html>
tools/animate.mjs প্রতিটা version এর জন্য 2 second এর একটা performance trace record করে, আর main-thread event গুলো যোগ করে:
// node animate.mjs: 60 cards animated three ways for 2 seconds, CPU throttled 4x,
// first by JavaScript (a property set every frame), then by a CSS animation.
// Which pipeline steps does the main thread run for each?
import { open, trace, mainThreadSummary, WEB, ms } from "./lib.mjs";
const steps = ["UpdateLayoutTree", "Layout", "PrePaint", "Paint", "Layerize"];
for (const driver of ["js", "css"]) {
console.log(`\n${driver === "js" ? "JavaScript sets" : "CSS animation of"} style layout pre-paint paint main thread busy`);
for (const mode of ["left", "color", "transform"]) {
const { browser, page } = await open({ cpu: 4 });
await page.goto(`${WEB}/x/animate.html?mode=${mode}&driver=${driver}`, { waitUntil: "load" });
await page.waitForTimeout(500);
const events = await trace(browser, page, () => page.waitForTimeout(2000));
await browser.close();
const s = mainThreadSummary(events, [...steps, "RunTask"]);
const cell = (k) => `${String(s[k].count).padStart(3)}x ${ms(s[k].ms).padStart(7)}`.padEnd(14);
console.log(`${mode.padEnd(17)} ${cell("UpdateLayoutTree")}${cell("Layout")}${cell("PrePaint")}${cell("Paint")}${ms(s.RunTask.ms).padStart(6)} of 2000 ms`);
}
}
JavaScript sets style layout pre-paint paint main thread busy
left 120x 71 ms 121x 39 ms 121x 57 ms 7381x 355 ms 679 ms of 2000 ms
color 120x 65 ms 0x 0 ms 121x 16 ms 7381x 290 ms 449 ms of 2000 ms
transform 120x 75 ms 0x 0 ms 120x 21 ms 0x 0 ms 306 ms of 2000 ms
CSS animation of style layout pre-paint paint main thread busy
left 121x 162 ms 121x 53 ms 121x 59 ms 7381x 348 ms 820 ms of 2000 ms
color 0x 0 ms 0x 0 ms 0x 0 ms 0x 0 ms 4 ms of 2000 ms
transform 0x 0 ms 0x 0 ms 0x 0 ms 0x 0 ms 5 ms of 2000 ms
Output টা পড়ি (2 s এ প্রায় 120 টা frame, তাই "120x" মানে প্রতি frame এ একবার):
- JS দিয়ে
leftset: প্রতি frame এ সব step চলে। Paint চলেছে 7,381 বার, মানে প্রতি frame এ প্রতিটা card এর জন্য একবার। 2 second এর মধ্যে main thread busy ছিল 679 ms। - JS দিয়ে
colorset: layout নেমে গেছে 0 তে। কিন্তু প্রতিটা card এর paint এখনো চলছে: 449 ms busy। - JS দিয়ে
transformset: layout আর paint দুইটাই 0। বাকি আছে শুধু JavaScript নিজে আর style recalculation: 306 ms। - CSS দিয়ে
transformanimation: 2 second এ main thread এর সময় মাত্র 5 ms। পুরো কাজটা করে compositor thread। - CSS দিয়ে
leftanimation সবচেয়ে খারাপ (820 ms)। Property টার নিজেরই layout লাগলে CSS animation কোনো কাজে আসে না। - CSS দিয়ে
background-coloranimation এও এই Chrome build এ main thread এ কোনো কাজ দেখা যায়নি। মানে Chrome এটাও main thread এর বাইরে চালিয়েছে। তবে সব জায়গায় এটার উপর ভরসা কোরো না।transformআরopacityহলো সেই দুইটা property যেগুলো সব বড় browser compositor এ animate করে।
| যা animate হচ্ছে | Main thread busy (2000 ms এর মধ্যে) | left এর তুলনায় |
|---|---|---|
left (JS) | 679 ms | |
color (JS) | 449 ms | -34% |
transform (JS) | 306 ms | -55% |
transform (CSS) | 5 ms | -99% |
In real apps
- Carousel, drawer, toast আর skeleton shimmer নড়ে
transformদিয়ে, fade হয়opacityদিয়ে। Web Animations API আর Framer Motion, GSAP এর মতো library তোমাকে এই property গুলোর দিকেই ঠেলে দেয়। - Accordion খোলা আসলেই height বদলায়। তাই team গুলো FLIP technique use করে: শেষ অবস্থা একবার measure করে, তারপর একটা
transform: scaleanimate করে change টা নকল করে, আর শেষে real height set করে। - Hover এ product image zoom হয়
transform: scaleদিয়ে, বড়widthদিয়ে না। - Sticky header আর parallax এমনভাবে বানানো হয় যাতে scroll এর সময় compositor main thread কে না জিজ্ঞেস করেই ওগুলো সরাতে পারে।
What goes wrong in production
- অনেকগুলো element এ
height,topবাbox-shadowanimate করা: laptop এ ঠিক আছে, 4 গুণ slow phone এ jank। - সবকিছুতে
will-change: শত শত layer, GPU memory বেড়ে যায়, আর low-end phone এ browser layer drop করতে শুরু করে, বা tab টাই kill হয়ে যায়। - Composite-only animation হঠাৎ আর composite-only না: একই keyframe এ
filter: blur()যোগ করলে বাwidthবদলালে পুরো animation আবার main thread এ ফিরে আসে। - বড় paint area: full-screen gradient বা বিশাল
box-shadowযেটা প্রতি frame এ repaint হয়, সেটা layout এর চেয়েও বেশি খরচ করতে পারে।
Key takeaway
প্রতিটা change pipeline আবার চালায় প্রথম যে step এ সে হাত দেয় সেখান থেকে: geometry layout থেকে, color paint থেকে, transform আর opacity শুধু composite এ। Animate করো transform আর opacity, পারলে CSS animation হিসেবে। আর প্রতি frame এর কাজ থেকে layout আর paint দূরে রাখো।
Practice
animate.html এ তোমার জন্য আরো দুইটা keyframe আছে: opacity (1 থেকে 0.3 পর্যন্ত fade) আর blur (একই transform move, সাথে filter: blur() 0 থেকে 1px)। CSS animation হিসেবে প্রতিটার main-thread time predict করো। তারপর ?mode=opacity&driver=css আর ?mode=blur&driver=css দিয়ে measure করো (animate.mjs এর list এ যোগ করে)।
Answer
opacity চলে transform এর মতোই: compositor layer টা fade করে। আমরা 2 second এ main-thread time পেয়েছি 7 ms, কোনো style, layout বা paint event নেই।
blur হলো surprise। একই transform keyframe এ একটা filter যোগ করতেই পুরো animation আবার main thread এ ফিরে এসেছে: style recalculation চলেছে 121 বার (262 ms), আর main thread busy ছিল 777 ms, প্রায় left animate করার মতোই খারাপ। Keyframe এ একটা বাড়তি property compositor path টা বাতিল করে দিতে পারে। তাই rule এর উপর ভরসা না করে trace দেখো। Browser version বদলালে এসব detail ও বদলায় (উপরের background-color row টা দেখো)।
Station stamp
Chapter শেষ করো
পড়া শেষ হলে Complete চাপো, progress save হবে। XP শুধু Pro তে — Pro থাকলে এই chapter এ +10 XP পেতে।
Pro নিয়ে XP পাও →