Uniform 发布 280 KB 的 WASM 布局与 GPU 渲染 UI 工具

原标题:Show HN: JavaScript web apps with WASM layout and GPU rendering in 280 KB
58AI 研判分

Uniform 是一套用 C 编写、在 Web 端编译为 WASM、在其他平台编译为原生代码的布局与渲染引擎,提供 JavaScript 与 C 两套 API,完整 Web 应用压缩后不足 280 KB。它放弃预设视觉风格,转而追求像素级精确布局、流畅动画与跨平台一致性,定位为原生 UI 工具的替代方案。

报道正文

This document was written by hand. No part of it was generated or modified by AI.

Introduction

Uniform provides tools for building responsive and beautiful user interfaces.

Despite implementing a full replacement for native UI tools, including text, layout, and rendering, a complete web application using Uniform is less than 280 KB (compressed).

Uniform does not have a visual identity. Instead, it makes it as easy as possible to express any visual identity. Its goal is to allow designers to implement their ideas precisely: pixel-perfect layout, smooth animations, and the flexibility to customize the tools when necessary. By taking low-level control, it behaves consistently across all supported platforms.

Uniform is easy to use, and has different APIs for different needs.
A JavaScript API is optimized to enable the fastest possible iteration speeds. With no build step, changes are visible immediately. It calls directly into the C core of the library, which compiles to WASM on the web, and native code on all other platforms.

There is also a simple, clean C API. It provides greater performance, but is just as usable as the JavaScript API. The library can be statically linked, or compiled from source together with the rest of the application. Compilation from source, including shaders, takes less than 1 second.

Uniform is currently in Preview, which means the API may change, and not all features are done. It supports iOS and macOS, where it renders via Metal, and the web, where it renders via WebGPU. Native support for Windows, Linux, and Android, where it will render via Vulkan, is planned.

Demo

Main Features

Powerful Layout. Uniform combines the best parts of constraint-based layout with an auto-layout system using stacks. Constraints are defined between parent and child, and work by pinning an anchor from the child to an anchor of the parent, plus an optional offset. Stacks support spacing between items, padding around the stack content, alignment, and distribution. Each item can define how it behaves as part of a stack: it either fills the available space, fits tightly around its content, or has a fixed size.

Layout is performed in fixed-point arithmetic, edges are snapped to pixel boundaries, and consistent spacing ensured. For text, Uniform supports text layout with character-level information accessible to the user, making custom text selection and visual effects possible. Custom fonts are supported and can be set, along with size, weight, and color, for specific ranges within a string.

Built for Custom Design. Most applications require exact control over visual design and behavior. Shipping pre-built components and providing selected customization points does not fulfill this requirement. Simple components like buttons are defined from scratch. Custom visuals, gestures, and behaviors take only a few lines of code. For more complex components like scrolling lists or text fields, the library provides the core functionality like event handling, gestures, physics, and animations. The user just reads the calculated result of what should be drawn in what location, and creates the visual elements with the appearance they want.

Shapes and Effects. Uniform uses SDF shapes as a central concept. SDF is short for Signed Distance Field. It means that every pixel has information about its distance to the closest edge, as well as the corresponding normal. This information can be used to enable advanced visual effects. Glass uses it to add refraction effects near the edge, vary the blur radius across the shape, and render realistic shadows. It also allows shapes to blend together.

Continuous Curves. The library renders perfectly smooth curves: continuous position, continuous slope, continuous curvature, and continuous curvature slope. These curves are used by default for rounded rectangles, which transition smoothly down to pills and circles. They can also be used for arbitrary polylines or polygons.

Low-Latency Input Processing. There is a platform-independent interface for touch, pencil, keyboard, and mouse input. For full control, raw events can be received on each interface element, hit-tested against the node hierarchy. For touch input, there is a powerful gesture system. Gestures enable negotiations between components to resolve what actions are performed. Common gestures like tap, long-press, pan, or pinch come with the library. These bundled gestures are implemented using the public API, so custom gestures have exactly the same level of control and interoperate seamlessly.

Advanced Display Technologies. Uniform supports extended dynamic range rendering with a simple interface. Solid colors, gradients, images, and more can just use values above 1.0 and will be rendered brighter on capable displays. The compositor can render with high bit depth, or apply dithering for improved color transitions on standard bit depth targets. High refresh rates are supported, too.

Simple API. Interface elements are created with a single function call. The result is an immutable node. Nodes can be organized into a hierarchy. Layout and rendering operate on such hierarchies and behave like pure functions. Creating and laying out nodes is a cheap operation that can be performed as often as needed. While most layouts can be realized with the built-in constraint and stack based tools, Uniform is designed to support any layout. The ability to perform layout on individual nodes and hierarchies, potentially repeatedly or with complex dependencies, makes this possible.

Native Integration. Uniform does not compete with platform-provided UI frameworks. It does not include copies of native controls. Instead, it provides tools to build controls that should eventually exceed native or browser quality. Because the library is shipped directly with the application, improvements can be delivered to all users immediately. At the platform boundaries, Uniform strives for perfect integration. Windows resize artifact-free and rendering happens with no additional latency compared to native UI.

Fast Iteration Speeds. For creative work, iteration times are highest priority. Uniform embraces this at every level. For rapid prototyping, the JavaScript API eliminates any build steps. C headers are minimal, and compiling the library from scratch takes less than a second. At runtime, every language calls into the common C core. For mos

(注:更多技术实现细节与完整 API 文档请查阅原项目 README)


作者发帖说明(HN):

Over the past year, I have been working to find a better way to write cross-platform apps.

The problems I am trying to solve:

  • Native frameworks only work on a subset of platforms, so a different implementation of the UI needs to be written per target. - They are updated at the same pace as the platform itself, which forces the developer to split the user base to be able to use new features, or wait a long time for bugs to get fixed. - They also force developers to use specific high-level languages, so they are not a good fit for apps with high performance requirements, like creative apps or games. - Cross-platform frameworks that rely on bundling a browser engine solve the problem of code reuse, but they inherit the limitations of HTML/CSS. - Those that take control over layout and rendering are not efficient and lightweight enough to be used on the web, and require the use of compiled languages.

Ideally, we would be able to bundle the UI framework directly with the application. This would allow developers to use new features and fix issues in the UI framework immediately. It would also allow ideas for improvements to compete in a natural way, rather than being decided by a committee.

Uniform is my idea for how we can solve these problems. This is the structure:

  • A layout and rendering engine written in C, compiled to WASM on the web and native code on all other platforms - A JavaScript API where each function call gets forwarded directly into the core, for best perfo...