- 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 - getContextAttributes() Method
The HTML Canvas getContextAttributes() method of Canvas API returns a Canvas object containing the same context parameters.
These attributes are generally requested by getContext() method on the context creation. It generally returns the context of any graphic drawn inside the canvas element when requested by using its object.
Syntax
Following is the syntax of HTML Canvas getContextAttributes() method
CanvasRenderingContext2D.getContextAttributes();
Parameters
Since it is only a return method, it does not take any parameters.
Return value
The interface CanvasRenderingContext2D method uses the context object and the object parameters are returned via console or window alerts when the HTML Canvas getContextAttributes() method is used by the object.
Example
The following example returns the context parameters of the canvas element in the console using the HTML Canvas getContextAttributes() method. The implementation code is given below.
<!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="200" height="200" style="border: 1px solid black;"></canvas> <script> function Context() { var canvas = document.getElementById("canvas"); var context = canvas.getContext("2d"); console.log(context.getContextAttributes()); } </script> </body> </html>
Output
The output returned by the method on the webpage as −
The output that can be seen on console screen as −
Example
The following example returns the context parameters of a filled rectangle inside canvas element in the console. The implementation code is given below.
<!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="200" height="200" style="border: 1px solid black;"></canvas> <script> function Context() { var canvas = document.getElementById("canvas"); var context = canvas.getContext("2d"); context.fillRect(10, 10, 150, 100); console.log(context.getContextAttributes()); } </script> </body> </html>
Output
The output returned by the method on web page as −
The output that can be seen on the console screen as −
To Continue Learning Please Login
Login with Google