Find suitable segments of a path for its label text.
To add the dependency to your project, run
npm install --save label-segment
To use in your code, type
import labelSegment from 'label-segment';
Include the following script tag in your HTML:
<script src="http://unpkg.com/label-segment/dist/label-segment.js"></script>
Considering a given path, maximum angle and label length, this function returns a straight enough subset of the path in correct direction for an upright label.
Example:
var path = [[20, 33], [40, 31], [60, 30], [80, 31], [100, 33]];
var textLength = 50; // Text has a width of 50 pixels
var textPath = labelSegment(path, Math.PI / 8, 50);
// Now render text along textPath
Parameters
path
Array<Array<number>> Path represented by coordinate pairs.maxAngle
number Maximum angle in radians for a suitable segment.labelLength
number Required segment length for the label.
Returns (Array<Array<number>> | undefined) Path to draw the label along, in the
correct direction for an upright label. Returns undefined
when no suitable
subset of the given path
was found.