nano-ui-sdl-web: Browser backend for nano-ui with SDL_ttf text and WebGL2

[ graphics ] [ Propose Tags ] [ Report a vulnerability ]

Runs nano-ui views in a browser canvas. Built with GHC's WebAssembly backend, it shapes and rasterises text with SDL_ttf, as nano-ui-sdl does, and draws with WebGL2. Requires SDL3 and SDL3_ttf (built for wasm32-wasi for the browser) and pkg-config. The bundled Inter font is under the SIL Open Font License 1.1.


[Skip to Readme]

Flags

Manual Flags

NameDescriptionDefault
sdl

Build the backend. Needs SDL3, SDL3_ttf, and pkg-config.

Enabled

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.2.0.0
Change log CHANGELOG.md
Dependencies base (>=4.20 && <4.23), bytestring (>=0.12 && <0.13), containers (>=0.7 && <0.9), nano-ui (>=0.2 && <0.3), nano-ui-emit (>=0.2 && <0.3), nano-ui-sdl-web, nano-ui-ttf (>=0.2 && <0.3), primitive (>=0.9 && <0.10), text (>=2.1.2 && <2.2), vector (>=0.13 && <0.14) [details]
Tested with ghc ==9.10.3 || ==9.14.1
License MIT AND OFL-1.1[multiple license files]
Author goolord
Maintainer zacharyachurchill@gmail.com
Uploaded by goolord at 2026-12-03T05:00:44Z
Category Graphics
Home page https://github.com/goolord/nano-ui
Bug tracker https://github.com/goolord/nano-ui/issues
Source repo head: git clone https://github.com/goolord/nano-ui.git(packages/nano-ui-sdl-web)
Distributions
Executables nano-ui-sdl-web-demo
Downloads 0 total (0 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for nano-ui-sdl-web-0.2.0.0

[back to package description]

nano-ui-sdl-web

Browser backend for nano-ui, targeting GHC's WebAssembly backend. Text shaping uses the same SDL3_ttf engine as nano-ui-sdl (HarfBuzz + FreeType into a texture atlas) and frames draw with WebGL2.

{-# LANGUAGE CPP, OverloadedStrings #-}

import NanoUI
import NanoUI.Backend.SdlWeb (defaultSdlWebOptions, runSdlWebApp)

main :: IO ()
main = runSdlWebApp defaultSdlWebOptions (label "Hello")

#if defined(wasm32_HOST_ARCH)
foreign export javascript "nanoMain" main :: IO ()
#endif

runSdlWebAppReduce pairs model -> NanoUIE msg () with an update function just like runSdlAppReduce. Configure page title (webWindowSettings), themes or prefers-color-scheme tracking (webThemeFor), fonts, base size, and extra UI scale through SdlWebOptions.

Building for the browser

Building for WebAssembly requires Linux or macOS with ghc-wasm-meta (GHC 9.14, Cabal 3.14, and node on PATH for Template Haskell font embedding).

1. One-time WASI C libraries

Upstream SDL3 does not publish prebuilt wasm32-wasi static archives, so cross-compile FreeType, HarfBuzz, SDL3, and SDL3_ttf into ~/.nano-ui-wasi once per machine using scripts/build-wasi-deps.sh (shipped in nano-ui-sdl-web's data-files and source tarball):

source ~/.ghc-wasm/env
cabal unpack nano-ui-sdl-web
nano-ui-sdl-web-0.2.0.0/scripts/build-wasi-deps.sh

2. Project setup

Start from templates/web (nix flake init -t github:goolord/nano-ui#web), or configure an executable as a Wasm reactor exporting nanoMain:

executable my-webapp
  main-is:            Main.hs
  default-language:   GHC2024
  default-extensions: OverloadedStrings
  build-depends:
      base >=4.20 && <4.23
    , nano-ui ^>=0.2
    , nano-ui-sdl-web ^>=0.2
    , text ^>=2.1
  if arch(wasm32)
    default-extensions: JavaScriptFFI
    ghc-options:
      -no-hs-main
      -optl-mexec-model=reactor
      "-optl-Wl,--export=hs_init,--export=nanoMain"

Add split-sections: True in cabal.project so the linker strips unreachable code:

package *
  split-sections: True

3. Standard GHC Wasm pipeline

You can use Vite, esbuild, or your own HTML page directly. Build the .wasm binary and run GHC's post-link.mjs before wasm-opt strips the JSFFI custom section:

source ~/.ghc-wasm/env
export PKG_CONFIG_PATH=~/.nano-ui-wasi/lib/pkgconfig

wasm32-wasi-cabal build -j1 my-webapp
wasm="$(wasm32-wasi-cabal list-bin my-webapp)"
"$(wasm32-wasi-ghc --print-libdir)/post-link.mjs" -i "$wasm" -o dist-web/ghc_wasm_jsffi.js
wasm-opt --strip-debug -O2 "$wasm" -o dist-web/main.wasm

Serve three browser assets alongside main.wasm and ghc_wasm_jsffi.js:

  • web/nano-ui-sdl-web.js (loaded before starting Wasm) and web/main.js (ES module loader), both installed in nano-ui-sdl-web's Cabal data-files directory (share/web/).
  • @bjorn3/browser_wasi_shim from npm.
  • fonts/inter-extended.ttf (from web/fonts/), unless you set webFallbackFonts = [] or supply your own font URLs.

If you prefer a self-contained static directory with no JS bundler, scripts/build-web.sh [out-dir [cabal-target]] runs the steps above and copies the assets (CABAL_PROJECT and WASM_OPT_FLAGS override defaults):

PKG_CONFIG_PATH=~/.nano-ui-wasi/lib/pkgconfig \
  bash path/to/nano-ui-sdl-web/scripts/build-web.sh dist-web my-webapp
python3 -m http.server -d dist-web 8000

Browser-specific behavior

  • Fonts: Embedded Inter covers printable ASCII to keep .wasm small; extended Latin, Greek, Cyrillic, and symbols come from webFallbackFonts (fonts/inter-extended.ttf by default). WebFontSearch and webFontFamilies use Chromium's Local Font Access API (prompting lazily on the first canvas click/key, or explicitly via listWebFontFamilies) and an optional Fontsource CDN catalog (webFontService = Just Fontsource, off by default so apps make no third-party requests unless you opt in). Explicit fonts also load via WebFontUrl (requires CORS if cross-origin) or WebFontBytes.
  • File dialogs: openWebFiles, openWebFolder, and saveWebFile work on filenames and byte contents (showSaveFilePicker in Chromium, standard download fallback in Firefox and Safari). Launch them from a useTask started inside a click handler so the browser allows the picker gesture.
  • Canvas and windowing: Give <canvas> an explicit CSS size; the backend matches the WebGL drawing buffer to that box multiplied by devicePixelRatio and tracks resizes. setWindowTitle updates document.title, and clipboard reads see text copied or pasted within the page.

Documentation

License

MIT. The bundled Inter font is under the SIL Open Font License 1.1 (LICENSE-Inter); include LICENSE-Inter when distributing builds.