DOM elements in a Dear ImGui app
Dear ImGui is an immediate-mode C++ toolkit used in game engines and tools. It draws everything itself and shows whatever texture the
application gives it. This app is Dear ImGui 1.92.5 compiled with Emscripten (0.4 MB). The email, country, date and notes fields and the
line of text are real elements, each shown with one ImGui::Image() call on a texture filled by texElementSubImage2D().
Tap a field to type. Scroll the window, drag the small window over a field, or open the ImGui combo above the fields.
Starting the ImGui app (0.4 MB of WebAssembly)…
The app hasn't started.
Show the code a DOM element as an ImGui image
- Put the elements inside the canvas that ImGui renders into. Each has a name in
data-imgui. - C++: the app.
DomImage("email", width)sits betweenImGui::SliderFloat()andImGui::BulletText(). - C++: the widget. Ask the browser for the height, upload if the browser repainted the element, call
ImGui::ImageWithBg(), then report the rectangle and the window's clip rectangle. - JavaScript inside the C++ file: element to texture.
GLctxis Emscripten's WebGL2 context. - Tell the browser where ImGui drew it.
- Events. ImGui has no platform backend here. The page sends it the pointer, and asks ImGui which window is on top before an event reaches an element.
- C++: the frame function. It draws only when an event or a
paintasked for frames. - Start the module.
- CSS for the elements.
- All of main.cpp.
Live checks
The checks load a second copy of the app in a small frame, so they don't touch the one above.
| Result | Experiment | Detail |
|---|
Findings
- Yes: one ImGui call shows a live element.
DomImage()is about 40 lines of C++ aroundImGui::ImageWithBg(). The browser side is about 100 lines of JavaScript inside the same file. Dear ImGui itself is unchanged. Measured in Chromium 157. - Everything ImGui does to an image applies to the element. The window scrolls it and clips it, a second window and the combo's popup cover it, and the slider changes its tint alpha.
- ImGui can say what is on top. The browser hit tests an element wherever its geometry is. Before an event reaches an element the page calls
FindHoveredWindowEx()(fromimgui_internal.h) for the pointer position. If another window or a popup is on top there, ImGui gets the event and the element's hit box is moved off the canvas. The same call each frame hides an element that is under another window at the pointer. - The window's clip rectangle maps to
canvasClip. A field that is half scrolled out of the window takes clicks only on its visible half, and a field scrolled fully out has no hit box. - ImGui's OpenGL backend blends straight alpha. The texture must not be premultiplied, the opposite of egui and Qt Quick on the earlier page.
- No GLFW or SDL was needed. The WebGL2 context comes from Emscripten's
html5.h, and the page sends pointer, wheel and key events toImGuiIOthrough five exported functions. The whole build is 0.4 MB of WebAssembly and 93 KB of JavaScript. - ImGui redraws only when asked. The frame function returns early unless an input event or a
paintevent asked for frames: 0 ImGui frames during 30 idle animation frames. A focused DOM field asks for frames at each caret blink. - ImGui has no touch scrolling. The page turns a vertical touch drag into wheel input unless ImGui reports that a control or a window title is held. A press on a window body sets an active id in ImGui (the window's move id) even when windows move only by their title bar, so the test has to exclude it.
- ImGui's own text field doesn't raise a phone keyboard.
ImGui::InputText()takes characters only from key events on the focused canvas. The DOM fields below it are the comparison. - A press on a visible element is also sent to ImGui. An image isn't an ImGui control, so the press only focuses the window and closes an open ImGui popup. Without this an open combo stayed open while the reader typed in a DOM field.
- A lost WebGL context isn't recovered. The page shows a message and asks for a reload. The app would need to recreate ImGui's font texture and each element texture.
- Not verified on a phone. Tested in headless Chromium 157 at a 2.625 device scale with touch events: typing in a field, the slider, the combo popup over a field, and touch scrolling. Where the native select and date pickers open, and IME, need a real phone.
This page follows DOM elements inside egui and Qt Quick apps. Source: native/imgui-dom. Dear ImGui is MIT licensed.