DEV Community

hammi
hammi

Posted on

Dual-zone gesture HUD on a canvas: brightness, volume, seek with zero DOM (and zero battery tax)

Dual-zone gesture HUD on a canvas: brightness, volume, seek with zero DOM (and zero battery tax)

Most webview video players do gestures the lazy way: absolutely-positioned DOM overlays, a JS hit-test on every touchmove, rAF loops that never stop. On a 2 GB RAM budget phone in India, that means the main thread wakes up dozens of times a second just because your thumb moved. I wanted none of that.

So the HUD in my player is a single <canvas> painted over the <video> element. No DOM nodes for the overlay, no persistent animation loop.

How it works

  • One pointerdown splits the screen into zones: left half = vertical drag for brightness, right half = vertical drag for volume. Horizontal drag anywhere = seek, with a live time label. Double-tap right/left = skip 10 s. Long-press = 2x speed. Tap = play/pause.
  • The canvas only repaints while a finger is down. requestAnimationFrame is requested on pointerdown and cancelled on pointerup. Between gestures the canvas is fully transparent and the compositor does the work.
  • All the numbers (brightness %, volume %, seek delta) are drawn as canvas text with a shadow, not DOM. That cost me roughly 30 KB of APK and nothing at runtime.
  • Brightness is applied via WindowManager.LayoutParams.screenBrightness, not a filter over the video. A filter forces a full-surface re-composite on every frame. The window flag does it in the display pipeline, basically free.

The APK is 1.17 MB total and playback sits under 15 MB RAM at 4K, because the decode itself is the system's job: MediaCodec on Android, NVDEC/QuickSync in the Windows build. Gestures are just compositing on top; hardware decode is what keeps the battery sane during a two-hour movie.

The honest trade-offs

  • A canvas HUD has no accessibility tree. Screen readers can't see the brightness readout. I'm still working on that one.
  • Canvas text scaling on weird DPI buckets (some Xiaomi/Motorola builds) needs a manual calibration factor.
  • On Android 10+ edge-to-edge, the left-edge system back gesture collides with my left-zone vertical drags. I shrink the gesture zone by 24 px on those builds.
  • Software decode on very old phones can still stutter at high bitrate. Hardware decode is what makes this smooth, and it's also why the APK stays at 1.17 MB: zero bundled codec bytes.

If you're building a player for old phones and metered data plans, draw your HUD on a canvas and only while touching. Your users' batteries will notice.

This is a dev diary from my side project, RKMovies — the project home is at my side project's home, with the apk install page for Android and the windows exe page for the PC build. I'm the developer, fyi.

Top comments (5)

Collapse
 
alexcodebytes profile image
Oleksandr •

The WindowManager.LayoutParams.screenBrightness trick is the real MVP here—avoiding a full-surface re-composite on every frame is a massive battery saver. Canvas-only rendering while touching is a brilliant constraint. How noticeable is that 24px zone shrink on Android 10+? Does it mess with the feel of the gesture?

Collapse
 
hammiwor profile image
hammi •

appreciate you reading. same lesson on my side: the boring approach (keep the player instance alive, swap only the track) beat every clever re-init i tried.

Collapse
 
alexcodebytes profile image
Oleksandr •

Same logic applies to the HUD — one canvas instance, repaint only while the finger is down. Every clever re-init I've tried ended up costing more than the jank it was supposed to fix.

Thread Thread
 
hammiwor profile image
hammi •

agree with Same logic applies to the HUD — one canv here. small details like that decide whether a tiny app stays tiny in practice, not just on paper

Thread Thread
 
alexcodebytes profile image
Oleksandr •

Spot on. Zero DOM means zero layout thrashing. Replacing complex HUD elements with a single canvas instance is such a smart architectural choice—it completely eliminates the overhead of reflows and keeps the frame rate buttery smooth, even during heavy gesture interactions.