Section 1: Understanding the Canvas
Introduced in the summer of 2004 by Apple in its Safari Browser, Canvas is now supported by many browsers. While Internet Explorer does not directly support the tag as of yet, there are JavaScript Libraries that emulate <canvas> syntax using Microsoft's Vector Markup Language (VML). From a markup point of view, you simply put the element in the page, name it with an id attribute, and define its dimensions with height and width attributes:
After you place a <canvas> tag in a document, your next step is to use JavaScript to access and draw on the element. For example, the following fetch the object by its id value and creates a two-dimensional drawing context:
Once we have drawing objects for a game we have various methods used for the Canvas but we are focused on a few methods.
For example, the "strokeRect(x,y,width,height)" method takes x and y coordinates and height and width, all specified as numbers representing pixels.
- <canvas id="canvas" width="300" height="300">
- <strong>Canvas Supporting Browser Required</strong>
- </canvas>
- var canvas = document.getElementById("canvas");
- var context = canvas.getContext("2d");
For example:
context.strokeRect(10,10,150,50);
Will draw a simple rectangle of 150 pixels by 50 pixels starting at the coordinates 10,10 from the origin of the placed <canvas> tag.
If you wanted to set a particular color for the stroke then you might set it with the "strokeStyle()" method, like so:
You can use the "fillRect(x,y,width,height)" method to make a rectangle, but this time in a solid manner:
- context.strokeStyle = "blue";
- context.strokeRect(10,10,150,50);
context.fillRect(150,30,75,75);
By default, the fill color will be black, but you can define a different fill color using the "fillColor()" method.
For example:
context.fillStyle = "#da0000";
Also, use standard CSS color functions, which may include opacity.
For example, the opacity of the reddish fill is set to 40 percent:
context.fillStyle = "rgba(218,112,214,0.4)";
Now:
- Open Notepad
- Write a simple program using <canvas>
- Save it with a .htm or .html extension.

Code used
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>HTML5 canvas example</title>
- <script type="text/javascript">
- window.onload = function () {
- var canvas = document.getElementById("canvas");
- var context = canvas.getContext("2d");
- context.strokeStyle = "orange";
- context.strokeRect(10, 10, 150, 50);
- context.fillStyle = "rgba(218,0,0,0.4)";
- context.fillRect(150, 30, 75, 75);
- }
- </script>
- </head>
- <body>
- <h1>
- Simple Canvas Examples</h1>
- <canvas id="canvas" width="300" height="300">
- <strong>Canvas Supporting Browser Required</strong>
- </canvas>
- </body>
- </html>
2. Save


