- HTML Canvas Tutorial
- HTML Canvas - Home
- HTML Canvas - Introduction
- Environmental Setup
- HTML Canvas - First Application
- HTML Canvas - Drawing 2D Shapes
- HTML Canvas - Path Elements
- 2D Shapes Using Path Elements
- HTML Canvas - Colors
- HTML Canvas - Adding Styles
- HTML Canvas - Adding Text
- HTML Canvas - Adding Images
- HTML Canvas - Canvas Clock
- HTML Canvas - Transformations
- Composting and Clipping
- HTML Canvas - Basic Animations
- Advanced Animations
- HTML Canvas API Functions
- HTML Canvas - Element
- HTML Canvas - Rectangles
- HTML Canvas - Lines
- HTML Canvas - Paths
- HTML Canvas - Text
- HTML Canvas - Colors and Styles
- HTML Canvas - Images
- HTML Canvas - Shadows and Transformations
- HTML Canvas Useful Resources
- HTML Canvas - Quick Guide
- HTML Canvas - Useful Resources
- HTML Canvas - Discussion
HTML Canvas - getTransform() Method
The HTML Canvas getTransform() method of Canvas 2D API gets the current transformation matrix applied to the context object of CanvasRenderingContext2D interface.
Syntax
Following is the syntax of HTML Canvas getTransform() method −
CanvasRenderingContext2D.getTransform();
Parameters
It does not take any parameters as it only returns the object data to which transformation matrix is applied.
Return value
It returns a transformation DOMMatrix object which is applied to the Canvas element.
Example
The following example returns transformation parameters on window alert of a CanvasRenderingContext2D interface context object of Canvas element using HTML Canvas getTransform() method.
<!DOCTYPE html> <html lang="en"> <head> <title>Reference API</title> <style> body { margin: 10px; padding: 10px; } </style> </head> <body onload="Context();"> <canvas id="canvas" width="400" height="300" style="border: 1px solid black;"></canvas> <script> function Context() { var canvas = document.getElementById("canvas"); var context = canvas.getContext("2d"); context.setTransform(1, 0.5, 0.7, 0.9, 0.8, 0); context.fillRect(25, 25, 150, 100); var trans = context.getTransform(); alert(trans); } </script> </body> </html>
Output
The output returned by the following code on the webpage as −
Example
The following example applies a transformation matrix to the graphic rendered inside the Canvas element and returns its properties by the console using getTransform() method.
<!DOCTYPE html> <html lang="en"> <head> <title>Reference API</title> <style> body { margin: 10px; padding: 10px; } </style> </head> <body> <canvas id="canvas" width="150" height="250" style="border: 1px solid black;"></canvas> <script> var canvas = document.getElementById('canvas'); var context = canvas.getContext('2d'); context.fillStyle = 'blue'; context.setTransform(0, 0.2, 0.4, 0.6, 0.8, 1); context.fillRect(50, 50, 200, 200); var trans = context.getTransform(); console.log(trans); </script> </body> </html>
Output
The output returned by the following code on the webpage as −
To Continue Learning Please Login
Login with Google