What is getContext()
method and what is drawing context exactly? why we always pass the string 2d
to the getContext()
method?
The getContext() function is the function that you use to get access to the canvas tags 2D drawing functions. As of April 2014, there are two types of context that are available to you: 2d and webgl . These provide you with the API that you use to draw on the canvas.
In Computer graphics, a drawing context is an abstraction (class/object) that encapsulates how you are going to draw stuff. At a 100k foot level, computer graphics is about converting drawing commands to pixels (image).
getContext():It returns the Context which is linked to the Activity from which it is called.
The canvas context is an object with properties and methods that you can use to render graphics inside the canvas element. The context can be 2d or webgl (3d). Each canvas element can only have one context. If we use the getContext() method multiple times, it will return a reference to the same context object.
Context is a way to choose what you are going to do with your canvas.
For moment you can use getContext for 2d (2dcanvas) or for 3d (WebGL).
HTML5 Specification say's about getContext : "Returns an object that exposes an API for drawing on the canvas. The first argument specifies the desired API. Subsequent arguments are handled by that API."
You can find specifications for each API there : https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-getcontext
It is also good to know that "webgl" is the correct name for API but for moment, as it is experimental you should use "experimental-webgl" to start creating WebGL content
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With