Working With Tree Fractal Using HTML 5

Introduction

 
In this article, we are going to understand working with tree fractals using HTML5. In this, we will see various randomized trees when the page is refreshed again and again in the browser.
 
Here we will use some JavaScript and some styles along with HTML code. Just go through the steps to see how to create this application. Let's see how the TreeFractal application can be created. To do so use the following steps.
 
Step 1 : Open a HTML editor or Visual Studio.
sd.gif
 
Open File menu ->select new ->Choose Website then.
 
0000.jpg
 
This is where we will create an HTML5 application.
  • Go to Solution Explorer
  • Right-click on the Application name
  • Select Add-->add new item
  • Now in the window that opens, select an HTML page or a new Webform
  • Rename it to Treefractal.aspx
tree 2.gif
 
Step 2: In this section, we will create the style for the media and create the .css on the media screen. Put the given script in the Head section of the HTML or between the <head>--</head> tags. Here the CSS is used for design purposes. CSS Script
  1. <style>  
  2. body {  
  3.   margin: 0px;  
  4.   padding: 0px;  
  5.   font-family: Comic Sans MS;  
  6.   outline-color: Yellow;  
  7. }  
  8.   
  9. #Bubble {  
  10.   border: 2px solid #9C9898;  
  11.   margin-top: 50px;  
  12.   margin-left: 60px;  
  13.   background-color: #00B2EE;  
  14.   box-shadow: 5px 5px 8px #222;  
  15. }  
  16.   
  17. .title {  
  18.   text-align: center;  
  19.   font-family: Segoe UI Light, Arial, Helvetica;  
  20.   font-size: 2.2em;  
  21.   margin: 1em;  
  22. }  
  23.   
  24. .info {  
  25.   text-align: center;  
  26.   font-family: Segoe UI Light, Arial, Helvetica;  
  27.   font-size: 1.2em;  
  28.   margin: 0.25em;  
  29. }</style> 
Step 3 : In this part we need to work on some JavaScript. To fully understanding how the JavaScript works, download the attached .rar file and run the TreeFractal application. The whole JavaScript looks as in the following.
  1. <script>  
  2.         function drawBranches(startX, startY, trunkWidth, level)  
  3.          {  
  4.             canvas = document.getElementById("myCanvas");  
  5.             context = canvas.getContext("2d");  
  6.             if (level < 12) {  
  7.                 var changeX = 100 / (level + 1);  
  8.                 var changeY = 200 / (level + 1);  
  9.                 var topRightX = startX + Math.random() * changeX;  
  10.                 var topRightY = startY - Math.random() * changeY;  
  11.                 var topLeftX = startX - Math.random() * changeX;  
  12.                 var topLeftY = startY - Math.random() * changeY;  
  13.                 // draw right branch  
  14.                 context.beginPath();  
  15.                 context.moveTo(startX + trunkWidth / 4, startY);  
  16.                 context.quadraticCurveTo(startX + trunkWidth / 4, startY - trunkWidth, topRightX, topRightY);  
  17.                 context.lineWidth = trunkWidth;  
  18.                 context.lineCap = "round";  
  19.                 context.stroke();  
  20.                 // draw left branch  
  21.                 context.beginPath();  
  22.                 context.moveTo(startX - trunkWidth / 4, startY);  
  23.                 context.quadraticCurveTo(startX - trunkWidth / 4, startY -  
  24.                     trunkWidth, topLeftX, topLeftY);  
  25.                 context.lineWidth = trunkWidth;  
  26.                 context.lineCap = "round";  
  27.                 context.stroke();  
  28.                 drawBranches(topRightX, topRightY, trunkWidth * 0.7, level + 1);  
  29.                 drawBranches(topLeftX, topLeftY, trunkWidth * 0.7, level + 1);  
  30.             }  
  31.         }  
  32.         window.onload = function ()  
  33.         {  
  34.             canvas = document.getElementById("myCanvas");  
  35.             context = canvas.getContext("2d");  
  36.             drawBranches(canvas.width / 2, canvas.height, 50, 0);  
  37.         };  
  38. </script> 
Step 4: In this section, we are going to become familiar with the body part of HTML scripting. Replace this script from the body section of the Treefractal.aspx page. Here we pass a #myCanvas in the canvas tag.
  1. <body style="background-color: #D1D1D1">  
  2.     <center>  
  3.         <h1>  
  4.             TREE FRACTAL DISPLAYING  
  5.         </h1>  
  6.     </center>  
  7.     <hr />  
  8.     <canvas id="myCanvas" width="578" height="500">  
  9.         </canvas>  
  10. </body> 
Step 5: The complete code for the TreeFractal application. 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Treefractal.aspx.cs" Inherits="TreeFractal._Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <style>  
  6.      Copy and paste step 2 here    
  7.     </style>  
  8.     <script>  
  9.      Copy and paste step 3 here  
  10.     </script>  
  11. </head>  
  12. <body style="background-color: #D1D1D1">  
  13.     <center>  
  14.         <h1>  
  15.             TREE FRACTAL DISPLAYING  
  16.         </h1>  
  17.     </center>  
  18.     <hr />  
  19.     <canvas id="myCanvas" width="578" height="500">  
  20.         </canvas>  
  21. </body>  
  22. </html> 
Step 6: Output Press F5 Note: For the accurate output of HTML5 applications, you must have the Google Chrome browser on your PC. You will see the different randomized trees when the page is refreshed again and again on the browser.
 
tree 1.gif
 
When you refresh the page in the browser the tree will be changed.
 
tree.gif
 
Here are some useful resources