4 ms·
How do we even see anything on a browser? How do pixels turn into shapes, color, and movement? Every time we scroll, hover, or trigger an animation, the browse
by luciodale 1y ago
How do we even see anything on a browser?
How do pixels turn into shapes, color, and movement?
Every time we scroll, hover, or trigger an animation, the browser goes through a whole routine. It calculates styles, figures out layout, paints pixels, and puts everything together on screen. All of that happens in just a few milliseconds.
It’s kind of wild how much is happening behind what feels instant. And the way we write code can make that process either smooth and fluid or heavy and janky.
I wrote an article that walks through this step by step, with a small demo showing exactly how these browser processes work and how a few CSS choices can make a big difference.
- johncolanduoni 1y agoA lot of these don’t happen on scroll and hover under normal circumstances. For example smooth scrolling on touchscreens is implemented by only re-running the compositor on each frame, and using the existing GPU-resident bitmap of the text being scrolled. That’s why non-passive onscroll callbacks make scrolling suck, especially on mobile.
- immibis 1y agoWhat's not stated, is that we used to re-render the text at the bottom each time you scrolled up, and could still do it pretty fast (not quite in 16.67 milliseconds but we could have if computers had been today's speed), and in the meantime, we seem to have forgotten how to do that. Although we also have more pixels now, which probably changes things.
- toast0 1y ago> we used to re-render the text at the bottom each time you scrolled up, and could still do it pretty fast If you go back far enough, the IBM graphics cards used a text mode where text was accelerated by the graphics card, software would write a attribute byte and a data byte and the card had a bitmapped font to render text. VGA text mode at least could use hardware windowing so scrolling could also be accelerated; not every system used it, but you can set the start address and a line number where it returns to the start of the data area. That makes it easy to scroll without having to rewrite the screen buffer all at once. (If you set the stride to something that evenly divides the screen buffer and is at least as wide as your lines, it makes things even easier)
- vbezhenar 1y agoI think that many projects use wrong architecture, when it's a possibility for business code to block animations. IMO all the "user" code must run in a dedicated thread, completely decoupled from the rendering loop. This code can publish changes to a scene tree, performing changes, starting animations, and so on, but these changes ultimately are asynchronous. You want to delete an element from a webpage, but it'll not be deleted at this JS line, it'll be deleted at the next frame, or may be after that, if rendering thread is a bit busy right now. Animations must stay fluid and UI must react to the user input instantly. FPS must not drop. Browser does it wrong. Android GUI API does it wrong. World of Warcraft addons do it wrong.
- b_e_n_t_o_n 1y agoMultithreading in the browser kinda sucks though, it's too slow to share significant data between workers (threads), and if you try it with SharedArrayBuffer you eat the serialisation costs.
- gamer223 1y agoI’ve only ever seen competitive video games get the architecture right. They have the natural incentive of needing stable high FPS. Random GUI apps aren’t incentivized enough and so garbage leaks through. I die a bit every time a random GUI app stutters drawing 2D boxes
- Rohansi 1y agoThat's basically what React Native does/did and it's generally good but turns into a nightmare when you need to synchronize interactions between the two threads. 16ms is a long time - if your UI manipulations eat up most of that time then there's something wrong. Entire video games can run basically on one thread within that time and they do way more.
- vbezhenar 1y ago16ms is a long time by C standards. 16ms is not a long time, when interpreted languages like Lua are used. 16ms is not a long time, when GC can suddenly kick in and stop the thread for unspecified amount of time. 16ms is not a long time, when JIT can suddenly kick in and stop the thread for unspecified amount of time. GC and JIT are good techniques for server-style throughput workloads, when infrequent delays are not noticeable. For GUI, infrequent delays lead to skipped frames and stuttering. I don't think that it's reasonable to ask the world to develop GUI apps in C or Rust. Probably Swift is a good middle point between usability and predictable performance. But most GUI projects use slow languages and/or languages with GC.
- AndriyKunitsyn 1y agoIf that's wild to you, wait until you see video games.