← Back to Index

SVG DOM Methods & Interfaces

All SVG-specific JavaScript interfaces, methods, and properties. Standard DOM methods (querySelector, addEventListener, etc.) work on SVG elements too but aren't listed here.

Interfaces

SVGElement (base interface)

All SVG elements inherit from this. Extends Element (so querySelector, classList, dataset, etc. all work).

Properties

PropertyTypeDescription
.ownerSVGElementSVGSVGElementThe nearest ancestor <svg> element. null on the outermost <svg> itself.
.viewportElementSVGElementThe element that established the current viewport (usually the nearest <svg>).
.datasetDOMStringMapAccess to data-* attributes (same as HTML elements).
.styleCSSStyleDeclarationInline styles. Works for CSS-compatible SVG properties (fill, opacity, transform, etc.).
.classNameSVGAnimatedStringNote: returns an SVGAnimatedString, not a plain string. Use .classList instead for manipulation.

SVGSVGElement (<svg> root)

The <svg> element's interface. Has the richest set of SVG-specific methods - coordinate transforms, viewBox control, animation timing.

Coordinate Methods

MethodReturnsDescription
.createSVGPoint()SVGPointCreates a blank point object for coordinate transforms.
.createSVGMatrix()SVGMatrixCreates a blank identity matrix.
.createSVGRect()SVGRectCreates a blank rect object.
.createSVGTransform()SVGTransformCreates a blank transform object.
.createSVGNumber()SVGNumberCreates a typed number wrapper.
.createSVGLength()SVGLengthCreates a typed length value.
.createSVGAngle()SVGAngleCreates a typed angle value.

ViewBox & Viewport

PropertyTypeDescription
.viewBoxSVGAnimatedRectAccess to the viewBox attribute. .viewBox.baseVal gives {x, y, width, height}.
.currentScaleNumberCurrent zoom level (for outermost SVG only). Settable.
.currentTranslateSVGPointCurrent pan offset (for outermost SVG only). Mutable.

Animation Timing (SMIL control)

MethodReturnsDescription
.getCurrentTime()Number (seconds)Current document time for SMIL animations.
.setCurrentTime(seconds)voidSeek SMIL animations to a specific time.
.pauseAnimations()voidPause all SMIL animations in this SVG.
.unpauseAnimations()voidResume paused SMIL animations.
.animationsPaused()BooleanWhether SMIL animations are currently paused.

Element Lookup

MethodReturnsDescription
.getElementById(id)ElementFinds element by ID within this SVG fragment (useful for embedded SVG).

SVGGraphicsElement (shapes, groups, text, <use>)

All renderable SVG elements inherit from this. Provides bounding box and coordinate transform methods.

Methods

MethodReturnsDescription
.getBBox()SVGRect {x, y, width, height}Bounding box in local coordinate space (before any transforms on this element). Does not include stroke width.
.getCTM()SVGMatrixCurrent Transformation Matrix from this element's coordinate space to the nearest viewport's coordinate space.
.getScreenCTM()SVGMatrixMatrix from this element's coordinate space to screen pixels. Accounts for all ancestor transforms, viewBox, scroll, CSS transforms, zoom.

Properties

PropertyTypeDescription
.transformSVGAnimatedTransformListAccess to the element's transform attribute. .transform.baseVal is the list of transform functions.

Common usage pattern

// Convert client coords → SVG coords const pt = svg.createSVGPoint(); pt.x = event.clientX; pt.y = event.clientY; const svgPt = pt.matrixTransform(svg.getScreenCTM().inverse());

SVGGeometryElement (all shape elements)

Inherited by <circle>, <rect>, <ellipse>, <line>, <polyline>, <polygon>, and <path>.

Methods

MethodReturnsDescription
.getTotalLength()NumberTotal arc length of the shape's outline (computed via numerical integration for curves). Works on all shapes, not just paths.
.getPointAtLength(distance)SVGPoint {x, y}Coordinates at the given distance along the outline. distance is in the same units as getTotalLength().
.isPointInFill(point)BooleanWhether the given SVGPoint is inside the element's fill area (respects fill-rule).
.isPointInStroke(point)BooleanWhether the given SVGPoint is on the element's stroke (respects stroke-width).

Properties

PropertyTypeDescription
.pathLengthSVGAnimatedNumberThe pathLength attribute value. Affects how stroke-dasharray/stroke-dashoffset are interpreted.

Usage notes

getTotalLength() and getPointAtLength() work on all geometry elements in modern browsers - not just <path>. You can call them on <circle>, <rect>, etc. to find positions along their outlines.

isPointInFill() is useful for custom hit testing - for example, checking whether a click landed inside a complex path shape without relying on browser event targeting.

SVGPathElement (<path>)

Extends SVGGeometryElement. In SVG 1.1, <path> had additional methods (now deprecated or removed in SVG 2):

MethodStatusDescription
.getTotalLength()CurrentInherited from SVGGeometryElement.
.getPointAtLength(d)CurrentInherited from SVGGeometryElement.
.getPathSegAtLength(d)Removed (SVG 2)Was: returns which path segment contains the given distance. No longer available.
.pathSegListRemoved (SVG 2)Was: list of path segments. Replaced by parsing the d attribute string directly.

In practice, you interact with <path> via getAttribute('d') / setAttribute('d', ...) for path data, plus the inherited geometry methods.

SVGTextContentElement (text elements)

Inherited by <text>, <tspan>, <textPath>. Provides character/glyph-level access.

Methods

