Book Image

Canvas Cookbook

Book Image

Canvas Cookbook

Overview of this book

With the growing popularity of HTML5 Canvas, this book offers tailored recipes to help you develop portable applications, presentations, and games. The recipes are simple yet creative and build on each other. At every step, the book inspires the reader to develop his/her own recipe. From basic to advanced, every aspect of Canvas API has been covered to guide readers to develop their own application, presentation, or game.
Table of Contents (16 chapters)
Canvas Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Drawing horizontal, vertical, and assorted lines


The output of our second recipe looks like this:

How to do it...

A small change in the previous example builds our new recipe.

You need to do the following to build this recipe:

  1. Add three different functions named drawHorizontalLines(), drawVerticalLines(), and drawAssortedLines() to the <script> tag.

  2. Call these functions through the init() method, the same way as the drawLine() function was called.

After making the necessary changes, our recipe looks like this:

<html>
<head>
  <title>Lines Lines and More Lines</title>
    <script type="text/javascript">
      var can;
      var ctx;
      function init() {
        can = document.getElementById("MyCanvasArea");
        ctx = can.getContext("2d");
        drawHorizontalLines();
        drawVerticalLines();
        drawAssortedLines();
      }
      function drawLine(xstart,ystart,xend,yend,width,color)
      {
        ctx.beginPath();
        ctx.strokeStyle=color;
        ctx.lineWidth=width;
        ctx.moveTo(xstart,ystart);
        ctx.lineTo(xend,yend);
        ctx.stroke();
        ctx.closePath();
      }
      function drawHorizontalLines()
      {
        xs=10;  ys=10;xe=100; ye=10;
        c="teal"; w=2;  
        //draw 10 lines
        for(i=1;i<=10;i++)
        {
          drawLine(xs,ys,xe,ye,w++,c);
          ys+=15;  //change through y axis
          ye+=15;
        }
      }  
      function drawVerticalLines()
      {
        xs=130;  ys=10;xe=130;ye=160;
        c="crimson";w=2;  
        //draw 10 lines
        for(i=1;i<=10;i++)
        {
          drawLine(xs,ys,xe,ye,w++,c);
          xs+=15; //change through x axis  
          xe+=15;
        }
      }
      function drawAssortedLines()
      {
        //center point
        xcenter=400;ycenter=125;  xe=xcenter-100;ye=ycenter;
        c="orange";  w=2;
        //Second quadrant
        for(xe=xcenter-100;xe<=xcenter;xe+=5,ye-=5)  
          drawLine(xcenter,ycenter,xe,ye,w,c);
        //first quadrant
        for(ye=ycenter-100;ye<=ycenter;xe+=5,ye+=5)
          drawLine(xcenter,ycenter,xe,ye,w,c);  
        //fourth quadrant  
        for(xe=xcenter+100;xe>=xcenter;xe-=5,ye+=5)
          drawLine(xcenter,ycenter,xe,ye,w,c);  
        //third quadrant  
        for(ye=ycenter+100;ye>=ycenter;xe-=5,ye-=5)
          drawLine(xcenter,ycenter,xe,ye,w,c);
      }    
    </script>
</head>
<body onload="init()">
  <br/><br/>
  <center>
  <canvas id="MyCanvasArea" height="260" width="520" style="border:3px solid brown;">
  </canvas>
  </center>
</body>
</html>

For convenience, the function names and their calls shown through init() are made in bold to help you to understand where to make changes in the previous recipe.

How it works...

The basic functions moveTo() and lineTo() remain the same. However, three different functions are created, which contain loops to repeatedly call the previously mentioned line drawing function.

In the drawHorizontalLines(),the lines are drawn along the x axis. In each iteration, the width of the line increases, thereby showing a gradual increase in thickness.

In the drawVerticalLines() function, the lines are drawn along the y axis.

The function drawAssortedLines() has four different loops drawing lines in four different quadrants. The drawLine() function is used in the loop and, in every iteration, the parameter values for the function change to draw lines starting from different coordinates. For instance, in the first loop, the value of xe starts from 300, which is less than the value of xcenter. So, we start drawing from the left side of the center. On every iteration, the value of xe increases and the value for ye decreases by 5. Thus, the starting point of a line moves a bit inwards and a bit upwards. The line is drawn from (xe,ye) to (xcenter,ycenter). The quadrants are filled in, in an anticlockwise direction, starting from the 2nd quadrant.

When you apply the concept of quadrants, bear in mind that the center is (400,125) and not (0,0).

All three functions mentioned previously are called within init().

There's more...

Try the following:

  • Change the order in which the functions are called

  • Increase the number in the terminating condition of the loop from 10 to some higher number

  • Change the colors