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:
- 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\)).
- Angle Derivation: It calculates the rotation angle (\(\theta\)) using the standard arctangent function: \[\theta = \operatorname{atan2}(dy, dx)\]
- 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:
- Start Markers (
marker-start): The angle is determined exclusively by the outgoing tangent vector at the start of the first segment (\(t = 0\)). - End Markers (
marker-end): The angle is determined exclusively by the incoming tangent vector at the end of the final segment (\(t = 1\)). - Mid Markers (
marker-mid): For intermediate vertices where two path commands meet, the browser calculates both the incoming tangent vector from the preceding segment and the outgoing tangent vector to the subsequent segment. The browser then computes the angle bisector between these two vectors to produce a balanced alignment between the incoming and outgoing curves.
Values of the
orient Attribute
The orient attribute accepts several values that govern
rotation behavior:
auto: Dynamically aligns the marker’s positive x-axis with the path’s forward tangent vector at the marker’s point.auto-start-reverse: Acts identically toautofor mid and end markers. For start markers, it rotates the marker by an additional 180 degrees. This allows a single marker definition (such as an arrowhead) to be reused at both ends of a bidirectional line or curve.<angle>(e.g.,90deg,0.5rad,45): Disables automatic path tracking and forces the marker to maintain a static, fixed rotation relative to the SVG canvas, regardless of the underlying path’s curvature.
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.