The Keyboard That Pushes the HUD Off the Screen
A chat box in an H5 game looks like a five-minute feature until you open it on a phone. The moment the on-screen keyboard slides up, the layout the game was built around stops existing: the viewport shrinks, 100vh become
A chat box in an H5 game looks like a five-minute feature until you open it on a phone. The moment the on-screen keyboard slides up, the layout the game was built around stops existing: the viewport shrinks, 100vh becomes a moving target, and anything pinned to the bottom jumps by the height of the keyboard.
On Android the keyboard does not resize the layout viewport the way a desktop window resize would. It mostly moves window.visualViewport, and resize arrives late or not at all on some builds. I tested this on a handful of H5 entries that put a name field in the middle of a round. aa666 apk redraws its HUD against visualViewport and stays where it was, while csx 8888 keeps the canvas at the old height and leaves a dead strip at the bottom that swallows taps.
The fix that held up was boring: listen to visualViewport resize and scroll, keep the game canvas on a fixed pixel height, and never autofocus an input. Autofocus is the trap, because the keyboard opens before the first frame and the boot sequence measures a viewport that is about to change. 6svip and 8svip both open their chat on an explicit tap only, and neither scrolls the page when the keyboard closes.
Closing is the half nobody tests. On a 4 GB phone the keyboard dismissal fires resize twice, once with the old height, so a HUD that re-measures on every event ends up half off screen. I now capture the height on focusout plus a 300 ms timer, and only accept a value that matches visualViewport.height. x88vip was the one build where this survived a full round with the keyboard opened six times. A text field is a layout event, not a UI detail.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.