SVG DOM & Geometry Reference

SVG DOM & JavaScript API Glossary: 45+ Methods, Interfaces & Geometry Types

Isometric 3D concept of SVG DOM and JavaScript vector graphics API showing coordinate space matrices and bounding box math
Figure 1: SVG DOM coordinate mapping architecture — affine transformation matrices bridge viewport screen pixels to internal user coordinate space.

No SVG DOM terms matching your query.

1. Coordinate Systems, Matrices & Transformations

getScreenCTM() Coordinate Matrix
Interface: SVGGraphicsElement Returns: DOMMatrix | null

Returns the Current Transformation Matrix (CTM) mapping local SVG user coordinates directly to device screen pixels. Invaluable for mapping client pointer events (e.clientX, e.clientY) to exact vector coordinates.

const ctm = svgElement.getScreenCTM();
const pt = new DOMPoint(event.clientX, event.clientY);
const svgCoords = pt.matrixTransform(ctm.inverse());
console.log(`X: ${svgCoords.x}, Y: ${svgCoords.y}`);
getCTM() Coordinate Matrix
Interface: SVGGraphicsElement Returns: DOMMatrix | null

Returns the transformation matrix mapping local element coordinate space to its nearest ancestor viewport coordinate space (typically the root <svg>).

const localMatrix = childGroup.getCTM();
console.log(`Scale X: ${localMatrix.a}, Translate X: ${localMatrix.e}`);
DOMMatrix Interface
Spec: W3C Geometry Interfaces Level 1 Supersedes: SVGMatrix

Modern web standard representing 2D and 3D affine transformation matrices. Supports hardware-accelerated matrix multiplication, inversion, translation, and rotation.

const matrix = new DOMMatrix()
  .translate(100, 50)
  .rotate(45)
  .scale(1.5);
element.style.transform = matrix.toString();
DOMPoint Interface
Spec: W3C Geometry Interfaces Level 1 Supersedes: SVGPoint

Represents an immutable or mutable 2D/3D coordinate vector (x, y, z, w). Replaces legacy svg.createSVGPoint() in modern browsers.

const pt = new DOMPoint(24, 48);
const transformed = pt.matrixTransform(matrix);
createSVGPoint() Legacy Utility
Interface: SVGSVGElement Returns: SVGPoint

Creates a detached SVGPoint initialized at (0, 0). While supported in all browsers, new DOMPoint() is preferred in modern codebases.

const pt = svg.createSVGPoint();
pt.x = mouseX;
pt.y = mouseY;
matrixTransform() Matrix Operation
Interface: SVGPoint / DOMPoint Returns: DOMPoint

Multiplies the point coordinates by a specified 2D/3D matrix, returning a new transformed point object.

const localPoint = screenPoint.matrixTransform(svg.getScreenCTM().inverse());
SVGTransformList List Interface
Interface: SVGAnimatedTransformList Property: element.transform.baseVal

A sequence of SVGTransform items governing an element's transform attribute. Provides indexed item access and methods like appendItem() and consolidate().

const transformList = element.transform.baseVal;
const translateTransform = svg.createSVGTransform();
translateTransform.setTranslate(20, 40);
transformList.appendItem(translateTransform);
createSVGTransformFromMatrix() Factory Method
Interface: SVGSVGElement Returns: SVGTransform

Creates an SVGTransform object wrapping an existing affine matrix.

const transform = svg.createSVGTransformFromMatrix(customDOMMatrix);
element.transform.baseVal.initialize(transform);

2. Geometry, Bounding Boxes & Path Measurement

getBBox() Measurement
Interface: SVGGraphicsElement Returns: DOMRect

Computes the tight bounding box containing the element's rendered geometry in current user coordinate space. Ignores external CSS transforms and stroke width by default.

const bbox = pathElement.getBBox();
console.log(`x: ${bbox.x}, y: ${bbox.y}, width: ${bbox.width}, height: ${bbox.height}`);
getTotalLength() Measurement
Interface: SVGGeometryElement Returns: float (pixels)

Returns the total path length in user space units. Fundamental for CSS path drawing animations using stroke-dasharray and stroke-dashoffset.

const length = path.getTotalLength();
path.style.strokeDasharray = `${length}`;
path.style.strokeDashoffset = `${length}`;
getPointAtLength() Path Trajectory
Interface: SVGGeometryElement Returns: DOMPoint

Calculates the (x, y) coordinates along a path trajectory at a specified distance from the origin. Used for particle path follow animations and motion path positioning.

const midPoint = path.getPointAtLength(length * 0.5);
marker.setAttribute('cx', midPoint.x);
marker.setAttribute('cy', midPoint.y);
isPointInFill() Hit Testing
Interface: SVGGeometryElement Returns: boolean

Determines whether a DOMPoint or user-space coordinate is inside the element's filled shape area according to its fill-rule.

