General HTML5/CSS3
One of the most important instruments in a painters toolkit is their canvas. It gives them the freedom to express all kinds of feelings, impressions, ideas, and so forth, in almost unlimited variations and combinations. And that freedom can be restricted only by two things� their skill level and their imagination.
The situation in the web development world is similar. With the ongoing progress of HTML5 and its powerful specifications, web developers have gained the ability to do things that were impossible in the past. Drawing graphics and creating animations directly in the browser is now completely possible thanks to a technology called HTML5 Canvas.
The canvas element is an element defined in HTML code using width
and height
attributes. The real power of the canvas element, however, is accomplished by taking advantage of the HTML5 Canvas API. This API is used by writing JavaScript that can access the canvas area through a full set of drawing functions, thus allowing for dynamically generated graphics.
Here are some reasons you might want to consider learning to use HTML's canvas feature:
OK, canvas is great. But what exactly can use it for? Let's see.
Now that we know why we should learn canvas, let's look at the basics of HTML5 Canvas and how to start using it.
Every HTML5 canvas element must have a context. The context defines what HTML5 Canvas API you'll be using. The 2d context is used for drawing 2D graphics and manipulating bitmap images. The 3d context is used for 3D graphics creation and manipulation. The latter is actually called WebGL and it's based on OpenGL ES.
Besides the canvas elements width
and height
attributes, you can also use CSS to set the size of a canvas element. However, sizing a canvas element with CSS is not the same as setting the elementswidth
and height
attributes. This is because a canvas actually has two sizes: the size of the element itself and the size of the elements drawing surface.
When you set the elements width
and height
attributes, you set both the elements size and the size of the elements drawing surface; however, when you use CSS to size a canvas element, you set only the elements size and not the drawing surface. When the canvas elements size does not match the size of its drawing surface, the browser scales the drawing surface to fit the element.
Because of this, it's a good idea to use the canvas elements width
and height
attributes to size the element, instead of using CSS.
In a 2D space, positions are referenced using X and Y coordinates. The X axis extends horizontally, and the Y axis extends vertically. The center has a position x = 0
and y = 0
, that can also be expressed as (0, 0)
. This method of positioning objects, used in mathematics, is known as the Cartesian coordinate system.
The Canvas coordinate system, however, places the origin at the upper-left corner of the canvas, with X coordinates increasing to the right and Y coordinates increasing toward the bottom of the canvas. So unlike a standard Cartesian coordinate space, the Canvas space doesn't have visible negative points. Using negative coordinates wont cause your application to fail, but objects positioned using negative coordinate points wont appear on the page.