Dev.to WebDev 🛠 Dev 👁 0 📖 2 min read

Building a HUD Speedometer for the Browser: Wake Lock, Fullscreen and Mirrored Text

A heads-up display speedometer is a neat trick: put your phone face-up on the dashboard at night, and the windscreen reflects the readout into your field of view. Building one in the browser takes three APIs and one CSS

A heads-up display speedometer is a neat trick: put your phone face-up on the dashboard at night, and the windscreen reflects the readout into your field of view. Building one in the browser takes three APIs and one CSS transform, and each has a gotcha.

Mirror the text, not the layout

The windscreen reflects the display, so everything must be flipped horizontally before it bounces back the right way round.

.hud {
  transform: scaleX(-1);
}

Apply it to the readout container, not to body. Flipping the whole document mirrors your controls too, and users end up tapping the wrong side of the screen to exit.

Keep the screen awake

A speedometer that dims after thirty seconds is useless. The Screen Wake Lock API fixes that:

let sentinel = null;

async function keepAwake() {
  try {
    sentinel = await navigator.wakeLock.request('screen');
  } catch (err) {
    // user declined, battery saver is on, or the API is unavailable
  }
}

The gotcha: the lock is released automatically whenever the page becomes hidden, and it does not come back on its own. Re-request it when the tab becomes visible again:

document.addEventListener('visibilitychange', () => {
  if (sentinel !== null && document.visibilityState === 'visible') keepAwake();
});

Wake Lock needs a secure context, so HTTPS or localhost, and it must be called while the document is visible. Calling it from a background tab throws.

Go fullscreen, from a gesture

Browser chrome ruins the effect and reflects as a bright band on the glass.

document.querySelector('.hud').requestFullscreen();

This must be called from a user gesture. Firing it on page load, or inside the geolocation success callback, is rejected: by then the gesture has expired. Wire it to the same button tap that starts the speedometer.

Design for reflected light

A reflection is dimmer and lower-contrast than the screen itself, and you are looking at it through glass at night.

  • Pure black background. Any lit pixel becomes a ghost on the windscreen.
  • A small number of very large, very bright glyphs. Thin fonts disappear in reflection; heavy weights survive.
  • One colour. Amber and white read best against road darkness. Avoid blue, which scatters and bleeds.
  • No animation on the number itself. Movement in the corner of a driver's eye is a genuine hazard, and a smoothed value updating 4 times a second looks calmer than one updating 20 times.

And the speed itself

Take it from position.coords.speed, which is Doppler-derived and far steadier than differencing coordinates, apply an accuracy gate and a small deadband so it reads a clean zero at traffic lights, and convert from m/s with * 3.6 for km/h.

A working version of all of this is at SpeediDash, and the HUD speedometer is the mirrored night-driving mode described above if you want to see the contrast choices in context before building your own.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.