const isHit = shape.isPointInFill(new DOMPoint(120, 85));
isPointInStroke() Hit Testing
Interface: SVGGeometryElement Returns: boolean

Determines whether a point falls within the stroke width outline of the geometry element.

const onBorder = shape.isPointInStroke(new DOMPoint(120, 85));
SVGGeometryElement Base Interface
Subclasses: SVGPathElement, SVGRectElement, SVGCircleElement...

The abstract base interface for all SVG shape elements that have a geometric outline. Exposes getTotalLength(), getPointAtLength(), and hit-testing methods.

pathLength.baseVal Property
Interface: SVGGeometryElement Returns: float

Reflects the author-defined pathLength attribute, allowing manual calibration of stroke dash calculations.

path.pathLength.baseVal = 100; // Calibrate path to 0-100 scale

3. Document Structure & Root SVGSVGElement APIs

SVGSVGElement Root Interface
Represents: <svg> element

The DOM interface for root <svg> elements, providing viewport control, animation state, and matrix instantiation factories.

viewBox.baseVal Viewport
Interface: SVGSVGElement Returns: SVGRect

Provides direct read/write access to the root element's viewBox min-x, min-y, width, and height values.

const vb = svg.viewBox.baseVal;
vb.x -= 10; // Pan left
vb.width *= 1.1; // Zoom out
currentScale Interactive Zoom
Interface: SVGSVGElement Type: float

Gets or sets the current zoom factor applied to the root SVG canvas.

svg.currentScale = 2.0; // Zoom canvas to 200%
currentTranslate Interactive Pan
Interface: SVGSVGElement Type: SVGPoint

Gets or sets the current panning translation coordinates on the root SVG viewport.

svg.currentTranslate.x += 25;
svg.currentTranslate.y += 10;
preserveAspectRatio.baseVal Scaling Mode
Interface: SVGPreserveAspectRatio

Governs how the SVG graphic aligns and scales when aspect ratios differ between viewBox and container.

svg.preserveAspectRatio.baseVal.align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMIDYMID;

4. Animation & Timeline Control

pauseAnimations() Timeline Control
Interface: SVGSVGElement

Suspends all SMIL animations (e.g. <animate>, <animateTransform>) running within the SVG fragment.

svg.pauseAnimations();
unpauseAnimations() Timeline Control
Interface: SVGSVGElement

Resumes SMIL animations from their current paused offsets.

svg.unpauseAnimations();
animationsPaused() State Query
Interface: SVGSVGElement Returns: boolean

Returns true if SMIL animations are currently suspended in the document.

if (svg.animationsPaused()) svg.unpauseAnimations();
getCurrentTime() Timeline Query
Interface: SVGSVGElement Returns: float (seconds)

Returns the elapsed animation timeline time in seconds since SVG document initialization.

const elapsed = svg.getCurrentTime();
setCurrentTime() Timeline Scrub
Interface: SVGSVGElement Parameter: seconds (float)

Scrubs the SMIL animation timeline to an exact timestamp, updating all animated vectors immediately.

svg.setCurrentTime(2.5); // Seek to 2.5 seconds

Frequently Asked Questions

What is the difference between getBBox() and getBoundingClientRect() on SVG elements?

getBBox() returns the tight bounding rectangle in the local SVG user coordinate space before any transforms, CSS styling, or viewport scrolling are applied. getBoundingClientRect() returns the element's rendered pixel dimensions and position in screen window coordinates, including all applied CSS transforms and page scroll offsets.

How do I convert a mouse click (clientX, clientY) into SVG coordinates?

Use the SVG's getScreenCTM() inverse matrix: instantiate an SVGPoint or DOMPoint with (e.clientX, e.clientY), then call pt.matrixTransform(svg.getScreenCTM().inverse()). This accurately maps screen pixels back into the SVG's internal viewBox user coordinate space regardless of responsive scaling.

Why is getTotalLength() only available on SVGGeometryElement elements?

getTotalLength() requires defined geometric path trajectories. It is implemented on SVGGeometryElement subclasses (, , , , , , ). It cannot be called on structural container elements like , , or because they do not have a single continuous outline path.

Is SVGMatrix deprecated in modern web browsers?

Yes. The W3C Geometry Interfaces Module Level 1 deprecated SVGMatrix in favor of DOMMatrix. Modern browsers return a DOMMatrix object from getScreenCTM() and getCTM(), which maintains backwards compatibility with SVGMatrix properties (a, b, c, d, e, f) while adding support for 3D hardware matrix operations.

How does pathLength differ from getTotalLength()?

getTotalLength() is a DOM JavaScript method returning the computed geometric path distance in actual user units (e.g., 284.6px). The pathLength attribute is an author-defined calibration value (e.g., pathLength='1') that normalizes stroke-dasharray and stroke-dashoffset percentages regardless of physical dimensions.