Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. CanvasRenderingContext2D
  4. getTransform()

CanvasRenderingContext2D: getTransform() method

Baseline Widely available

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

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

Syntax

js
getTransform()

Parameters

None.

Return value

ADOMMatrix object.

The transformation matrix is described by:

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

Note:The returned object is not live, so updating it will notaffect the current transformation matrix, and updating the current transformationmatrix will not affect an already returnedDOMMatrix.

Examples

In the following example, we have two<canvas> elements. We apply atransform to the first one's context usingCanvasRenderingContext2D.setTransform() and draw a square on it, thenretrieve the matrix from it usinggetTransform().

We then apply the retrieved matrix directly to the second canvas context by passing theDOMMatrix object directly tosetTransform(), and draw a circleon it.

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();

Result

Specifications

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

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp