HTML5 Canvas Tag

HTML5 has made it very easy to draw graphics on a web page with the help of Javascript. Canvas is actually used as a container of a graphic which has not border and no content in it. Actual graphic is written using Javascript. Now you can not only draw simple graphics but can render game graphics, photo compositions, other visual images on the fly. You can give various effects like coloring, multicoloring figures or text, gradiant etc with the use of canvas.

The HTML5 <canvas> Tag has many attributes like width, height, style, class, id and name which you can use as and when required.

Syntax

<canvas id="canvas1" width="150" height="250">
  graphic code using Javascript will come here...
</canvas>

Browser Support

Almost all latest browsers like Safari, Google Chrome, Mozilla Firefox, Opera, Internet Explorer 9.0 support many HTML5 features and functionalities. The good thing is that now many mobiles web browsers like iPhones, iPads, and Android phones all have excellent support for HTML5.

Canvas Border

The <style> attribute is used to create a border of canvas.

Canvas Border Example

<!DOCTYPE HTML>  
<html>  
<head>  
    <meta charset=UTF-8" />  
    <title>HTML5 Canvas Border Example</title>  
</head>  
<body>
<canvas id="canvas1" width="150" height="150" style="border:2px solid #FF0000;">
</canvas>
</body>
</html>

This will produce following result

Canvas Circle

The ctx.arc() function is used to create circles based on a center points and a radius.

Canvas Circle Example

<!DOCTYPE HTML>  
<html>  
<head>  
    <meta charset=UTF-8" />  
    <title>HTML5 Canvas Circle Example</title>  
</head>  
<body>
<canvas id="canvas2" width="200" height="150" style="border:2px solid #FF0000;">
</canvas>
<script>
var c = document.getElementById("canvas2");
var con = c.getContext("2d");
con.beginPath();
con.arc(100,60,50,0,2*Math.PI);
con.fillStyle = 'yellow';
con.fill();
con.stroke();
</script>
</body>
</html>

This will produce following result