Gradient Along Stroke
This example shows a single thick SVG path rendered as a continuous gradient that transitions from green to red along the stroke, which can indicate directionality. Using the D3 v4 library, the code samples points uniformly along the path with `getPointAtLength`, then computes miter joints via line-line intersection for each segment. Each segment is filled and stroked with a color interpolated by `d3.interpolateRainbow` based on normalized path length, creating the gradient effect while avoiding antialiasing artifacts.
AI-generated descriptionThis example demonstrates how to create a gradient that follows a stroke. This technique is sometimes used to indicate directionality along a curved edge, such as with hierarchical edge bundling.
To start, take any SVG path element and uniformly sample points along the path using getPointAtLength. (This method can also be used for path tweening.) Then, for each segment between adjacent points, compute the miter joint via line-line intersection. Lastly fill each segment by interpolating the start and end colors, here green to red, using the normalized length t along the path. Although each segment is a constant color, there are many segments to give the appearance of a continuous gradient.
This example uses a thin stroke in addition to filling the segments. This avoids antialiasing artifacts due to most web browsers not implementing full-scene antialiasing.
forked from <a href='http://bl.ocks.org/mbostock/'>mbostock</a>'s block: <a href='http://bl.ocks.org/mbostock/4163057'>Gradient Along Stroke</a>