Introduction

This article shows you a world of possibilities using the HTML5 Canvas element along with what you can create with it, applications domains, methods, browser compatibility, features, and a reference example.

Canvas

In the most simple words, we can define Canvas as an element to draw graphics into via scripting. This tag is nothing but a container for graphics. We need to use a script to provide the structure or shapes of the graphics.

HTML5 Canvas | Methods

There are several methods for various functionalities, likes drawing shapes, adding images, graphics and so on in a Canvas element.

Attributes

HTML5 Canvas | Browser Compatibility

The Canvas element works in nearly every browser, but the thing is it needs to be updated. It works well with Internet Explorer 9.0+, Chrome, Opera, Firefox and Safari. Why HTML5 Canvas? | Features Here are some reasons you need to know if you really want a warm touch of the HTML5 Canvas element. These features are:

Html5 Canvas | Possibilities

Here are presenting validations regarding my article, I mentioned a few applications domains of HMTL5 canvas elements:

HTML5 Canvas | Attributes

There are only two specific attributes of HTML5 Canvas elements:

In spite of this, there are several other attributes too, like name, id, class, type and so on.

<canvas id="firstCanvas" width="250" height="320"></canvas>

Creation of a canvas

Canvas is a simple rectangular or square area on an HTML page that is specified with the <canvas> element. By default it is blank or we can say it doesn't have any specific content until or unless we declare any attribute with it or in it. There are 2 simple steps in creating a canvas.

Defining Markup This tag line is used as markup in the HTML page for defining canvas functionality:

Adding Attributes Attributes are user-dependent, you can use attributes depending on your preference or your requirements.

For example

<canvas id="firstCanvas" width="250" height="320" style="border:1px solid #993300;"></canvas>

Canvas with JavaScript

We can only define the functioning part from the canvas element but for graphics, shapes or any other work, we need to enable scripting with it. In general, we do use JavaScript.

For example

<canvas id="firstCanvas" width="250" height="320" style="border:1px solid #993300;"></canvas>
<script>
  var v = document.getElementById("firstCanvas");
  var ctx = v.getContext("2d");
  ctx.fillStyle = "#FF0000";
  ctx.fillRect(0, 0, 320, 250);
</script>

Outcome

Outcomes

Reference Example

<html>
   <head>
      <title> HTML5 Canvas</title>
   </head>
   <body>
      <h2> Graphics | Image</h2>
      <img id="" src="" alt="" width="250" height="320" />
      <p> Canvas: </p>
      <canvas id="firstCanvas" width="250" height="320" style="border:1px solid #000000;">
         <!-- your text will be here-->
      </canvas>
      <script>
         var v = document.getElementById("firstCanvas");
         var ctx = v.getContext("2d");
         var img = document.getElementById("Dexter");
         ctx.drawImage(img,10,10);
      </script>
   </body>
</html>

Outcome

Graphics

I hope you will like that. For more stuff of HTML5 keep visiting.