How orient=“auto” Aligns SVG Markers to Path Curves

The orient attribute of the SVG <marker> element determines how a marker rotates when placed along a vector path. When set to auto or auto-start-reverse, the browser calculates the path’s directional tangent at the exact coordinate of the marker vertex and rotates the marker’s coordinate system to match that angle. This article explains the technical mechanics behind this calculation, how curves and vertices are handled, and how browsers apply coordinate transformations to keep markers aligned with path curvature.

The Tangent Calculation

When an SVG user agent (such as a web browser) renders an SVG path, it breaks the path data (d attribute) into mathematical segments, such as lines, cubic Bézier curves, or quadratic Bézier curves.

To determine the orientation of a marker at any given point along a curve:

  1. Derivative Evaluation: The browser calculates the derivative (the instantaneous rate of change, \(dx\) and \(dy\)) of the parametric curve equation at the marker’s position (\(t\)).
  2. Angle Derivation: It calculates the rotation angle (\(\theta\)) using the standard arctangent function: \[\theta = \operatorname{atan2}(dy, dx)\]
  3. Transformation Matrix: The marker’s coordinate system is translated to the point on the path and rotated by angle \(\theta\).

Handling Different Marker Positions

A path can place markers at three distinct locations via CSS or SVG attributes: marker-start, marker-mid, and marker-end. The orient calculation differs slightly depending on where the marker sits:

Values of the orient Attribute

The orient attribute accepts several values that govern rotation behavior:

Behavior on Smooth vs. Sharp Curves

On smooth curves (like continuous Bézier segments), the calculated tangent changes continuously, allowing intermediate markers or endpoints to point accurately along the curve’s flow.

If a path encounters a sharp corner (a non-differentiable cusp where incoming and outgoing vectors differ significantly), the bisector angle calculation ensures the marker divides the interior angle evenly rather than snapping arbitrarily to one of the adjoining segments. If a zero-length segment occurs, the browser falls back to the adjacent non-zero tangent to maintain continuity.