CanvasRenderingContext2D .getTransform() method of the Canvas 2D API retrieves the current transformation matrix being applied to the context.

句法

let storedTransform = ctx.getTransform();
					

参数

None.

返回值

A DOMMatrix 对象。

The transformation matrix is described by: [ a c e b d f 0 0 1 ] \left[ \begin{array}{ccc} a & c & e \\ b & d & f \\ 0 & 0 & 1 \end{array} \right]

注意 : The returned object is not live, so updating it will not affect the current transformation matrix, and updating the current transformation matrix will not affect an already returned DOMMatrix .

范例

In the following example, we have two <canvas> elements. We apply a transform to the first one's context using CanvasRenderingContext2D.setTransform() and draw a square on it, then retrieve the matrix from it using getTransform() .

We then apply the retrieved matrix directly to the second canvas context by passing the DOMMatrix object directly to setTransform() , and draw a circle on it.

HTML

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

CSS

canvas {
  border: 1px solid black;
}
					

JavaScript

const canvases = document.querySelectorAll('canvas');
const ctx1 = canvases[0].getContext('2d');
const ctx2 = canvases[1].getContext('2d');
ctx1.setTransform(1, .2, .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();
					

结果

规范

规范 状态 注释
HTML 实时标准
The definition of 'CanvasRenderingContext2D.getTransform' in that specification.
实时标准

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
getTransform Chrome 68 Edge 79 Firefox 70 IE No Opera 55 Safari 11 WebView Android 68 Chrome Android 68 Firefox Android No Opera Android 48 Safari iOS 11 Samsung Internet Android 10.0

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: