Run a Glutter app in the browser
Compile a Glutter app to WebAssembly with glutter-web, serve it while you work, and make the folder that goes on a web server.
A Glutter app runs in the browser too. It is the same Go code, compiled with GOOS=js GOARCH=wasm by the standard Go toolchain (no TinyGo), and it draws into one canvas. The apps of the Playground are made this way.
This guide uses the app of Write your first Glutter app, but any Glutter app works. Run the commands in the folder of your module.
Serve it while you work#
go run github.com/rm4n0s/glutter/cmd/glutter-web serve .
glutter-web is a tool of Glutter. serve builds the app and serves it at http://localhost:8080/. Open that address in a browser. -addr :9000 chooses another port.
Build the folder to deploy#
go run github.com/rm4n0s/glutter/cmd/glutter-web build -o dist .
build writes these files into dist:
| File | What it is |
|---|---|
index.html | The page, with the loader |
wasm_exec.js | Go’s support code for WebAssembly |
app.wasm | The app |
app.wasm.gz | The app, gzipped |
It is a release build, stripped. -debug makes a debug build instead.
Put it on a web server#
Any static web server can serve the folder. Two things matter:
app.wasmmust be served with the typeapplication/wasm.- The app is large, so serve it compressed: send
app.wasm.gzwith the headerContent-Encoding: gzipto a browser that asks forapp.wasm.glutter-web servedoes this, and most web servers can be set to do it.
A release build of the counter is about 42 MB of WebAssembly, and 9.6 MB gzipped.
Put the app inside your own page#
By default the app fills the page. To put it in an element of a page of your own, set a CSS selector before the loader runs:
<script>globalThis.glutterConfig = { hostElement: "#app" };</script>
index.html has this line, with an empty configuration. Another way, which needs no change, is an <iframe> whose src is the app’s index.html. This site’s Playground does that.
Choose the renderer#
The page tries WebGPU, then WebGL 2, then a software renderer on a 2D canvas, once, at the first frame. To choose one by hand, add a parameter to the address:
http://localhost:8080/?GLUTTER_RENDERER=webgl
The values are webgpu, webgl and soft. A renderer that you ask for by name never falls back: a browser that cannot give it shows the error in the page.
Any parameter of the address whose name starts with GLUTTER_ reaches the program as the environment variable of that name.
What a page does not have#
A web page is not a desktop. An app asks foundation.IsWeb where it must differ:
import "github.com/rm4n0s/glutter/foundation"
// notesPlace says where an app keeps its notes: a page has no data directory.
func notesPlace() string {
if foundation.IsWeb {
return "in memory"
}
return "in the data directory"
}
The text editor of the Playground does this: on the web its notes live in memory until the page is closed.
Not on the web:
- a second window, and the system tray;
- file dialogs, and drag and drop out of the app;
- system fonts: the text uses the bundled Roboto and Material Icons, and the fonts that the app loads;
- real files: assets come from
embed.FS, as on the desktop; - accessibility.
Opening a link and notifications work. net/http goes through the browser’s fetch, so the browser’s CORS rules apply.
Size and speed#
The page loads one large module and runs on one thread. A click that builds a whole new screen takes tens of milliseconds. Measure a release build, which is the default.
Browsers#
Glutter’s browser tests run in Chromium, in Firefox, and in Safari on a Mac. In Firefox and in Chrome on Linux, WebGPU is off by default, so the page uses WebGL 2 there.
From Glutter 1.18, an app that draws with its own shaders, or shows a 3D view, runs on WebGL 2 as well as on WebGPU. Only the software renderer has no GPU device: there such a view shows its fallback.
Where to go next#
- The Playground has fourteen apps built this way.
- Glutter’s README has the full notes on browsers, and PERFORMANCE.md has the numbers.