Canvas Graphical Equation Representation Using HTML 5

Introduction

 
In this article, we are going to understand the creation of graphical equation representations using the HTML 5 canvas. In this section of this article, we will perform a graphical equation by setting up the co-ordinate geometry within the canvas tag and its properties.
 
A Graphical equation displays when the application runs 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 CanvasGraphEquation application can be created. To do so use the following steps.
 
Step 1 : Open a HTML editor or Visual Studio. Open File menu ->select new ->Choose Website
 
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 new Webform
  • Rename it to canvasgraphicalequation.aspx
canvasg1.png
 
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. {  
  4.   margin0px;  
  5.   padding0px;  
  6. }  
  7. Canvas  
  8. {  
  9.    border2px solid #9C9898;  
  10.    margin-top50px;  
  11.    margin-left50px;  
  12.    background-color#F4D19F;  
  13.    box-shadow: 5px 5px 8px #222;  
  14.  }  
  15. .title  
  16. {  
  17.    text-aligncenter;  
  18.    font-family: Segoe UI Light, ArialHelvetica;  
  19.    font-size2.2em;  
  20.    margin1em;  
  21. }  
  22. .info  
  23. {  
  24.    text-aligncenter;  
  25.    font-family: Segoe UI Light, ArialHelvetica;  
  26.    font-size1.2em;  
  27.    margin0.25em;  
  28. }  
  29. </style> 
Step 3: In this part, we need to work on some JavaScript. To fully understand how JavaScript works, download the attached .rar file and run the CanvasGraphEquation application.
 
The whole JavaScript looks as in the following:
  1. <script>  
  2.         function Graph(config)  
  3.           {  
  4.             // user defined properties  
  5.             this.canvas = document.getElementById(config.canvasId);  
  6.             this.minX = config.minX;  
  7.             this.minY = config.minY;  
  8.             this.maxX = config.maxX;  
  9.             this.maxY = config.maxY;  
  10.             this.unitsPerTick = config.unitsPerTick;  
  11.             // constants  
  12.             this.axisColor = "#aaa";  
  13.             this.font = "8pt Calibri";  
  14.             this.tickSize = 20;  
  15.             // relationships  
  16.             this.context = this.canvas.getContext("2d");  
  17.             this.rangeX = this.maxX - this.minX;  
  18.             this.rangeY = this.maxY - this.minY;  
  19.             this.unitX = this.canvas.width / this.rangeX;  
  20.             this.unitY = this.canvas.height / this.rangeY;  
  21.             this.centerY = Math.round(Math.abs(this.minY / this.rangeY) * this.canvas.height);  
  22.             this.centerX = Math.round(Math.abs(this.minX / this.rangeX) * this.canvas.width);  
  23.             this.iteration = (this.maxX - this.minX) / 1000;  
  24.             this.scaleX = this.canvas.width / this.rangeX;  
  25.             this.scaleY = this.canvas.height / this.rangeY;  
  26.             // draw x and y axis  
  27.             this.drawXAxis();  
  28.             this.drawYAxis();  
  29.         }  
  30.         Graph.prototype.drawXAxis = function ()  
  31.          {  
  32.             var context = this.context;  
  33.             context.save();  
  34.             context.beginPath();  
  35.             context.moveTo(0, this.centerY);  
  36.             context.lineTo(this.canvas.width, this.centerY);  
  37.             context.strokeStyle = this.axisColor;  
  38.             context.lineWidth = 2;  
  39.             context.stroke();  
  40.             // draw tick marks  
  41.             var xPosIncrement = this.unitsPerTick * this.unitX;  
  42.             var xPos, unit;  
  43.             context.font = this.font;  
  44.             context.textAlign = "center";  
  45.             context.textBaseline = "top";  
  46.             // draw left tick marks  
  47.             xPos = this.centerX - xPosIncrement;  
  48.             unit = -1 * this.unitsPerTick;  
  49.             while (xPos > 0)  
  50.             {  
  51.                 context.moveTo(xPos, this.centerY - this.tickSize / 2);  
  52.                 context.lineTo(xPos, this.centerY + this.tickSize / 2);  
  53.                 context.stroke();  
  54.                 context.fillText(unit, xPos, this.centerY + this.tickSize / 2 + 3);  
  55.                 unit -= this.unitsPerTick;  
  56.                 xPos = Math.round(xPos - xPosIncrement);  
  57.             }  
  58.             // draw right tick marks  
  59.             xPos = this.centerX + xPosIncrement;  
  60.             unit = this.unitsPerTick;  
  61.             while (xPos < this.canvas.width)  
  62.             {  
  63.                 context.moveTo(xPos, this.centerY - this.tickSize / 2);  
  64.                 context.lineTo(xPos, this.centerY + this.tickSize / 2);  
  65.                 context.stroke();  
  66.                 context.fillText(unit, xPos, this.centerY + this.tickSize / 2 + 3);  
  67.                 unit += this.unitsPerTick;  
  68.                 xPos = Math.round(xPos + xPosIncrement);  
  69.             }  
  70.             context.restore();  
  71.         };  
  72.         window.onload = function ()  
  73.           {  
  74.             var myGraph = new Graph({  
  75.                 canvasId: "myCanvas",  
  76.                 minX: -10,  
  77.                 minY: -10,  
  78.                 maxX: 10,  
  79.                 maxY: 10,  
  80.                 unitsPerTick: 1  
  81.             });  
  82.              myGraph.drawEquation(function (x)  
  83.             {  
  84.                return 5 * Math.sin(x);  
  85.             }, "yellow", 3);  
  86.              myGraph.drawEquation(function (x)  
  87.             {  
  88.                return x * x;  
  89.             }, "orange", 3);  
  90.             myGraph.drawEquation(function (x)  
  91.             {  
  92.                 return 1 * x;  
  93.             }, "green", 3);  
  94.         };  
  95. </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 canvasgraphicalequation.aspx page. Here we pass a Canvas in the canvas tag. 
  1. <body style="background-color: #C9E0E6">  
  2.     <center>  
  3.         <h1>  
  4.             Canvas Graphical Equation Representation  
  5.         </h1>  
  6.     </center>  
  7.     <hr />  
  8.     <canvas id="myCanvas" width="578" height="300">  
  9.         </canvas>  
  10. </body> 
Step 5: The complete code for the CanvasGraphEquation application.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="canvasgraphicalequation.aspx.cs" Inherits="CanvasGraphEquation._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.     </style>  
  7.     <script>  
  8.     </script>  
  9. </head>  
  10. <body style="background-color: #C9E0E6">  
  11.     <center>  
  12.         <h1>  
  13.             Canvas Graphical Equation Representation  
  14.         </h1>  
  15.     </center>  
  16.     <hr />  
  17.     <canvas id="myCanvas" width="578" height="300">  
  18.         </canvas>  
  19. </body>  
  20. </html>  
Step 6: Output Press F5
 
Note: For the accurate output of HTML5 applications, you must have the Google Chrome browser on your PC. A Graphical equation displays when the application runs on the browser.