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?
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
- The elements go inside the canvas. The program refers to them by number, in document order.
- C: the main loop. It never returns.
wait_event()is an import. - 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.
- C: widgets the program draws itself.
- JavaScript:
wait_event()returns a promise that the nextpaintevent settles. - JavaScript: the imports.
WebAssembly.Suspendingmarks the one that may suspend.dom_draw()isdrawElementImage(). - JavaScript: start it.
WebAssembly.promising()wraps the export so that the call returns when the program first waits. - JavaScript: queue pointer states, so a tap that begins and ends between two frames still reaches the program as a press and a release.
- CSS for the elements.
- All of ui.c. Built with
clang --target=wasm32 -nostdlib. - 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
- WebNN. The graph takes the RGBA bytes from
getImageData()as auint8tensor. - C.
- JavaScript: call the C function while
run()is suspended. - 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.
| Result | Experiment | Detail |
|---|
Findings
- Yes to both.
run()in C is an endless loop that is called once. It draws real elements withdrawElementImage(), blocks inwait_event(), and continues when the canvas firespaint.ask()blocks in the same import from a nested loop and returns 1 or 0 to its caller when a real<button>in the dialog is clicked. Measured in Chromium 157. - The JavaScript for JSPI is two calls.
new WebAssembly.Suspending(fn)on the import andWebAssembly.promising(run)on the export. The C source has no async code, no state machine and no Asyncify build step. The module is 3 KB. - The loop does no work between events. With nothing happening the program draws 0 frames while the page has 30 animation frames. One change to a field gives one paint event and one frame. A focused text field gives a frame for each blink of its caret, because the browser repaints the field.
- Drawing after the promise settles still counts as drawing in the paint event. The program continues in a microtask of the
paintlistener.drawElementImage()worked there in every run, andrequestPaint()called from there gives another paint event. - The first frame has to wait for the first paint event. Before it the elements have no paint record and
drawElementImage()throws. The page callsrun()after the firstpaint. - A blocking loop sees one pointer state per frame. A tap can begin and end between two paint events. The page queues each pointer change and gives the program one per frame, and asks for another paint while the queue isn't empty.
- A modal dialog needs the page to say what is under it. The browser hit tests an element wherever it was last drawn and doesn't know the program drew a dimmed layer and a dialog over it. The page sets
inerton every element the program draws as not interactive, and on the dialog while it isn't drawn. In a 2D canvas that was enough: noupdateElementGeometry()call was needed. - The module can be called while
run()is suspended. The edge filter is a second export of the same module and runs on the same memory while the main loop is stopped insidewait_event(). - The pixels of a drawn element can be read back and filtered.
getImageData()on the drawable canvas returns the pixels of the notes field, and the C filter takes about 10 ms for 900×220 pixels in the test browser. Chromium logs a notice that repeated readback is faster withwillReadFrequently. - WebNN wasn't measured.
navigator.mlis missing from both test browsers (Chromium 153 and 157), and the plan lists it as an origin trial that needs a GPU. The graph code on this page was run only against a JavaScript stand-in for the seven WebNN calls it uses, where its output matched the C filter. Whether it builds and runs in a real WebNN implementation is unknown. The live check for it is expected to fail wherenavigator.mlis missing. - The widgets the program draws have no DOM node. The slider and the Delete button can't be reached with the keyboard or a screen reader. The fields, the text and the dialog buttons can.
- Not verified on a phone. Tested in headless Chromium 157 at a 2.625 device scale with touch events: tapping, typing, dragging the slider and both dialog buttons. If the canvas loses its drawing context the page shows a message, and the program draws again when the context is restored.
This page follows DOM elements inside egui and Qt Quick apps, where the toolkits keep their own frame loops. Source: native/wasm-ui.