A WebAssembly program whose loop waits for the paint event

A native UI program has a main loop that blocks until there's an event, and a modal dialog is a function that returns the answer. A web page can't block. JavaScript Promise Integration (JSPI) lets a WebAssembly import return a promise while the WebAssembly code sees an ordinary blocking call. I wanted to know two things: can a C program draw DOM elements with drawElementImage() from a loop that blocks on the canvas paint event, and can it show a modal dialog made of real buttons from a nested blocking loop?

Project settings

Opacity of the two fields: 100%

Delete project…

C says: ask() has not been called.

Delete this project? The C function ask() does not return until you choose.

Starting the program (3 KB of WebAssembly)…

The program hasn't started.

The name field and the notes field are real elements, and so are the two buttons in the dialog. The slider and the Delete button are rectangles drawn by the C program, which reads the pointer itself. The whole program is 170 lines of C with no libc and no toolkit library.

Show the code a blocking C loop on the paint event
  1. The elements go inside the canvas. The program refers to them by number, in document order.
  2. C: the main loop. It never returns. wait_event() is an import.
  3. C: a modal dialog as a function that returns the answer. It has its own loop and blocks in the same import, two calls deep.
  4. C: widgets the program draws itself.
  5. JavaScript: wait_event() returns a promise that the next paint event settles.
  6. JavaScript: the imports. WebAssembly.Suspending marks the one that may suspend. dom_draw() is drawElementImage().
  7. JavaScript: start it. WebAssembly.promising() wraps the export so that the call returns when the program first waits.
  8. JavaScript: queue pointer states, so a tap that begins and ends between two frames still reaches the program as a press and a release.
  9. CSS for the elements.
  10. All of ui.c. Built with clang --target=wasm32 -nostdlib.
  11. All of ui.js.

An edge filter on a live field: WebNN and WebAssembly

The plan for this page also asks whether a WebNN graph can run on the pixels of a live element. The button reads the pixels the program drew for the notes field and runs a Sobel edge filter on them twice: as a WebNN graph (one 3×3 convolution with two filters, then an L2 norm) and as a C function in the same WebAssembly module. After the first run the filter runs again on every frame the program draws, so typing in the field updates the picture.

Show the code the filter as a WebNN graph and as C
  1. WebNN. The graph takes the RGBA bytes from getImageData() as a uint8 tensor.
  2. C.
  3. JavaScript: call the C function while run() is suspended.
  4. JavaScript: run both and show the result.

Live checks

The checks load a second copy of the program in a small frame, so they don't touch the one above.

ResultExperimentDetail

Findings

This page follows DOM elements inside egui and Qt Quick apps, where the toolkits keep their own frame loops. Source: native/wasm-ui.