This project is in early development stage. It’s not ready for production use.
red-otter with a package manager of your choice. For example with NPM:
npm i red-otter
A flexbox (think CSS or Yoga) layout engine that comes with its own TTF font parser, text rasterizer, UI renderer, and a declarative UI API (think React).
…a web/mobile developer – it’s like a mini Flutter. A sort of a framework for rendering UIs which fully recreates the whole stack on top of the graphics API.
While it doesn’t introduce anything new, all other possible solutions to this problem are either heavy C++ libraries or… entire web browsers. If you need a layout engine, Facebook Yoga is often used but it requires loading either via asm.js or WASM (See Satori).
If you are working on a web browser, operating system, game engine, a game with AAA-grade UI, Red Otter might be very useful.
Every browser is essentially a different platform. There are differences in how basic HTML elements are implemented, there are literally thousands small quirks in behavior coming from decades of legacy code that needs to be supported. If you ever worked on a long-lived web application, you for sure have encountered a lot of code written just to achieve the same behavior on IE, Safari and the rest (notable example: clicking a button in Safari doesn’t give it focus). What if… you could ditch the platform and become the platform?
Red Otter is not meant to be used for creating websites. By rendering your own UI and text you prevent users from using screen readers, automatic translations, high-contrast mode etc. Use it only for applications where it is implied that those capabilities are not needed or otherwise would not be available.
I always loved recreating things from scratch to learn how they work (see my blog). So eventually time came for complex UIs.
This resonates well with the recent trend of creating new software that attemps to solve problems that were deemed too hard to be ever approach again, especially by a small team. Good example is a new web browser Ladybird and a new operating system Serenity OS.
And finally, it was just a fun set of problems to work on.
When creating the whole tech stack from scratch, there are infinitely many possible choices so one could ask why recreate something that already exists and has problems.
I tried to find an example of IMGUI library with styling capabilities on par with what I expect and I didn’t manage to. I don’t think that there are any fundamental difficulties with that, after all it’s just a matter of passing more complex structs or calling more layout-defining functions. But I didn’t want to be the first to reach that point and instead I went with approach that I knew would work. The key word here is reasonable – I wanted to first achieve something that will work and only then experiment with new ideas as I have a good starting point. And I think that this is a reasonable approach.
This library works in a bit different set of constraints than a typical web application. When browser renders a web page, if all animations run under the screen’s frame rate time (usually 16.66ms) and all larger paints run under 100ms or however much time there is before user notices a part of UI and decides to interact with it – it’s perfect. There’s nothing really to improve there. It doesn’t matter if the actual rendering process took 1ms or 10ms as long as it happens in a given timeframe. If browser leans closer to the upper end but uses less CPU and consumes less battery – that’s great.
Games and 3D applications are different. Very different. It’s ok, even better if the game can use all computer resources effectively. CPU has 16 cores? Run on all 16. There’s never too fast for rendering a game. If all rendering work is done under 1ms – it leaves time for more complex game simulations and makes it more likely that the game will run on older PCs.
Even though this library does a lot of things, you don’t need to use it all. Modern JS bundlers do well with tree shaking so as long as you import only what you need, your bundle size will grow only by the amount of code you actually use.
It’s entirely up to you which parts to pick. Do you need a TTF parser? Good, we have one. Are you in need of some matrix calculations? We’ve got you covered. Do you need a full UI renderer for your WebGL game? We have that and all the needed pieces.
The library consists of several directories grouping different features.
/font– providing TTF parsing, font atlas generation.
/layout– the layout algorithm and other important functions.
/math– with a math library equipped with
/renderer– all renderers included with the library.
/utils– a classic bag of a bit of everything.
/widgets– a set of built-in UI widgets. Notably a (hopefully decent)
The pipeline of taking user defined components to the screen has multiple layers, separated based on how often they need to be run:
layout()– takes tree of nodes and calculates screen positions and sizes. Runs only when component tree changes.
compose()– takes tree of nodes after layout and calculates screen-space positions after including scrolling. Runs after user events.
paint()– takes tree of nodes after compose and prepares commands for the renderer.
Renderer#render()– given precise commands renderes styled rectangles and text to the screen.
As a rule of thumb, styling most closely resembles React Native which in turn resembles CSS.
The library is not bundled, just compiled with
tsc. The reason is that the source code is ’fluffy’ and it should always be bundled, tree-shaken and minified by the consumer to avoid shipping unused code.
Sandpack examples use a bundled version generated via Vite’s
build command which uses rollup.
Library examples (visit
bun run dev
cd docs && bun run dev