From C to Browser: WebAssembly
From C to Browser: WebAssembly
Compiling native code to run in the browser.
The Challenge
MiniRT started as a C ray tracer using the minilibx graphics library. The goal was simple: get it running in a browser without requiring users to install anything.
Traditional approaches would have been:
- JavaScript reimplementation — Months of work, potential performance loss
- Native plugin — Dead technology, security concerns
- Server-side rendering — Latency, no interactivity
WebAssembly offered a fourth path.
The Compilation
Emscripten handles the heavy lifting. It compiles C to WASM while providing JavaScript bindings for browser APIs:
C Source → LLVM IR → Emscripten → WebAssembly + JS Glue
The key insight is that minilibx (the graphics library) had already been ported to work with raylib. And raylib had been compiled to WebAssembly before. The question wasn’t whether it was possible — it was whether I could wire it all together correctly.
What Gets Compiled
The WASM build produces four files:
| File | Purpose |
|---|---|
miniRT.wasm | The compiled ray tracer |
miniRT.js | Emscripten runtime and bindings |
miniRT.data | Bundled scene files (virtual filesystem) |
miniRT.html | Shell page (customizable) |
Virtual Filesystem
Emscripten creates a virtual filesystem. Pre-bundled scene files are embedded at compile time and available at their original paths when the WASM runs.
Result
A fully interactive ray tracer running at near-native speeds in any modern browser. The same C code, compiled once, runs everywhere.