SVGTextPathElement
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The SVGTextPathElement interface corresponds to the <textPath> element.
Instance properties
This interface also inherits properties from its parent interface, SVGTextContentElement.
SVGTextPathElement.hrefRead only-
An
SVGAnimatedStringcorresponding to thehreforxlink:hrefattribute of the given element. SVGTextPathElement.sideRead only-
An
SVGAnimatedEnumerationcorresponding to thesideattribute of the given element. Allowed values are specified by theTEXTPATH_SIDETYPE_*constants defined on this interface. SVGTextPathElement.startOffsetRead only-
An
SVGAnimatedLengthcorresponding to the X component of thestartOffsetattribute of the given element. SVGTextPathElement.methodRead only-
An
SVGAnimatedEnumerationcorresponding to themethodattribute of the given element. Allowed values are specified by theTEXTPATH_METHODTYPE_*constants defined on this interface. SVGTextPathElement.spacingRead only-
An
SVGAnimatedEnumerationcorresponding to thespacingattribute of the given element. Allowed values are specified by theTEXTPATH_SPACINGTYPE_*constants defined on this interface.
Instance methods
This interface does not provide any specific methods, but implements those of its parent, SVGTextContentElement.
Static properties
TEXTPATH_METHODTYPE_UNKNOWN(0)-
The type is not one of predefined types. It is invalid to attempt to define a new value of this type or to attempt to switch an existing value to this type.
TEXTPATH_METHODTYPE_ALIGN(1)-
Corresponds to the value
align. TEXTPATH_METHODTYPE_STRETCH(2)-
Corresponds to the value
stretch. TEXTPATH_SPACINGTYPE_UNKNOWN(0)-
The type is not one of predefined types. It is invalid to attempt to define a new value of this type or to attempt to switch an existing value to this type.
TEXTPATH_SPACINGTYPE_AUTO(1)-
Corresponds to the value
auto. TEXTPATH_SPACINGTYPE_EXACT(2)-
Corresponds to the value
exact. TEXTPATH_SIDETYPE_UNKNOWN(0)-
The type is not one of the predefined types. It is invalid to attempt to define a new value of this type or to attempt to switch an existing value to this type.
TEXTPATH_SIDETYPE_LEFT(1)-
Corresponds to the value
left. TEXTPATH_SIDETYPE_RIGHT(2)-
Corresponds to the value
right.
Examples
>Basic usage
This example shows how you can set and get the properties of an SVGTextPathElement.
HTML
First we define the SVG and associated CSS to draw a path with text drawn along it (this is a near-copy of the SVG in the path attribute topic).
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<!-- to hide the path, it is usually wrapped in a <defs> element -->
<!-- <defs> -->
<path
id="MyPath"
fill="none"
stroke="red"
d="M10,90 Q90,90 90,45 Q90,10 50,10 Q10,10 10,40 Q10,70 45,70 Q70,70 75,50" />
<text>
<textPath href="#MyPath">Quick brown fox jumps over the lazy dog.</textPath>
</text>
</svg>
We also add a button for toggling the side property in order to change what side of the path the text is drawn on.
Note that there is also hidden logging code that is not relevant to the example.
<button id="toggleBtn">Toggle side</button>
JavaScript
The code below toggles the side.baseVal property on the textPath, causing the text to swap sides.
First we define a function to log each of the properties of the the path element, and call it to log the initial state on load.
The side.baseVale property is logged first, and demonstrates how the enumerated constants may be read and interpreted (this is done in a try...catch block, because side is not supported in all browsers).
The other properties of the text path are also logged, but as raw values of their associated baseVal property.
const textPath = document.querySelector("textPath");
function logPathBaseVal() {
// Log the baseVal for each property
log("LOG:");
try {
let side;
if (textPath.side.baseVal === SVGTextPathElement.TEXTPATH_SIDETYPE_RIGHT) {
side = "right";
} else if (
textPath.side.baseVal === SVGTextPathElement.TEXTPATH_SIDETYPE_LEFT
) {
side = "left";
} else if (
textPath.side.baseVal === SVGTextPathElement.TEXTPATH_SIDETYPE_UNKNOWN
) {
side = "unknown";
} else {
side = "unexpected value";
}
log(` Current side: ${side}`);
} catch {
log(`side property is not supported in this browser`);
}
log(` href: ${textPath.href.baseVal}`);
log(` method: ${textPath.method.baseVal}`);
log(` spacing: ${textPath.spacing.baseVal}`);
log(` startOffset: ${textPath.startOffset.baseVal}`);
}
// Log the initial state on load
logPathBaseVal();
The toggle button event handler code is shown below This reads the current value of the side.baseVal property, and toggles the value to match the other side.
It then logs the current state.
// Toggle the side when the button is clicked
toggleBtn.addEventListener("click", () => {
try {
if (textPath.side.baseVal === SVGTextPathElement.TEXTPATH_SIDETYPE_RIGHT) {
// Change to left
textPath.side.baseVal = SVGTextPathElement.TEXTPATH_SIDETYPE_LEFT;
} else {
// Change to right
textPath.side.baseVal = SVGTextPathElement.TEXTPATH_SIDETYPE_RIGHT;
}
// Log the updated state
logPathBaseVal();
} catch (e) {
log("Setting the side property is not supported in this browser.");
}
});
Result
Press the button to toggle the states.
Specifications
| Specification |
|---|
| Scalable Vector Graphics (SVG) 2> # InterfaceSVGTextPathElement> |