Trail that draws itself
A hiking route draws itself across a topographic map while a walker moves along it, distance and height count up, the height profile fills in and stops pop up as you pass them.
"use client";
import { useEffect, useRef, useState, type CSSProperties } from "react";
import "./trail-draw.css";
export type Stop = { at: number; name: string };
type TrailDrawProps = {
title: string;
/** SVG path of the route in a 600 x 340 box. */
path: string;
km: number;
/** Height in metres at evenly spaced points along the route. */
heights: number[];
stops: Stop[];
theme?: "light" | "dark";
};
const TOPO = [
"M60 250c40-60 120-90 190-70s120 70 200 40 120-90 110-130",
"M40 290c60-50 150-60 220-40s130 50 210 20 100-60 100-90",
"M200 130c30-40 90-55 130-30s40 80-10 100-110 10-120-70Z",
"M230 135c20-22 60-28 82-12s22 46-8 58-70 4-74-46Z",
"M258 140c10-10 30-12 40-4s10 22-4 28-34 2-36-24Z",
"M420 90c25-30 80-35 105-10s10 70-35 75-80-35-70-65Z",
"M445 95c14-16 46-18 60-4s4 40-20 42-46-20-40-38Z",
"M20 120c50 10 90-20 120-50",
"M60 330c80-40 170-30 240-10s150 10 240-30",
];
/**
* TrailDraw: a hiking route that draws itself across a topographic map.
* - The trail line is drawn with a moving walker; distance and height count up as it goes.
* - The height profile underneath fills in step with the map, and stops pop up as you pass them.
* - Scrub with the slider (or arrow keys) to walk back and forth, or press play to walk it again.
*/
export function TrailDraw({
title,
path,
km,
heights,
stops,
theme = "dark",
}: TrailDrawProps) {
const [p, setP] = useState(0);
const [playing, setPlaying] = useState(false);
const route = useRef<SVGPathElement>(null);
const root = useRef<HTMLDivElement>(null);
const [len, setLen] = useState(1000);
const [walker, setWalker] = useState({ x: 0, y: 0 });
const [pins, setPins] = useState<{ x: number; y: number }[]>([]);
const pos = useRef(0);
// Start walking the first time the map scrolls into view.
useEffect(() => {
const el = root.current;
if (!el) return;
const reduce = window.matchMedia(
"(prefers-reduced-motion: reduce)",
).matches;
const io = new IntersectionObserver(
([e]) => {
if (!e.isIntersecting) return;
io.disconnect();
if (reduce) setP(1);
else setPlaying(true);
},
{ threshold: 0.4 },
);
io.observe(el);
return () => io.disconnect();
}, []);
useEffect(() => {
if (!playing) return;
let raf = 0;
let last = performance.now();
const tick = (now: number) => {
pos.current = Math.min(1, pos.current + (now - last) / 5200);
last = now;
setP(pos.current);
if (pos.current >= 1) setPlaying(false);
else raf = requestAnimationFrame(tick);
};
raf = requestAnimationFrame(tick);
return () => cancelAnimationFrame(raf);
}, [playing]);
useEffect(() => {
pos.current = p;
}, [p]);
// Measuring the SVG path needs the DOM, so these run after render.
useEffect(() => {
const r = route.current;
if (!r) return;
const L = r.getTotalLength();
setLen(L);
setPins(stops.map((s) => r.getPointAtLength(L * s.at)));
}, [path, stops]);
useEffect(() => {
const r = route.current;
if (!r) return;
const L = r.getTotalLength();
const pt = r.getPointAtLength(L * p);
setWalker({ x: pt.x, y: pt.y });
}, [p]);
const idx = p * (heights.length - 1);
const lo = Math.floor(idx);
const hi = Math.min(heights.length - 1, lo + 1);
const h = Math.round(heights[lo] + (heights[hi] - heights[lo]) * (idx - lo));
const top = Math.max(...heights);
const bottom = Math.min(...heights);
const climb = heights
.slice(0, hi + 1)
.reduce(
(s, v, i, a) =>
i && v > a[i - 1] ? s + (v - a[i - 1]) * (i <= lo ? 1 : idx - lo) : s,
0,
);
const prof = heights
.map(
(v, i) =>
`${(i / (heights.length - 1)) * 600},${90 - ((v - bottom) / (top - bottom || 1)) * 74}`,
)
.join(" L");
return (
<div className="gu-trail" data-theme={theme} ref={root}>
<header className="gu-trail__head">
<h3>{title}</h3>
<dl>
<div>
<dt>Distance</dt>
<dd>{(km * p).toFixed(1)} km</dd>
</div>
<div>
<dt>Height</dt>
<dd>{h} m</dd>
</div>
<div>
<dt>Climbed</dt>
<dd>{Math.round(climb)} m</dd>
</div>
</dl>
</header>
<svg
className="gu-trail__map"
viewBox="0 0 600 340"
role="img"
aria-label={`Map of ${title}, ${km} km`}
>
<g className="gu-trail__topo">
{TOPO.map((d, i) => (
<path
key={i}
d={d}
style={{ "--i": i } as CSSProperties}
pathLength={1}
/>
))}
</g>
<path className="gu-trail__ghost" d={path} />
<path
ref={route}
className="gu-trail__route"
d={path}
style={
{
strokeDasharray: len,
strokeDashoffset: len * (1 - p),
} as CSSProperties
}
/>
{stops.map((s, i) => {
const pt = pins[i] ?? { x: -99, y: -99 };
const passed = p >= s.at;
return (
<g
key={s.name}
className="gu-trail__stop"
data-passed={passed}
transform={`translate(${pt.x} ${pt.y})`}
>
<circle r="6" />
<g className="gu-trail__flag">
<rect
x="-4"
y="-38"
rx="8"
width={s.name.length * 7.2 + 18}
height="24"
/>
<text x="5" y="-21">
{s.name}
</text>
</g>
</g>
);
})}
<g
className="gu-trail__walker"
transform={`translate(${walker.x} ${walker.y})`}
data-on={p > 0 && p < 1}
>
<circle className="gu-trail__pulse" r="14" />
<circle r="7" />
</g>
</svg>
<svg
className="gu-trail__profile"
viewBox="0 0 600 100"
preserveAspectRatio="none"
aria-hidden="true"
>
<defs>
<clipPath id="gu-trail-done">
<rect width={600 * p} height="100" />
</clipPath>
</defs>
<path className="gu-trail__prof-bg" d={`M0,100 L${prof} L600,100Z`} />
<path
className="gu-trail__prof-fill"
d={`M0,100 L${prof} L600,100Z`}
clipPath="url(#gu-trail-done)"
/>
<line
x1={600 * p}
x2={600 * p}
y1="0"
y2="100"
className="gu-trail__cursor"
/>
</svg>
<div className="gu-trail__controls">
<button
type="button"
onClick={() => {
if (p >= 1) {
pos.current = 0;
setP(0);
}
setPlaying((v) => !v || p >= 1);
}}
aria-label={
playing
? "Pause the walk"
: p >= 1
? "Walk it again"
: "Play the walk"
}
>
<svg viewBox="0 0 24 24" aria-hidden="true">
{playing ? (
<path d="M8 5.5v13M16 5.5v13" />
) : p >= 1 ? (
<path d="M5 12a7 7 0 1 0 2.1-5M5 4.5V8h3.5" />
) : (
<path d="M8 5.5v13l10-6.5Z" />
)}
</svg>
</button>
<input
type="range"
min={0}
max={1000}
value={Math.round(p * 1000)}
aria-label="Position along the route"
aria-valuetext={`${(km * p).toFixed(1)} of ${km} km`}
onChange={(e) => {
setPlaying(false);
setP(Number(e.target.value) / 1000);
}}
style={{ "--p": p } as CSSProperties}
/>
</div>
</div>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Trail that draws itself
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A card with a hiking route that draws itself across a topographic map. A walker dot travels the line, the distance, current height and total climb count up, a height profile under the map fills in step, and named stops pop up flags as the walker passes them. A play button and a slider let people replay it or scrub back and forth.
## Layout and look
- Card max 720px, radius 28px, padding 18–28px. Dark: #0e0e10 with a faint orange glow top-right, cream #f3eee3 text; light: the reverse. Accent #ff5a1f.
- Header: route name (22–30px, weight 800) on the left; three stats (12px label at 60% opacity, 18px bold tabular number) on the right.
- Map: an SVG 600 × 340. Thin contour lines (1.2px at 14% opacity) that draw in one after another on load. A dotted ghost of the full route (2 8 dashes, 18% opacity). The drawn route is 4.5px orange with a soft orange glow.
- Stops: small rings that fill orange when passed, with a pill flag (ink background, 12px bold label) that springs up from below.
- Walker: white dot with an orange ring and an orange pulse ring while moving.
- Height profile: a 70px area chart, grey for the whole route and orange for the walked part, with a thin orange cursor line.
- Controls: a 48px round orange play / pause / replay button and a range slider with an orange filled track.
## Behavior and motion
1. When 40% of the card scrolls into view, the walk starts: progress goes from 0 to 1 over 5.2s, driven by requestAnimationFrame.
2. The route uses stroke-dasharray = path length and stroke-dashoffset = length × (1 − progress). The walker position is getPointAtLength(length × progress).
3. Height is interpolated from the heights array; climb adds only the uphill parts walked so far.
4. Dragging the slider pauses the walk and sets progress directly.
## Accessibility
- The map is role="img" with a label (name and length). The slider is a real range input with aria-valuetext "6.1 of 14.2 km". The button's label says what it does (Play, Pause, Walk it again). Visible focus rings.
- prefers-reduced-motion: the route appears complete with every flag shown, no contour drawing or pulse; the slider still works.
## Props
title, path (SVG path in a 600 × 340 box), km, heights (metres at even steps), stops ({ at: 0–1, name }), theme.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### TrailDraw.tsx
```tsx
"use client";
import { useEffect, useRef, useState, type CSSProperties } from "react";
import "./trail-draw.css";
export type Stop = { at: number; name: string };
type TrailDrawProps = {
title: string;
/** SVG path of the route in a 600 x 340 box. */
path: string;
km: number;
/** Height in metres at evenly spaced points along the route. */
heights: number[];
stops: Stop[];
theme?: "light" | "dark";
};
const TOPO = [
"M60 250c40-60 120-90 190-70s120 70 200 40 120-90 110-130",
"M40 290c60-50 150-60 220-40s130 50 210 20 100-60 100-90",
"M200 130c30-40 90-55 130-30s40 80-10 100-110 10-120-70Z",
"M230 135c20-22 60-28 82-12s22 46-8 58-70 4-74-46Z",
"M258 140c10-10 30-12 40-4s10 22-4 28-34 2-36-24Z",
"M420 90c25-30 80-35 105-10s10 70-35 75-80-35-70-65Z",
"M445 95c14-16 46-18 60-4s4 40-20 42-46-20-40-38Z",
"M20 120c50 10 90-20 120-50",
"M60 330c80-40 170-30 240-10s150 10 240-30",
];
/**
* TrailDraw: a hiking route that draws itself across a topographic map.
* - The trail line is drawn with a moving walker; distance and height count up as it goes.
* - The height profile underneath fills in step with the map, and stops pop up as you pass them.
* - Scrub with the slider (or arrow keys) to walk back and forth, or press play to walk it again.
*/
export function TrailDraw({
title,
path,
km,
heights,
stops,
theme = "dark",
}: TrailDrawProps) {
const [p, setP] = useState(0);
const [playing, setPlaying] = useState(false);
const route = useRef<SVGPathElement>(null);
const root = useRef<HTMLDivElement>(null);
const [len, setLen] = useState(1000);
const [walker, setWalker] = useState({ x: 0, y: 0 });
const [pins, setPins] = useState<{ x: number; y: number }[]>([]);
const pos = useRef(0);
// Start walking the first time the map scrolls into view.
useEffect(() => {
const el = root.current;
if (!el) return;
const reduce = window.matchMedia(
"(prefers-reduced-motion: reduce)",
).matches;
const io = new IntersectionObserver(
([e]) => {
if (!e.isIntersecting) return;
io.disconnect();
if (reduce) setP(1);
else setPlaying(true);
},
{ threshold: 0.4 },
);
io.observe(el);
return () => io.disconnect();
}, []);
useEffect(() => {
if (!playing) return;
let raf = 0;
let last = performance.now();
const tick = (now: number) => {
pos.current = Math.min(1, pos.current + (now - last) / 5200);
last = now;
setP(pos.current);
if (pos.current >= 1) setPlaying(false);
else raf = requestAnimationFrame(tick);
};
raf = requestAnimationFrame(tick);
return () => cancelAnimationFrame(raf);
}, [playing]);
useEffect(() => {
pos.current = p;
}, [p]);
// Measuring the SVG path needs the DOM, so these run after render.
useEffect(() => {
const r = route.current;
if (!r) return;
const L = r.getTotalLength();
setLen(L);
setPins(stops.map((s) => r.getPointAtLength(L * s.at)));
}, [path, stops]);
useEffect(() => {
const r = route.current;
if (!r) return;
const L = r.getTotalLength();
const pt = r.getPointAtLength(L * p);
setWalker({ x: pt.x, y: pt.y });
}, [p]);
const idx = p * (heights.length - 1);
const lo = Math.floor(idx);
const hi = Math.min(heights.length - 1, lo + 1);
const h = Math.round(heights[lo] + (heights[hi] - heights[lo]) * (idx - lo));
const top = Math.max(...heights);
const bottom = Math.min(...heights);
const climb = heights
.slice(0, hi + 1)
.reduce(
(s, v, i, a) =>
i && v > a[i - 1] ? s + (v - a[i - 1]) * (i <= lo ? 1 : idx - lo) : s,
0,
);
const prof = heights
.map(
(v, i) =>
`${(i / (heights.length - 1)) * 600},${90 - ((v - bottom) / (top - bottom || 1)) * 74}`,
)
.join(" L");
return (
<div className="gu-trail" data-theme={theme} ref={root}>
<header className="gu-trail__head">
<h3>{title}</h3>
<dl>
<div>
<dt>Distance</dt>
<dd>{(km * p).toFixed(1)} km</dd>
</div>
<div>
<dt>Height</dt>
<dd>{h} m</dd>
</div>
<div>
<dt>Climbed</dt>
<dd>{Math.round(climb)} m</dd>
</div>
</dl>
</header>
<svg
className="gu-trail__map"
viewBox="0 0 600 340"
role="img"
aria-label={`Map of ${title}, ${km} km`}
>
<g className="gu-trail__topo">
{TOPO.map((d, i) => (
<path
key={i}
d={d}
style={{ "--i": i } as CSSProperties}
pathLength={1}
/>
))}
</g>
<path className="gu-trail__ghost" d={path} />
<path
ref={route}
className="gu-trail__route"
d={path}
style={
{
strokeDasharray: len,
strokeDashoffset: len * (1 - p),
} as CSSProperties
}
/>
{stops.map((s, i) => {
const pt = pins[i] ?? { x: -99, y: -99 };
const passed = p >= s.at;
return (
<g
key={s.name}
className="gu-trail__stop"
data-passed={passed}
transform={`translate(${pt.x} ${pt.y})`}
>
<circle r="6" />
<g className="gu-trail__flag">
<rect
x="-4"
y="-38"
rx="8"
width={s.name.length * 7.2 + 18}
height="24"
/>
<text x="5" y="-21">
{s.name}
</text>
</g>
</g>
);
})}
<g
className="gu-trail__walker"
transform={`translate(${walker.x} ${walker.y})`}
data-on={p > 0 && p < 1}
>
<circle className="gu-trail__pulse" r="14" />
<circle r="7" />
</g>
</svg>
<svg
className="gu-trail__profile"
viewBox="0 0 600 100"
preserveAspectRatio="none"
aria-hidden="true"
>
<defs>
<clipPath id="gu-trail-done">
<rect width={600 * p} height="100" />
</clipPath>
</defs>
<path className="gu-trail__prof-bg" d={`M0,100 L${prof} L600,100Z`} />
<path
className="gu-trail__prof-fill"
d={`M0,100 L${prof} L600,100Z`}
clipPath="url(#gu-trail-done)"
/>
<line
x1={600 * p}
x2={600 * p}
y1="0"
y2="100"
className="gu-trail__cursor"
/>
</svg>
<div className="gu-trail__controls">
<button
type="button"
onClick={() => {
if (p >= 1) {
pos.current = 0;
setP(0);
}
setPlaying((v) => !v || p >= 1);
}}
aria-label={
playing
? "Pause the walk"
: p >= 1
? "Walk it again"
: "Play the walk"
}
>
<svg viewBox="0 0 24 24" aria-hidden="true">
{playing ? (
<path d="M8 5.5v13M16 5.5v13" />
) : p >= 1 ? (
<path d="M5 12a7 7 0 1 0 2.1-5M5 4.5V8h3.5" />
) : (
<path d="M8 5.5v13l10-6.5Z" />
)}
</svg>
</button>
<input
type="range"
min={0}
max={1000}
value={Math.round(p * 1000)}
aria-label="Position along the route"
aria-valuetext={`${(km * p).toFixed(1)} of ${km} km`}
onChange={(e) => {
setPlaying(false);
setP(Number(e.target.value) / 1000);
}}
style={{ "--p": p } as CSSProperties}
/>
</div>
</div>
);
}
```
### trail-draw.css
```css
.gu-trail {
--ink: #f3eee3;
--bg: #0e0e10;
--line: rgb(243 238 227 / 0.14);
--accent: #ff5a1f;
--spring: cubic-bezier(0.34, 1.56, 0.64, 1);
width: min(100%, 720px);
padding: clamp(18px, 3vw, 28px);
border-radius: 28px;
background:
radial-gradient(90% 60% at 80% 0%, rgb(255 90 31 / 0.1), transparent 70%),
var(--bg);
color: var(--ink);
font-family: ui-sans-serif, system-ui, sans-serif;
}
.gu-trail[data-theme="light"] {
--ink: #0e0e10;
--bg: #f3eee3;
--line: rgb(14 14 16 / 0.14);
}
.gu-trail__head {
display: flex;
flex-wrap: wrap;
align-items: end;
justify-content: space-between;
gap: 12px 24px;
}
.gu-trail__head h3 {
margin: 0;
font-size: clamp(22px, 3.4vw, 30px);
font-weight: 800;
letter-spacing: -0.03em;
line-height: 1.05;
}
.gu-trail__head dl {
display: flex;
gap: 22px;
margin: 0;
}
.gu-trail__head dt {
font-size: 12px;
opacity: 0.6;
}
.gu-trail__head dd {
margin: 2px 0 0;
font-size: 18px;
font-weight: 800;
font-variant-numeric: tabular-nums;
letter-spacing: -0.02em;
}
.gu-trail__map {
display: block;
width: 100%;
height: auto;
margin-top: 12px;
overflow: visible;
}
.gu-trail__topo path {
fill: none;
stroke: var(--line);
stroke-width: 1.2;
stroke-dasharray: 1;
stroke-dashoffset: 1;
animation: gu-trail-topo 2.4s calc(var(--i) * 0.12s)
cubic-bezier(0.16, 1, 0.3, 1) forwards;
}
@keyframes gu-trail-topo {
to {
stroke-dashoffset: 0;
}
}
.gu-trail__ghost {
fill: none;
stroke: var(--ink);
stroke-opacity: 0.18;
stroke-width: 3;
stroke-dasharray: 2 8;
stroke-linecap: round;
}
.gu-trail__route {
fill: none;
stroke: var(--accent);
stroke-width: 4.5;
stroke-linecap: round;
stroke-linejoin: round;
filter: drop-shadow(0 0 6px rgb(255 90 31 / 0.55));
}
.gu-trail__stop circle {
fill: var(--bg);
stroke: var(--ink);
stroke-opacity: 0.4;
stroke-width: 2.5;
transition:
fill 0.3s,
stroke 0.3s,
r 0.5s var(--spring);
}
.gu-trail__stop[data-passed="true"] circle {
fill: var(--accent);
stroke: var(--accent);
stroke-opacity: 1;
r: 7;
}
.gu-trail__flag {
opacity: 0;
translate: 0 8px;
scale: 0.8;
transform-box: fill-box;
transform-origin: 0 100%;
transition:
opacity 0.3s,
translate 0.5s var(--spring),
scale 0.5s var(--spring);
}
.gu-trail__stop[data-passed="true"] .gu-trail__flag {
opacity: 1;
translate: 0 0;
scale: 1;
}
.gu-trail__flag rect {
fill: var(--ink);
}
.gu-trail__flag text {
fill: var(--bg);
font-size: 12px;
font-weight: 700;
}
.gu-trail__walker circle:last-child {
fill: #fff;
stroke: var(--accent);
stroke-width: 3;
}
.gu-trail__pulse {
fill: var(--accent);
opacity: 0;
transform-box: fill-box;
transform-origin: center;
}
.gu-trail__walker[data-on="true"] .gu-trail__pulse {
animation: gu-trail-pulse 1.2s ease-out infinite;
}
@keyframes gu-trail-pulse {
from {
opacity: 0.5;
scale: 0.4;
}
to {
opacity: 0;
scale: 1.6;
}
}
.gu-trail__profile {
display: block;
width: 100%;
height: 70px;
margin-top: 6px;
}
.gu-trail__prof-bg {
fill: var(--line);
}
.gu-trail__prof-fill {
fill: rgb(255 90 31 / 0.55);
}
.gu-trail__cursor {
stroke: var(--accent);
stroke-width: 2;
vector-effect: non-scaling-stroke;
}
.gu-trail__controls {
display: flex;
align-items: center;
gap: 14px;
margin-top: 14px;
}
.gu-trail__controls button {
flex: none;
display: grid;
place-items: center;
width: 48px;
height: 48px;
border: 0;
border-radius: 50%;
background: var(--accent);
color: #0e0e10;
cursor: pointer;
transition: scale 0.4s var(--spring);
}
.gu-trail__controls button:hover {
scale: 1.08;
}
.gu-trail__controls button:active {
scale: 0.92;
}
.gu-trail__controls button:focus-visible {
outline: 3px solid var(--ink);
outline-offset: 3px;
}
.gu-trail__controls svg {
width: 20px;
height: 20px;
fill: currentColor;
stroke: currentColor;
stroke-width: 2.2;
stroke-linecap: round;
stroke-linejoin: round;
}
.gu-trail__controls input {
--p: 0;
flex: 1;
height: 28px;
margin: 0;
background: transparent;
appearance: none;
cursor: pointer;
}
.gu-trail__controls input::-webkit-slider-runnable-track {
height: 6px;
border-radius: 6px;
background: linear-gradient(
90deg,
var(--accent) calc(var(--p) * 100%),
var(--line) 0
);
}
.gu-trail__controls input::-moz-range-track {
height: 6px;
border-radius: 6px;
background: linear-gradient(
90deg,
var(--accent) calc(var(--p) * 100%),
var(--line) 0
);
}
.gu-trail__controls input::-webkit-slider-thumb {
appearance: none;
width: 22px;
height: 22px;
margin-top: -8px;
border-radius: 50%;
background: #fff;
border: 3px solid var(--accent);
}
.gu-trail__controls input::-moz-range-thumb {
width: 16px;
height: 16px;
border-radius: 50%;
background: #fff;
border: 3px solid var(--accent);
}
.gu-trail__controls input:focus-visible {
outline: 3px solid var(--accent);
outline-offset: 4px;
border-radius: 8px;
}
@media (prefers-reduced-motion: reduce) {
.gu-trail__topo path {
animation: none;
stroke-dashoffset: 0;
}
.gu-trail__walker .gu-trail__pulse {
animation: none !important;
}
.gu-trail__flag,
.gu-trail__stop circle {
transition: none;
}
}
```
You might also want
Next piece: Weather app with a living skyMarker highlight
Animations, beginner
Odometer counter
Animations, beginner
Reading progress bar
Animations, beginner