Trail
import { useState } from 'react';
const W = 230;
const H = 90;
const N = 46;
const LENGTH_KM = 12;
// A fixed elevation profile in metres, sampled along the trail.
const ELEV = Array.from({ length: N }, (_, i) => {
const x = i / (N - 1);
return 620 + 300 * Math.sin(x * 2.4 - 0.9) + 140 * Math.sin(x * 6.1 + 1.2) + 50 * Math.sin(x * 13 + 2);
});
const yOf = (m: number) => H - 8 - ((m - 120) / 1000) * (H - 20);
const POINTS = ELEV.map((m, i) => `${((i / (N - 1)) * W).toFixed(1)} ${yOf(m).toFixed(1)}`);
const LINE = `M${POINTS.join('L')}`;
const AREA = `${LINE}L${W} ${H}L0 ${H}Z`;
const css = `
.slider-trail { display: grid; gap: 8px; width: 230px; }
.slider-trail__read { display: flex; justify-content: space-between; color: light-dark(#5f626a, #a7abb2); font-size: 11px; }
.slider-trail__read b { color: light-dark(#373a40, #f1f2f3); font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; }
.slider-trail__map { position: relative; height: 90px; }
.slider-trail__map svg { display: block; width: 100%; height: 100%; overflow: visible; }
.slider-trail__area { fill: light-dark(#f3f4f7, #1a1c1f); }
.slider-trail__walked { fill: color-mix(in oklab, light-dark(#50535b, #c6c8cd) 30%, transparent); clip-path: inset(0 calc((1 - var(--slider-trail-v)) * 100%) 0 0); }
.slider-trail__line { fill: none; stroke: light-dark(#cbced3, #393c41); stroke-width: 1.5; stroke-dasharray: 3 3; }
.slider-trail__line--done { stroke: light-dark(#373a40, #f1f2f3); stroke-width: 2; stroke-dasharray: none; clip-path: inset(0 calc((1 - var(--slider-trail-v)) * 100%) 0 0); }
.slider-trail__drop { stroke: light-dark(#50535b, #c6c8cd); stroke-width: 1; stroke-dasharray: 2 3; }
.slider-trail__hiker { fill: light-dark(#373a40, #f1f2f3); stroke: light-dark(#fbfcfd, #121417); stroke-width: 3; paint-order: stroke; }
.slider-trail__flag { fill: light-dark(#50535b, #c6c8cd); }
.slider-trail:has(.slider-trail__input:focus-visible) .slider-trail__hiker { stroke: light-dark(#50535b, #c6c8cd); }
.slider-trail__input { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: ew-resize; -webkit-appearance: none; appearance: none; background: none; }
.slider-trail__input::-webkit-slider-thumb { -webkit-appearance: none; width: 2px; height: 90px; }
.slider-trail__input::-moz-range-thumb { width: 2px; height: 90px; border: 0; }
`;
export function TrailSlider() {
const [v, setV] = useState(40);
const t = v / 100;
const f = t * (N - 1);
const i = Math.min(N - 2, Math.floor(f));
const m = ELEV[i] + (ELEV[i + 1] - ELEV[i]) * (f - i);
const x = (t * W).toFixed(1);
const y = yOf(m).toFixed(1);
const km = (t * LENGTH_KM).toFixed(1);
const metres = Math.round(m).toLocaleString('en-US');
return (
<>
<style>
{css}
</style>
<div className="slider-trail" style={{ '--slider-trail-v': t } as React.CSSProperties}>
<span className="slider-trail__read" aria-hidden="true">
<span>
<b>{km}</b> km
</span>
<span>
<b>{metres}</b> m
</span>
</span>
<span className="slider-trail__map">
<svg viewBox={`0 0 ${W} ${H}`} aria-hidden="true">
<path className="slider-trail__area" d={AREA} />
<path className="slider-trail__walked" d={AREA} />
<path className="slider-trail__line" d={LINE} />
<path className="slider-trail__line slider-trail__line--done" d={LINE} />
<line className="slider-trail__drop" x1={x} y1={y} x2={x} y2={H} />
<circle className="slider-trail__hiker" cx={x} cy={y} r="5" />
<path className="slider-trail__flag" d={`M${W - 4} ${yOf(ELEV[N - 1]).toFixed(1)}v-14l-8 4 8 4`} />
</svg>
<input
className="slider-trail__input"
type="range"
min={0}
max={100}
value={v}
onChange={(e) => setV(+e.target.value)}
aria-label="Distance along the trail"
aria-valuetext={`${km} kilometres, ${metres} metres high`}
/>
</span>
</div>
</>
);
}Markup where the component goes, CSS in your stylesheet, script before </body>. In Vue, Svelte or Angular, call initSliderTrail(el) on the mounted element instead.
<div class="slider-trail" style="--slider-trail-v:0.4">
<span class="slider-trail__read" aria-hidden="true">
<span><b>4.8</b> km</span>
<span><b>611</b> m</span>
</span>
<span class="slider-trail__map">
<svg viewBox="0 0 230 90" aria-hidden="true">
<path class="slider-trail__area" d="M0.0 51.1L5.1 50.6L10.2 50.3L15.3 50.4L20.4 50.7L25.6 51.1L30.7 51.6L35.8 52.0L40.9 52.4L46.0 52.6L51.1 52.6L56.2 52.3L61.3 51.9L66.4 51.3L71.6 50.6L76.7 49.8L81.8 49.0L86.9 48.3L92.0 47.6L97.1 47.2L102.2 46.8L107.3 46.7L112.4 46.7L117.6 46.8L122.7 47.0L127.8 47.2L132.9 47.3L138.0 47.2L143.1 46.8L148.2 46.2L153.3 45.1L158.4 43.7L163.6 41.9L168.7 39.7L173.8 37.2L178.9 34.5L184.0 31.7L189.1 28.8L194.2 26.0L199.3 23.3L204.4 21.0L209.6 19.0L214.7 17.4L219.8 16.3L224.9 15.7L230.0 15.4L230 90L0 90Z"></path>
<path class="slider-trail__walked" d="M0.0 51.1L5.1 50.6L10.2 50.3L15.3 50.4L20.4 50.7L25.6 51.1L30.7 51.6L35.8 52.0L40.9 52.4L46.0 52.6L51.1 52.6L56.2 52.3L61.3 51.9L66.4 51.3L71.6 50.6L76.7 49.8L81.8 49.0L86.9 48.3L92.0 47.6L97.1 47.2L102.2 46.8L107.3 46.7L112.4 46.7L117.6 46.8L122.7 47.0L127.8 47.2L132.9 47.3L138.0 47.2L143.1 46.8L148.2 46.2L153.3 45.1L158.4 43.7L163.6 41.9L168.7 39.7L173.8 37.2L178.9 34.5L184.0 31.7L189.1 28.8L194.2 26.0L199.3 23.3L204.4 21.0L209.6 19.0L214.7 17.4L219.8 16.3L224.9 15.7L230.0 15.4L230 90L0 90Z"></path>
<path class="slider-trail__line" d="M0.0 51.1L5.1 50.6L10.2 50.3L15.3 50.4L20.4 50.7L25.6 51.1L30.7 51.6L35.8 52.0L40.9 52.4L46.0 52.6L51.1 52.6L56.2 52.3L61.3 51.9L66.4 51.3L71.6 50.6L76.7 49.8L81.8 49.0L86.9 48.3L92.0 47.6L97.1 47.2L102.2 46.8L107.3 46.7L112.4 46.7L117.6 46.8L122.7 47.0L127.8 47.2L132.9 47.3L138.0 47.2L143.1 46.8L148.2 46.2L153.3 45.1L158.4 43.7L163.6 41.9L168.7 39.7L173.8 37.2L178.9 34.5L184.0 31.7L189.1 28.8L194.2 26.0L199.3 23.3L204.4 21.0L209.6 19.0L214.7 17.4L219.8 16.3L224.9 15.7L230.0 15.4"></path>
<path class="slider-trail__line slider-trail__line--done" d="M0.0 51.1L5.1 50.6L10.2 50.3L15.3 50.4L20.4 50.7L25.6 51.1L30.7 51.6L35.8 52.0L40.9 52.4L46.0 52.6L51.1 52.6L56.2 52.3L61.3 51.9L66.4 51.3L71.6 50.6L76.7 49.8L81.8 49.0L86.9 48.3L92.0 47.6L97.1 47.2L102.2 46.8L107.3 46.7L112.4 46.7L117.6 46.8L122.7 47.0L127.8 47.2L132.9 47.3L138.0 47.2L143.1 46.8L148.2 46.2L153.3 45.1L158.4 43.7L163.6 41.9L168.7 39.7L173.8 37.2L178.9 34.5L184.0 31.7L189.1 28.8L194.2 26.0L199.3 23.3L204.4 21.0L209.6 19.0L214.7 17.4L219.8 16.3L224.9 15.7L230.0 15.4"></path>
<line class="slider-trail__drop" x1="92.0" y1="47.6" x2="92.0" y2="90"></line>
<circle class="slider-trail__hiker" cx="92.0" cy="47.6" r="5"></circle>
<path class="slider-trail__flag" d="M226 15.4v-14l-8 4 8 4"></path>
</svg>
<input class="slider-trail__input" type="range" min="0" max="100" aria-label="Distance along the trail" aria-valuetext="4.8 kilometres, 611 metres high" value="40"/>
</span>
</div>.slider-trail { display: grid; gap: 8px; width: 230px; }
.slider-trail__read { display: flex; justify-content: space-between; color: light-dark(#5f626a, #a7abb2); font-size: 11px; }
.slider-trail__read b { color: light-dark(#373a40, #f1f2f3); font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; }
.slider-trail__map { position: relative; height: 90px; }
.slider-trail__map svg { display: block; width: 100%; height: 100%; overflow: visible; }
.slider-trail__area { fill: light-dark(#f3f4f7, #1a1c1f); }
.slider-trail__walked { fill: color-mix(in oklab, light-dark(#50535b, #c6c8cd) 30%, transparent); clip-path: inset(0 calc((1 - var(--slider-trail-v)) * 100%) 0 0); }
.slider-trail__line { fill: none; stroke: light-dark(#cbced3, #393c41); stroke-width: 1.5; stroke-dasharray: 3 3; }
.slider-trail__line--done { stroke: light-dark(#373a40, #f1f2f3); stroke-width: 2; stroke-dasharray: none; clip-path: inset(0 calc((1 - var(--slider-trail-v)) * 100%) 0 0); }
.slider-trail__drop { stroke: light-dark(#50535b, #c6c8cd); stroke-width: 1; stroke-dasharray: 2 3; }
.slider-trail__hiker { fill: light-dark(#373a40, #f1f2f3); stroke: light-dark(#fbfcfd, #121417); stroke-width: 3; paint-order: stroke; }
.slider-trail__flag { fill: light-dark(#50535b, #c6c8cd); }
.slider-trail:has(.slider-trail__input:focus-visible) .slider-trail__hiker { stroke: light-dark(#50535b, #c6c8cd); }
.slider-trail__input { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: ew-resize; -webkit-appearance: none; appearance: none; background: none; }
.slider-trail__input::-webkit-slider-thumb { -webkit-appearance: none; width: 2px; height: 90px; }
.slider-trail__input::-moz-range-thumb { width: 2px; height: 90px; border: 0; }function initSliderTrail(root) {
var W = 230;
var H = 90;
var N = 46;
var LENGTH_KM = 12;
// The same fixed elevation profile the markup was drawn from, in metres.
var ELEV = [];
for (var k = 0; k < N; k++) {
var x = k / (N - 1);
ELEV.push(620 + 300 * Math.sin(x * 2.4 - 0.9) + 140 * Math.sin(x * 6.1 + 1.2) + 50 * Math.sin(x * 13 + 2));
}
function yOf(m) { return H - 8 - ((m - 120) / 1000) * (H - 20); }
var input = root.querySelector('.slider-trail__input');
var drop = root.querySelector('.slider-trail__drop');
var hiker = root.querySelector('.slider-trail__hiker');
var reads = root.querySelectorAll('.slider-trail__read b');
function update() {
var t = input.value / 100;
var f = t * (N - 1);
var i = Math.min(N - 2, Math.floor(f));
var m = ELEV[i] + (ELEV[i + 1] - ELEV[i]) * (f - i);
var px = (t * W).toFixed(1);
var py = yOf(m).toFixed(1);
var km = (t * LENGTH_KM).toFixed(1);
var metres = Math.round(m).toLocaleString('en-US');
root.style.setProperty('--slider-trail-v', t);
drop.setAttribute('x1', px);
drop.setAttribute('x2', px);
drop.setAttribute('y1', py);
hiker.setAttribute('cx', px);
hiker.setAttribute('cy', py);
reads[0].textContent = km;
reads[1].textContent = metres;
input.setAttribute('aria-valuetext', km + ' kilometres, ' + metres + ' metres high');
}
input.addEventListener('input', update);
update();
return function () { input.removeEventListener('input', update); };
}
document.querySelectorAll('.slider-trail').forEach(initSliderTrail);Goal: Create a "Trail" slider.
Visual behavior: A trail-distance slider over an elevation profile: drag and the hiker walks the line, the walked part fills in and the distance and height above update.
Technique:
- A real <input type="range"> covers the profile, transparent, so dragging moves the hiker and the arrow, Page Up/Down, Home and End keys work natively. The map is aria-hidden.
- A fixed elevation profile is sampled once; the hiker's height is interpolated between samples, and the walked area and line are the same paths cut at the value with clip-path: inset().
- The aria-valuetext gives both the distance and the height.
Accessibility: A native range input with an aria-label and an aria-valuetext in words, so a screen reader announces the reading rather than a bare number; keyboard focus outlines the drawn thumb. The scene follows the value directly, with no transitions or loops, so there is no motion you did not make.
My stack: {{USER_STACK}}
My styling: {{USER_STYLING}}
My constraints: {{USER_CONSTRAINTS}}
My colors: neutral only. Black, white and grays, no hues. Use the color values above as written, or my project's closest neutral tokens.
Return a single self-contained component. No dependencies.Goal: Create a "Trail" slider.
Visual behavior: A trail-distance slider over an elevation profile: drag and the hiker walks the line, the walked part fills in and the distance and height above update.
Technique:
- A real <input type="range"> covers the profile, transparent, so dragging moves the hiker and the arrow, Page Up/Down, Home and End keys work natively. The map is aria-hidden.
- A fixed elevation profile is sampled once; the hiker's height is interpolated between samples, and the walked area and line are the same paths cut at the value with clip-path: inset().
- The aria-valuetext gives both the distance and the height.
Accessibility: A native range input with an aria-label and an aria-valuetext in words, so a screen reader announces the reading rather than a bare number; keyboard focus outlines the drawn thumb. The scene follows the value directly, with no transitions or loops, so there is no motion you did not make.
My stack: {{USER_STACK}}
My styling: {{USER_STYLING}}
My constraints: {{USER_CONSTRAINTS}}
My colors: use my project's existing color tokens. The color values above are grayscale placeholders. Map each one by its role (surface, text, border, accent, glow) to my theme, and keep their relative lightness so contrast and the effect still read.
Return a single self-contained component. No dependencies.Paste into Claude, ChatGPT or Cursor after filling in {{USER_STACK}}, {{USER_STYLING}} and {{USER_CONSTRAINTS}}. Neutral keeps the greys you see here; Match my project asks the model to map them onto your own colour tokens.
Free to use in any project, commercial included. MIT-0, no attribution required. Licence
About this component
A trail-distance slider over an elevation profile: drag and the hiker walks the line, the walked part fills in and the distance and height above update.
- Dependencies
- None
- Difficulty
- Intermediate
- Triggers
- state change, fill, draw
- Techniques
- svg, css variables, clip path
- Reduced motion
- Respected
- Theme
- Light and dark (follows your page's color-scheme)
A native range input with an aria-label and an aria-valuetext in words, so a screen reader announces the reading rather than a bare number; keyboard focus outlines the drawn thumb. The scene follows the value directly, with no transitions or loops, so there is no motion you did not make.