# PathMeasure: Position and Direction at Any Distance Along a Path

> PathMeasure turns a Path into something you can sample by distance, which is what powers draw-on animations, text-on-path, and dash effects.

**Date:** 2026-10-17T00:00:00.000Z
**Tags:** Canvas, Path, Math

## The intuition

A `Path` is built from `moveTo`/`quadraticTo`/`cubicTo` calls, so it's
tempting to assume "50% of the way along the path" means `t = 0.5` in
whichever curve call drew it. It doesn't.

* A curve's `t` is a blend weight fed into its interpolation math, not a
  pixel measurement. Wherever a control point pulls hardest, the curve
  covers more ground per step of `t`; wherever it pulls least, the same
  step of `t` covers almost nothing.
* `t = 0.5` lands wherever that interpolation says it lands. Nothing
  about the number 0.5 ties it to "half the pixels" of the curve.
* What you actually want, for a moving dot, a draw-on stroke reveal, or
  text that follows a curve, is distance: "give me the point 150 pixels
  along this path," regardless of how the curve's own `t` happens to be
  distributed.

[`PathMeasure`](https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/PathMeasure)'s
whole job is two steps: walk a `Path` once to find its real length, then
let you query position and direction at any distance into that length,
`t` never entering the picture.

* [Quadratic vs Cubic Bezier vs Arc: Picking the Right Path Curve](/quadratic-cubic-bezier-and-arc) — Why a Bezier curve's t parameter is a blend weight, not a pixel position, which is exactly the mismatch PathMeasure exists to paper over.

* [atan, atan2, and atanh: Three Different Inverses, One Confusing Name](/atan-atan2-atanh) — getTangent hands back a direction vector, not an angle. atan2 is the function that turns it into a rotation you can actually apply.

## The problem: t is a blend weight, not a distance

```kotlin
val path = Path().apply {
    moveTo(20f, 20f)
    quadraticTo(x1 = 260f, y1 = 20f, x2 = 260f, y2 = 80f)
}
```

The control point at `(260, 20)` sits far off to the side of the chord
between start and end, so it pulls hard on the curve's early half and
barely at all on its later half.

Drag the slider below and watch the gap between the two dots open up:
the raw-`t` dot shoots ahead early, while the arc-length dot keeps an
even pace and only catches up once the curve flattens out near the end:

*\[Interactive demo: PathMeasureSpeedDemo — view this post in a browser for the live version.]*

Nobody hand-rolls the interpolation behind that raw-`t` dot, `quadraticTo`
already runs it internally, so you never call it yourself. The mistake it
stands in for is one you *do* write: feeding a plain `0f..1f` progress,
from `animateFloat`, a drag gesture, a `SeekBar`, straight into anything
that walks the curve's own parameter space instead of real distance. A
hand-written "evenly spaced waypoints" sampler, a dash-phase calculation, a
rotation keyed off `progress` directly, all of them assume `progress = 0.5`
already means "half the pixels." It doesn't:

```kotlin
// the math behind the raw-t dot above, for reference, not something
// you'd write yourself: t fed straight into quadraticTo's own interpolation
fun quadraticAt(p0: Offset, p1: Offset, p2: Offset, t: Float): Offset {
    val mt = 1 - t
    return Offset(
        x = mt * mt * p0.x + 2 * mt * t * p1.x + t * t * p2.x,
        y = mt * mt * p0.y + 2 * mt * t * p1.y + t * t * p2.y,
    )
}
val raw = quadraticAt(Offset(20f, 20f), Offset(260f, 20f), Offset(260f, 80f), t = 0.5f)
// raw == Offset(200f, 35f)
```

`t = 0.5` lands at `(200, 35)`, which turns out to already be 69% of the
way along the curve's real 262px length, not 50%, because the curve covers
most of its distance early (where the control point pulls hardest) and
crawls through the rest.

```kotlin
val measure = PathMeasure()
measure.setPath(path, forceClosed = false)

val totalLength = measure.length // real pixel length of the curve
val point = measure.getPosition(distance = totalLength * progress)
val direction = measure.getTangent(distance = totalLength * progress)
```

`progress = 0.5` now means `(150, 26)`, the actual halfway point of the
curve's 262px, every time, regardless of where the control point pulls.
A dot driven by `getPosition` moves at a constant visual speed from one
end to the other, `t` never entering the picture.

## What getPosition and getTangent actually return

* `getPosition(distance): Offset` is the point on the path at that
  distance from the start. Compose quietly clamps `distance` into
  `0..length`, so a distance before the start or past the end doesn't
  throw, it just snaps to whichever end is closer.
* `getTangent(distance): Offset` is a direction vector at that same
  point, not an angle. It points the way the path is heading right there,
  which is exactly what you need to rotate something so it faces the way
  it's moving (a car icon cornering along a race-track path, a glyph
  tilting to follow curved text).

Turning that direction vector into a rotation `Float` for
`graphicsLayer { rotationZ = ... }` is one `atan2` call away:

```kotlin
val angleDegrees = Math.toDegrees(
    atan2(direction.y.toDouble(), direction.x.toDouble())
).toFloat()
```

