Home / Learn / How do I make an animated gauge or speedometer meter?

How do I make an animated gauge or speedometer meter?

A half-circle gauge shows a percentage or score at a glance, and fits dashboards and performance ratings. We draw an arc in SVG and fill it with stroke-dashoffset, with a real number in HTML that a screen reader can read.

Steps

  1. Draw two identical arcs with the A command in the d attribute: one for the grey track and one for the colored fill.
  2. Get the fill arc length with getTotalLength() and set stroke-dasharray to it.
  3. Set stroke-dashoffset = length x (1 - value/100) after two frames, with a CSS transition, so the arc fills to the value.
  4. Animate the number with requestAnimationFrame from 0 to the value, and put role="meter" with aria-valuenow, aria-valuemin and aria-valuemax on the wrapper.
  5. In RTL flip the drawing with scaleX(-1) so small values start on the right, and keep the number outside the SVG so it is not mirrored.

Sources

The code

HTML

<!doctype html>
<html lang="ar" dir="rtl">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<style>
  body { margin: 0; min-height: 100vh; display: grid; place-items: center; background: #0f172a; color: #fff; font-family: system-ui, "Segoe UI", Tahoma, sans-serif; }
  .gauge { position: relative; width: min(80vw, 320px); }
  svg { display: block; width: 100%; height: auto; }
  [dir="rtl"] svg { transform: scaleX(-1); }            /* low values start on the right in RTL */
  .track, .fill { fill: none; stroke-width: 16; stroke-linecap: round; }
  .track { stroke: #1e293b; }
  .fill { stroke: #22c55e; transition: stroke-dashoffset 1.4s cubic-bezier(.2,.8,.2,1); }
  .read { position: absolute; inset-inline: 0; bottom: 0; text-align: center; }
  .read b { display: block; font-size: 2.6rem; font-variant-numeric: tabular-nums; direction: ltr; }
  @media (prefers-reduced-motion: reduce) { .fill { transition: none; } }
</style>
<div class="gauge" role="meter" aria-valuemin="0" aria-valuemax="100" aria-valuenow="72" aria-label="نسبة الإنجاز">
  <svg viewBox="0 0 200 110" aria-hidden="true">
    <path class="track" d="M10 100 A90 90 0 0 1 190 100"/>
    <path class="fill" id="fill" d="M10 100 A90 90 0 0 1 190 100"/>
  </svg>
  <div class="read"><b id="num">0%</b><span>نسبة الإنجاز</span></div>
</div>
<script>
  const VALUE = 72;
  const fill = document.getElementById('fill'), num = document.getElementById('num');
  const len = fill.getTotalLength();
  const reduce = matchMedia('(prefers-reduced-motion: reduce)').matches;
  fill.style.strokeDasharray = len;
  fill.style.strokeDashoffset = reduce ? len * (1 - VALUE / 100) : len;
  requestAnimationFrame(() => requestAnimationFrame(() => { fill.style.strokeDashoffset = len * (1 - VALUE / 100); }));
  if (reduce) num.textContent = VALUE + '%';
  else {
    const t0 = performance.now();
    const tick = now => { const p = Math.min(1, (now - t0) / 1400); num.textContent = Math.round(VALUE * (1 - Math.pow(1 - p, 3))) + '%'; if (p < 1) requestAnimationFrame(tick); };
    requestAnimationFrame(tick);
  }
</script>
</html>

can't find the motion you need? Request it here

Related articles: