HTML5 CANVAS
5
Canvas is an HTML element that can perform dynamic generation of 2D shapes and bitmap
images using JavaScript. HTML Canvas is a great alternative for drawing pictorial data
such as graphs, charts, and maps inside a web page. It is a low-level procedural model
which updates in the form of a bitmap.
The Canvas element is only the basic rectangular-shaped container for graphics, which is
usually done using JavaScript. The shapes or any other graphics implementation inside
the Canvas element can be done using JavaScript. HTML Canvas element is an HTML tag
like the div, a, or table, with the exception that its contents are rendered using JavaScript.
Why Canvas?
There are more than 1.2 billion websites available today which are unique from each other
and have various features involved. The canvas element is specially designed to make
visual content that can be uniquely applied to develop website UI and make them more
attractive for users. It is very easy to learn and requires a knowledge base of HTML and
JavaScript.
Canvas and <img> tag?
The canvas element looks like the HTML5 image <img> element at first, except for not
having “src” and “alt” attributes. Unlike the image element, the canvas element requires
the closing tag. The canvas element does have only two attributes which are width and
height. To design graphics using Canvas, we must use JavaScript.
Dimensions of Canvas
By default, canvas does not have a border and no content. The width and height can be
declared in the element or can be manually adjusted using the DOM properties of
JavaScript. When no width and height attributes are given in the code, it is set to default
where width is 300 pixels and height is 150 pixels.
The element can also be styled by CSS, using the Style tag, and can also give parameters
such as border color and thickness. If the CSS Style or dimensions are given do not match
with the initial Canvas, it will appear as a distorted rectangle. Even though, the Canvas
element can be styled using CSS, the actual drawing on the Canvas remains ineffective
and can be modified using Scripting languages.
Canvas element and Canvas context
While using canvas, it is important to understand the basic difference between the canvas
element and the canvas context, as often people get these both confused. The canvas
element is the actual DOM node that is embedded in the HTML page. A 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 3D (by using WebGL). We can assign only one
context to the canvas element using the getContext() function. Changing context
multiple times in the same canvas may result in the same object context.