MethodReturnsDescription
.getNumberOfChars()NumberNumber of rendered characters (after bidi reordering, ligatures, etc.).
.getComputedTextLength()NumberTotal advance width of the rendered text (in user units).
.getSubStringLength(startIdx, nChars)NumberAdvance width of a character range.
.getStartPositionOfChar(idx)SVGPointStarting position of character at index.
.getEndPositionOfChar(idx)SVGPointEnd position of character at index.
.getExtentOfChar(idx)SVGRectBounding box of character at index.
.getRotationOfChar(idx)Number (degrees)Rotation angle of character at index (relevant for text on a path).
.getCharNumAtPosition(point)NumberIndex of the character at the given SVGPoint, or -1.
.selectSubString(startIdx, nChars)voidSelects a range of characters (creates a text selection).

SVGAnimationElement (SMIL animation elements)

Inherited by <animate>, <animateTransform>, <animateMotion>, <set>.

Methods

MethodReturnsDescription
.getStartTime()Number (seconds)Resolved start time of this animation element.
.getCurrentTime()Number (seconds)Current time within this animation's active duration.
.getSimpleDuration()Number (seconds)Simple (one iteration) duration. Throws if indefinite.
.beginElement()voidProgrammatically start this animation (as if begin condition was met).
.endElement()voidProgrammatically end this animation.
.beginElementAt(offset)voidStart after offset seconds from now.
.endElementAt(offset)voidEnd after offset seconds from now.

Properties

PropertyTypeDescription
.targetElementSVGElementThe element being animated (the parent element).

Usage note

beginElement() is the way to trigger SMIL animations from JavaScript - set begin="indefinite" on the animation element, then call .beginElement() when you want it to start. This bridges JS event handling with SMIL animation.

SVGPoint & SVGMatrix (helper objects)

SVGPoint (DOMPoint in modern spec)

Property/MethodDescription
.xX coordinate (mutable).
.yY coordinate (mutable).
.matrixTransform(matrix)Returns a new SVGPoint transformed by the given SVGMatrix. This is how you convert between coordinate spaces.

SVGMatrix (DOMMatrix in modern spec)

A 2D affine transformation matrix (3×2 values: a, b, c, d, e, f). Each method returns a new matrix (immutable pattern).

MethodReturnsDescription
.multiply(other)SVGMatrixMatrix multiplication (compose transforms).
.inverse()SVGMatrixThe inverse matrix. Used to convert from screen to SVG coordinates.
.translate(x, y)SVGMatrixPost-multiply by a translation.
.scale(factor)SVGMatrixPost-multiply by a uniform scale.
.scaleNonUniform(sx, sy)SVGMatrixNon-uniform scale.
.rotate(angle)SVGMatrixPost-multiply by a rotation (degrees).
.rotateFromVector(x, y)SVGMatrixRotate to align with the vector (x, y).
.flipX()SVGMatrixFlip horizontally.
.flipY()SVGMatrixFlip vertically.
.skewX(angle)SVGMatrixHorizontal skew (degrees).
.skewY(angle)SVGMatrixVertical skew (degrees).

Properties (matrix components)

PropertyDescription
.aScale X component
.bSkew Y component
.cSkew X component
.dScale Y component
.eTranslate X
.fTranslate Y

Modern note: Browsers are transitioning from SVGPoint/SVGMatrix to DOMPoint/DOMMatrix. The SVG-specific versions still work but may be deprecated in future specs. DOMMatrix also supports 3D transforms.

SVGLength, SVGAngle, SVGNumber (typed values)

SVG attributes that represent lengths, angles, or numbers have typed DOM interfaces. These are accessed via properties like circle.r, rect.width, etc.

SVGAnimatedLength (e.g. circle.r)

PropertyTypeDescription
.baseValSVGLengthThe base (non-animated) value.
.animValSVGLength (read-only)The current animated value (reflects SMIL animations).

SVGLength (the actual value)

Property/MethodDescription
.valueThe value in user units (pixels). Settable.
.valueInSpecifiedUnitsThe value in its declared unit (e.g. if "50%", this is 50).
.unitTypeNumeric constant for the unit: SVG_LENGTHTYPE_PX, _PERCENTAGE, _EM, etc.
.convertToSpecifiedUnits(unitType)Convert the value to a different unit in place.
.newValueSpecifiedUnits(unitType, value)Set a new value with a specific unit.

Practical example

// Read circle radius as a number (no string parsing needed) const radius = circle.r.baseVal.value; // e.g. 40 // Set it directly circle.r.baseVal.value = 60; // Check what it currently looks like during animation const animatedR = circle.r.animVal.value;

Element Creation & Namespace

The essential pattern for creating SVG elements from JavaScript.

MethodContextDescription
document.createElementNS(ns, tag)DocumentCreate an element in a specific namespace. Required for SVG elements.
element.setAttributeNS(ns, name, value)ElementSet a namespaced attribute. Only needed for xlink:href (legacy). Modern SVG uses plain href.
element.setAttribute(name, value)ElementSet a non-namespaced attribute. Works for all common SVG attributes.

Namespace URIs

NamespaceURIWhen needed
SVGhttp://www.w3.org/2000/svgEvery createElementNS call for SVG elements.
XLink (legacy)http://www.w3.org/1999/xlinkOnly for xlink:href on older SVG. Use plain href instead.
XMLhttp://www.w3.org/XML/1998/namespaceRarely needed. For xml:lang, xml:space.

Helper pattern

const SVG_NS = 'http://www.w3.org/2000/svg'; function svgEl(tag, attrs = {}, parent = null) {   const el = document.createElementNS(SVG_NS, tag);   for (const [k, v] of Object.entries(attrs)) el.setAttribute(k, v);   if (parent) parent.appendChild(el);   return el; }

Notes

← Back to Index