> **The fundamental**
>
> This is **arc-length parameterization**: re-indexing a curve so its input
> is real distance traveled instead of whatever parameter happened to
> define the math (a curve's own `t`, an angle, a knot index). Equal steps
> in the new input always mean equal steps in pixels, which is exactly the
> property raw `t` doesn't have. `PathMeasure` builds this by walking the
> path once, accumulating length as it goes, so later distance queries are
> lookups against that walk rather than fresh geometry.

## Elsewhere: same fundamental, different framework

* **SVG**: `SVGGeometryElement.getPointAtLength(length)` and
  `getTotalLength()` on any `<path>` do exactly this, by name. "Length"
  in the method name is the same arc-length distance `PathMeasure.length`
  reports.
* **CSS motion path**: `offset-path` plus `offset-distance` moves an
  element along an SVG path by real distance, and the browser paces it
  for you, an entire `PathMeasure` built into two CSS properties with no
  JS at all.
* **WPF**: `PathGeometry.GetPointAtFractionLength(progress, out point,
  out tangent)` takes a 0-1 fraction instead of a raw distance, but it's
  the same query, point and tangent together, arc-length-based under the
  hood.
* **Flutter**: `Path.computeMetrics()` returns a sequence of
  `PathMetric` objects, each with a `.length` and a
  `.getTangentForOffset(distance)` that hands back position and angle
  together, the same two numbers `getPosition`/`getTangent` split into
  separate calls.
* **SwiftUI**: `Path.trimmedPath(from:to:)` (and the `Shape.trim(from:to:)`
  modifier built on it) trims by real arc length, not the path's own curve
  parameter. Trimming the same curve from this post at `to: 0.5` lands on
  its true arc-length midpoint, not wherever raw `t = 0.5` would land, the
  exact distinction this whole post is about.

## Edge cases

Compose's `PathMeasure` only measures the single
contour (One continuous, unbroken piece of a Path: everything drawn between one moveTo and the next. A Path with several moveTo calls holds several contours, even though it's a single Path object.)
it's given, and **there's no `nextContour()` to step to the next one**,
unlike the older
[`android.graphics.PathMeasure`](https://developer.android.com/reference/kotlin/android/graphics/PathMeasure)
it's modeled after.

Call `setPath` on a `Path` built from several disconnected pieces
(`addPath` of two separate shapes, say) and `length`/`getPosition` only
account for the first contour found, silently. If a path has multiple
disjoint pieces that all need to be walked, measure each one with its
own `PathMeasure` built from its own sub-`Path`, rather than handing the
combined path to one instance and expecting it to cover everything.

**`forceClosed` changes what "the end" means for measurement, not what's
drawn.** Passing `forceClosed = true` to `setPath` adds a synthetic
closing segment back to the start, even if the `Path` itself never
called `close()`, and that segment counts toward `length` and every
position query from then on. The stroke on screen never changes either
way, since `forceClosed` only affects what `PathMeasure` computes, not
what `drawPath` renders.

The path below is just two `lineTo` calls, so the gap between its last
point and its start is never actually drawn, toggling `forceClosed`
doesn't close it. What moves is the end dot, `getPosition(length)`: at
the last point the stroke really reaches with `forceClosed` off, snapped
back to the start with it on, and `length` itself jumps by exactly the
gap distance:

*\[Interactive demo: ForceClosedDemo — view this post in a browser for the live version.]*

## When to reach for PathMeasure

* **Draw-on / stroke-reveal animations**: a signature replaying its own
  strokes, a delivery map's route line drawing in behind a moving pin, a
  checklist's checkmark tracing itself in.
* **Constant-speed motion along a curved path**: a map app's blue dot
  gliding smoothly along your route instead of jumping waypoint to
  waypoint, a delivery tracker's icon following the road, an avatar
  hopping along a game's level-select path.
* **Text or icons that need to follow a curve's direction**, not just its
  position: a wordmark arced along a circular badge, a caption bending
  along a photo collage's edge, a label curving around a chart slice.

`getPosition`/`getTangent` answer "where's the dot right now, and which
way is it facing." `getSegment` answers "give me everything between these
two distances, as an actual drawable `Path`," which is what draw-on needs:
a real, shorter path to stroke, not a clip or a mask:

```kotlin
val measure = PathMeasure()
measure.setPath(path, forceClosed = false)

val trimmed = Path()
measure.getSegment(
    startDistance = 0f,
    stopDistance = measure.length * progress,
    destination = trimmed,
)
drawPath(trimmed, style = Stroke(width = 4f))
```

At `progress = 0.3`, `trimmed` holds just the first 30% of the path's
real length, stroke-reveal style, regardless of how the underlying
curve's `t` is distributed, which is the same mechanism behind trimming
a progress ring to a determinate percentage.

`destination` is mutated in place rather than returned, and `getSegment`
returns `false` (leaving it untouched) for a zero-length segment or when
`startDistance` isn't less than `stopDistance`. A fourth parameter,
`startWithMoveTo` (`true` by default), controls whether the written
segment opens with its own `moveTo`; set it `false` when appending
several segments into the same `destination` so they connect into one
subpath instead of starting a new one each time.

For constant-speed motion, that's the same `animateFloat` progress from
the demo earlier in this post, now driving `getPosition` instead of a
hand-rolled sampler, which is exactly what delivers the even pace a map
app's dot needs.

For text or icons following a curve's direction, rotating each glyph or
icon so it tilts with the curve is `getTangent` plus `atan2`, sampled
once per position along the path, the same math a curved wordmark or a
chart label needs to stay tangent to its arc.

## Mental model to actually use

**A `Path` is drawing instructions; a `PathMeasure` is that same path
re-indexed by real distance.** Reach for it any time "progress" needs to
mean pixels traveled, not wherever a curve's own `t` happens to land.


---

Canonical page: https://recomposed.blog/path-measure-position-along-path