SVGTransformList: createSVGTransformFromMatrix()-Methode - Web-APIs | MDN

Baseline Weitgehend verfügbar

Diese Funktion ist gut etabliert und funktioniert auf vielen Geräten und in vielen Browserversionen. Sie ist seit Juli 2015 browserübergreifend verfügbar.

Die createSVGTransformFromMatrix()-Methode der SVGTransformList-Schnittstelle erstellt ein SVGTransform-Objekt, das zu einer Transformation des Typs SVG_TRANSFORM_MATRIX initialisiert wird und dessen Werte die gegebene Matrix sind.

Die Werte des Parameter-Matrix werden kopiert; die Matrix-Parameter wird nicht als SVGTransform::matrix übernommen.

Syntax

js

createSVGTransformFromMatrix(matrix)

Parameter

matrix

Ein DOMMatrix-Objekt; die Transformationsmatrix.

Rückgabewert

Ein SVGTransform-Objekt.

Beispiele

Erstellung einer Transformation aus einer Matrix

html

<svg width="200" height="200">
  <rect width="100" height="100" fill="blue" />
</svg>

js

const svgElement = document.querySelector("svg");
const rectElement = svgElement.querySelector("rect");

// Access the transform list of the <rect> element
const transformList = rectElement.transform.baseVal;

// Create a DOMMatrix object for a rotation transformation
const rotationMatrix = new DOMMatrix();
rotationMatrix.a = Math.cos(Math.PI / 4); // 45-degree rotation
rotationMatrix.b = Math.sin(Math.PI / 4);
rotationMatrix.c = -Math.sin(Math.PI / 4);
rotationMatrix.d = Math.cos(Math.PI / 4);

// Create an SVGTransform object from the matrix
const svgTransform = transformList.createSVGTransformFromMatrix(rotationMatrix);

// Append the new transformation to the transform list
transformList.appendItem(svgTransform);

console.dir(svgTransform); // Output: SVGTransform { type: 1, matrix: SVGMatrix, angle: 0 }

Spezifikationen

Spezifikation
Scalable Vector Graphics (SVG) 2
# __svg__SVGTransformList__createSVGTransformFromMatrix

Browser-Kompatibilität

Siehe auch

Help improve MDN

Erfahren Sie, wie Sie beitragen können Diese Seite wurde automatisch aus dem Englischen übersetzt.