SVGSVGElement: createSVGTransformFromMatrix() Methode

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.

Die createSVGTransformFromMatrix() Methode der Schnittstelle SVGSVGElement erstellt ein SVGTransform Objekt außerhalb jeglicher Dokumentenbäume, basierend auf dem angegebenen DOMMatrix Objekt.

Syntax

js
createSVGTransformFromMatrix(matrix)

Parameter

matrix

Ein DOMMatrix Objekt, das die anfängliche Matrix für die Transformation darstellt.

Rückgabewert

Ein SVGTransform Objekt, das auf die gegebene Matrixtransformation initialisiert ist. Es ist eine matrix() Transformation, wenn die matrix 2D ist, und eine matrix3d() Transformation andernfalls.

Beispiele

Erstellen einer Transformation aus einer Matrix

html
<svg id="exampleSVG" width="200" height="200">
  <rect id="exampleRect" x="50" y="50" width="100" height="50" fill="blue" />
</svg>
js
const svgElement = document.getElementById("exampleSVG");
const rectElement = document.getElementById("exampleRect");

// Create a new matrix
const matrix = svgElement.createSVGMatrix();
matrix.a = 1; // Scale x
matrix.d = 1; // Scale y
matrix.e = 50; // Translate x
matrix.f = 50; // Translate y

// Create a new SVGTransform from the matrix
const transform = svgElement.createSVGTransformFromMatrix(matrix);

// Apply the transform to the rectangle
rectElement.transform.baseVal.appendItem(transform);

Spezifikationen

Specification
Scalable Vector Graphics (SVG) 2
# __svg__SVGSVGElement__createSVGTransformFromMatrix

Browser-Kompatibilität

BCD tables only load in the browser

Siehe auch