CanvasRenderingContext2D: getTransform() Methode

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.

Die CanvasRenderingContext2D.getTransform()-Methode der Canvas 2D API ruft die aktuell auf den Kontext angewendete Transformationsmatrix ab.

Syntax

js
getTransform()

Parameter

Keine.

Rückgabewert

Ein DOMMatrix-Objekt.

Die Transformationsmatrix wird beschrieben durch:

[acebdf001]\left[ \begin{array}{ccc} a & c & e \\ b & d & f \\ 0 & 0 & 1 \end{array} \right]

Hinweis: Das zurückgegebene Objekt ist nicht live, sodass eine Aktualisierung desselben die aktuelle Transformationsmatrix nicht beeinflusst und die Aktualisierung der aktuellen Transformationsmatrix keine Auswirkungen auf eine bereits zurückgegebene DOMMatrix hat.

Beispiele

Im folgenden Beispiel haben wir zwei <canvas>-Elemente. Wir wenden eine Transformation auf den Kontext des ersten Canvas an, indem wir CanvasRenderingContext2D.setTransform() verwenden und ein Quadrat darauf zeichnen. Danach rufen wir die Matrix mit getTransform() ab.

Anschließend wenden wir die abgerufene Matrix direkt auf den Kontext des zweiten Canvas an, indem wir das DOMMatrix-Objekt direkt an setTransform() übergeben, und zeichnen einen Kreis darauf.

HTML

html
<canvas width="240"></canvas> <canvas width="240"></canvas>

CSS

css
canvas {
  border: 1px solid black;
}

JavaScript

js
const canvases = document.querySelectorAll("canvas");
const ctx1 = canvases[0].getContext("2d");
const ctx2 = canvases[1].getContext("2d");

ctx1.setTransform(1, 0.2, 0.8, 1, 0, 0);
ctx1.fillRect(25, 25, 50, 50);

let storedTransform = ctx1.getTransform();
console.log(storedTransform);

ctx2.setTransform(storedTransform);
ctx2.beginPath();
ctx2.arc(50, 50, 50, 0, 2 * Math.PI);
ctx2.fill();

Ergebnis

Spezifikationen

Specification
HTML
# dom-context-2d-gettransform-dev

Browser-Kompatibilität

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
getTransform

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Siehe auch