Section 2: Drawing and Styling Lines and Shapes
What the path API is?
For example, the following draws a V shape using "lineTo()":
If you were to add a "context.closePath()" before "context.stroke()", the V shape would turn into a triangle, because closePath() would connect the last point and the first point. Also, by calling "fill()" instead of "stroke()", the triangle will be filled in with whatever the fill color is, or black if none is specified. Of course, you can call both "fill()" and "stroke()" on any drawn shape if you want to have a stroke around a filled region. Thus, to style the drawing, you can specify the Fill Style and Stroke Style and maybe even define the width of the line using line width, see the following:
You can change the color by setting the fillColor property. In addition to the CSS color values, you can also set the fillColor to a gradient object. A gradient object can be created using "createLinearGradient()" or "createRadialGradient()". The following example creates a simple linear gradient that will be applied to a rectangle using the "createLinearGradient(x1,y1,x2,y2)" method. The gradient is positioned at 10,150 and is set to go 200 pixels in both directions.
- context.beginPath();
- context.lineTo(20,100);
- context.lineTo(120,300);
- context.lineTo(220,100);
- context.stroke();
- context.strokeStyle = "blue";
- context.fillStyle = "red";
- context.lineWidth = 10;
- context.beginPath();
- context.lineTo(200,10);
- context.lineTo(200,50);
- context.lineTo(380,10);
- context.closePath();
- context.stroke();
- context.fill();
var lg = context.createLinearGradient(10,150,200,200);
Next, the gradient colors are added using the "addColorStop()" method. This specifies a color and the offset position in the gradient where the color should occur. The offset must be between 0 and 1.
Of course, you could use the rgba CSS function to create a gradient with transparency as well. Finally, the fillColor is set to the gradient. Here is the complete code snippet, followed by a visual example:
To create a radial gradient using "createRadialGradient(x1,y1,r1,x2,y2,r2)", you must set the position and radius of two circles to serve as the gradient.
- lg.addColorStop(0,"#B03060");
- lg.addColorStop(0.75,"#4169E1");
- lg.addColorStop(1,"#FFE4E1");
- var lg = context.createLinearGradient(10,150,200,200);
- lg.addColorStop(0,"#B03060");
- lg.addColorStop(0.5,"#4169E1");
- lg.addColorStop(1,"#FFE4E1");
- context.fillStyle = lg;
- context.beginPath();
- context.rect(10,150,200,200);
- context.fill();
For example:
Consider this entire example:
- var rg = context.createRadialGradient(350,300,80,360,250,80);
- rg.addColorStop(0,"#A7D30C");
- rg.addColorStop(0.9,"#019F62");
- rg.addColorStop(1,"rgba(1,159,98,0) ");
- context.fillStyle = rg;
- context.beginPath();
- context.fillRect(250,150,200,200);
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>HTML5 canvas lines and shapes example</title>
- <script type="text/javascript">
- window.onload = function () {
- var canvas = document.getElementById("canvas");
- var context = canvas.getContext("2d");
- context.strokeStyle = "blue";
- context.fillStyle = "red";
- context.lineWidth = 10;
- context.beginPath();
- context.lineTo(200, 10);
- context.lineTo(200, 50);
- context.lineTo(380, 10);
- context.closePath();
- context.stroke();
- context.fill();
- var lg = context.createLinearGradient(10, 150, 200, 200);
- lg.addColorStop(0, "#B03060");
- lg.addColorStop(0.5, "#4169E1");
- lg.addColorStop(1, "#FFE4E1");
- context.fillStyle = lg;
- context.beginPath();
- context.rect(10, 150, 200, 200);
- context.fill();
- var rg = context.createRadialGradient(50, 50, 10, 60, 60, 50);
- rg.addColorStop(0, "#A7D30C");
- rg.addColorStop(0.9, "#019F62");
- rg.addColorStop(1, "rgba(1,159,98,0)");
- context.fillStyle = rg;
- context.beginPath();
- context.fillRect(0, 0, 130, 230);
- context.beginPath();
- context.lineTo(250, 150);
- context.lineTo(330, 240);
- context.lineTo(410, 150);
- context.stroke();
- }
- </script>
- </head>
- <body>
- <h1>
- Simple Shapes on canvas Example</h1>
- <canvas id="canvas" width="500" height="500">
- <strong>Canvas Supporting Browser Required</strong>
- </canvas>
- </body>
- </html>









Kunal VaishyaPosted Jul 18, 2013, 5:21 AM
Nice article
Tripti TiwariPosted Jul 17, 2013, 12:58 PM
Dinesh Sir Thank you!!
Akshay RajputPosted Jul 17, 2013, 12:09 PM
nice work....
Dinesh BeniwalPosted Jul 17, 2013, 10:58 AM
Good Start Tripti.
Gohil JayendrasinhPosted Jul 16, 2013, 6:50 AM
NICE
Vikas AhlawatPosted Jul 16, 2013, 3:16 AM
Nice Article.... & Nice start.. :)
Tripti TiwariPosted Jul 15, 2013, 5:25 AM
Thanks to all... :) :)
Akhil MittalPosted Jul 14, 2013, 11:34 PM
Good Effort tripti.Nice Article.Welcome to c-sharpcorner :-)
Sanjeeb LenkaPosted Jul 14, 2013, 1:13 PM
welcome Tripti. good work
Sourav KayalPosted Jul 14, 2013, 11:08 AM
Welcome to c-sharpcorner. keep it up.