This is an experimental technologyCheck the Browser compatibility table carefully before using this in production.
The CanvasRenderingContext2D
.resetTransform()
method of the Canvas 2D API resets the current transform to the identity matrix.
Syntax
void ctx.resetTransform();
Examples
Resetting the matrix
This example draws a rotated rectangle after modifying the matrix, and then resets the matrix using the resetTransform()
method.
HTML
<canvas id="canvas"></canvas>
JavaScript
The rotate()
method rotates the transformation matrix by 45°. The fillRect()
method draws a filled rectangle, adjusted according to that matrix.
const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); // Draw a rotated rectangle ctx.rotate(45 * Math.PI / 180); ctx.fillRect(60, 0, 100, 30); // Reset transformation matrix to the identity matrix ctx.resetTransform();
Result
Continuing with a regular matrix
Whenever you're done drawing transformed shapes, you should call resetTransform()
before rendering anything else. In this example, the first two shapes are drawn with a skew transformation, and the last two are drawn with the identity (regular) transformation.
HTML
<canvas id="canvas"></canvas>
JavaScript
const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); // Skewed rectangles ctx.transform(1, 0, 1.7, 1, 0, 0); ctx.fillStyle = 'gray'; ctx.fillRect(40, 40, 50, 20); ctx.fillRect(40, 90, 50, 20); // Non-skewed rectangles ctx.resetTransform(); ctx.fillStyle = 'red'; ctx.fillRect(40, 40, 50, 20); ctx.fillRect(40, 90, 50, 20);
Result
The skewed rectangles are gray, and the non-skewed rectangles are red.
Polyfill
You can also use the setTransform()
method to reset the current transform to the identity matrix, like so:
ctx.setTransform(1, 0, 0, 1, 0, 0);
Specifications
Specification | Status | Comment |
HTML Living StandardThe definition of 'CanvasRenderingContext2D.resetTransform' in that specification. | Living Standard |
Browser compatibility
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.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Chrome
Full support 31 |
Edge
Full support 79 |
Firefox
Full support 36 |
IE
No support No |
Opera
Full support Yes |
Safari
Full support Yes |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support 36 |
Opera Android
No support No |
Safari iOS
Full support Yes |
Samsung Internet Android
Full support Yes |
Legend
- Full support
- Full support
- No support
- No support
- Experimental. Expect behavior to change in the future.'
- Experimental. Expect behavior to change in the future.
See also
- The interface defining this method:
CanvasRenderingContext2D
CanvasRenderingContext2D.resetTransform() by Mozilla Contributors is licensed under CC-BY-SA 2.5.