Occlusion without a guard in a Dear ImGui app
A DOM field in a Dear ImGui window still takes the pointer when a second ImGui window is drawn over it. The first ImGui page asks ImGui what is on top at every pointer event. I tried nine ways to say it in advance. Drag Window A or B over a field, pick a route, and read the table.
Starting the ImGui app (0.4 MB of WebAssembly)…
With the outline on, each dot is a sample point. the browser's hit test answers the field and the field is visible there. it answers the field under a window (a wrong hit). a wrong hit that the guard stops in script. the field is visible there and doesn't take the pointer (a lost hit).
| Field | Points under a window | Wrong hits | Lost hits | Visible points that hit |
|---|---|---|---|---|
| input | … | … | … | … |
| textarea | … | … | … | … |
| select | … | … | … | … |
| p + link | … | … | … | … |
| All four | … | … | … | … |
The app hasn't started.
Show the code nine ways to say "this window covers that field"
- The fields, two blocker elements and two overlay elements.
- C++: which windows cover the form, and where. The rectangle is the one in which ImGui gives the pointer to the window, which is 4 px larger on each side than the window it draws.
- JavaScript inside the C++ file: the report. The app only records the rectangles. The route decides what to do with them.
- The routes. Each one changes what a field asks for: a clip rectangle, a CSS value, or nothing.
- Applying a route after each ImGui frame. A field's geometry is sent only when it changed. The blockers are placed again after any field, because the last call wins.
- Route 3: the largest visible rectangle.
- Routes 5a and 5b: the visible part as rectangles.
- The pointer. One listener sends ImGui every event that the browser didn't give to a field. The guard's question to ImGui is the only part that depends on the route.
- C++: the guard's answer.
- The measurement.
- Route 4: what
canvasClipaccepts. Run by the live checks. - C++: a DOM element as an ImGui image.
- C++: the three windows.
- C++: the frame function. It draws only when something asked for frames.
- The table.
- Start the module.
- CSS for the fields and the blockers.
How it works
- The app reports, the page decides. After each ImGui frame the C++ side gives the page the rectangle of every field, the clip rectangle of the form window, and the rectangle of every window above the form in ImGui's z-order. The form is kept behind the other windows (
ImGuiWindowFlags_NoBringToFrontOnFocus). - A route is a function from those rectangles to what the browser is told. Routes 1 to 3 only call
updateElementGeometry()on the field. Routes 5 and 7 also write CSS or an attribute on the field. Route 6 places two more elements in the canvas. Route 8 moves two elements that are outside the canvas. - The pointer. ImGui has no input of its own in a browser, so one listener sends it every pointer event whose target isn't a field: the canvas, a blocker or an overlay. That listener is the same on every route. Only the guard adds a question to ImGui before it lets a field keep an event.
- The measurement. 96 sample points per field (16 by 6). For each point ImGui answers whether a window above the form takes the pointer there, and
elementFromPoint()answers which element the browser finds. A wrong hit is a covered point where the browser finds the field. A lost hit is an uncovered point where it does not. An attribute on the canvas is toggled first, because Chromium 157 keeps a stale hit-test answer after a geometry change. - When it runs. The table is filled once at the start, when the route changes, and a quarter of a second after the last frame in which a window moved. ImGui draws only when an event or a
paintasked for frames.
Live checks
The checks load a second copy of the app in a small frame, so they do not touch the one above. A route that does not work is a check that is expected to fail.
| Result | Experiment | Detail |
|---|
Findings
Measured in headless Chromium 157 on 2026-11-21, at a device scale of 2.625 with touch input. The numbers are from the live checks: 96 sample points on the textarea, window A in four positions (over all of it, over its right side, across its middle, over one corner).
canvasClipis one rectangle and can't be more. It is aDOMRectInitdictionary in the element's own coordinates. An array of twoDOMRects, an array of four numbers, aPath2D, aDOMRectListand aDOMQuaddon't throw: each is read as a dictionary with no members, which is an empty rectangle, and the field loses its whole hit box. A string or a number throwsTypeError. Aradiimember isn't read. A negative width is normalised. ANaNor infinite member leaves the earlier clip in place, and so does leavingcanvasClipout. A zero-size rectangle removes the hit box and leavesgetBoundingClientRect()where it was.- Route 3, the largest visible rectangle, has no wrong hits and loses part of the field. Window over all of the field or over one side: exact. Window across the middle: 36 of 72 visible points lost, because only one of the two halves can be the clip. Window over a corner: 14 of 68 lost, the short arm of the L. Over the four positions 74% of the visible points take the pointer.
- Route 5a, CSS
clip-pathon the field, is exact. The hit test of a drawable child followsclip-path:path(),path(evenodd, …)with a hole andpolygon()all work. 0 wrong hits and 0 lost hits in all four positions. The clipped part also leaves the field's texture (the texel readsrgba(0, 0, 0, 0)), which ImGui draws over, and a texel in the uncovered part is unchanged. The cost is onepaintevent and one texture upload each time the window moves: 10 of each for 10 moves. - Route 5b, CSS
mask, doesn't work. The mask cuts the texture and costs the same uploads, and the hit test ignores it: every covered point still hits the field. - Route 6, a blocker element, is exact and costs no repaint. An empty 1 by 1 px
<div drawable>is scaled to the window's rectangle withcanvasTransform. 0 wrong hits, 0 lost hits, 0paintevents and 0 uploads for 10 moves, and oneupdateElementGeometry()call per move. A press on the blocker is sent to ImGui, which drags the window. - Where two hit boxes overlap, the most recent
updateElementGeometry()call wins.z-indexon either element and the order of the elements in the DOM play no part. A call with the same values as before still raises the element. A repaint of the field does not. So the page places the blockers again, back to front, after any field was placed. - Routes 7a and 7b are all or nothing.
inertorpointer-events: nonewhile any part is covered removes the wrong hits and every hit on the visible part: 0% of the visible points take the pointer in the three partial positions.inertis the only route that also stopsfocus()and takes the field out of the Tab order. - Route 7c: one element has one hit box. A second
updateElementGeometry()call replaces the first. An element has one parent, so two drawable wrappers can't hold the same field. - Route 8, a plain element above the canvas, is exact and uses no canvas API. An absolutely positioned
<div>beside the canvas, moved with one style write per window move. It only works when the page that owns the canvas can put elements over it. - Route 2, the guard, leaves the browser's hit test wrong. All 190 covered points still hit the field. The listener asks ImGui, stops the event and parks the field off the canvas. With touch input the browser had already chosen the field: a tap on a covered point didn't focus it, but a drag that started there got
pointercanceland scrolled the page, becausetouch-action: noneon the canvas didn't apply to a touch that began on the field. On routes 3, 5a, 6 and 8 the same drag moved the window. - Tab still reaches a covered field. With the textarea fully covered, Tab from the input went to the textarea on every route except
inert.getBoundingClientRect()stayed where ImGui drew the field on every route. No route here takes a covered field out of the accessibility tree without also making a partly covered field unusable. - ImGui claims more than it draws.
FindHoveredWindowEx()gives a window the pointer up to 4 px outside its drawn edge (TouchExtraPadding,WindowBorderHoverPadding). The routes use that larger rectangle. ImGui windows here have square corners. A rounded window would needclip-pathon the blocker, which wasn't tried. - Not verified on a phone. The touch results are from CDP touch events in headless Chromium 157. An ImGui popup or combo over a field isn't in this app.
This page follows DOM elements in a Dear ImGui app. Source: native/toolkit-occlusion. Dear ImGui is MIT licensed.