Curve Detection With Canvas Using HTML 5

Introduction

 
In this article, we are going to understand the concept of Curve detection using HTML 5. In this section, we will see a ball thrown around with your cursor and the ball revolves around a specified path while being displayed 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 CurveDetection 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 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 new Web form
  • Rename it to curvedetectionpage.aspx
curve1.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. JavaScript for Handling curve collision
  1. function handleCurveCollision(ball, curve)  
  2.        {  
  3.            var curveLayer = curve.getLayer();  
  4.            var curveContext = curveLayer.getContext();  
  5.            var curveDamper = 0.05; // 5% energy loss  
  6.             if (curveContext.isPointInPath(ball.x, ball.y))  
  7.               {  
  8.                 var normal = getNormal(curve, ball);  
  9.                  if (normal !== null)  
  10.                  {  
  11.                    var angleToNormal = angleBetween(normal, invert(ball.velocity));  
  12.                    var crossProduct = cross(normal, ball.velocity);  
  13.                    var polarity = crossProduct.z > 0 ? 1 : -1;  
  14.                    var collisonAngle = polarity * angleToNormal * 2;  
  15.                    var collisionVector = rotate(ball.velocity, collisonAngle);  
  16.                    ball.velocity.x = collisionVector.x;  
  17.                    ball.velocity.y = collisionVector.y;  
  18.                    ball.velocity.x *= (1 - curveDamper);  
  19.                    ball.velocity.y *= (1 - curveDamper);  
  20.                    while (curveContext.isPointInPath(ball.x, ball.y))  
  21.                      {  
  22.                         ball.x += normal.x;  
  23.                          if (ball.velocity.y > 0.1)  
  24.                          {  
  25.                            ball.y += normal.y;  
  26.                          }  
  27.                         else  
  28.                         {                              
  29.                           ball.y += normal.y / 10;  
  30.                         }  
  31.                     }  
  32.                 }  
  33.             }  
  34.         } 
JavaScript for window onload
  1. window.onload = function ()  
  2.          {  
  3.            var stage = new Kinetic.Stage("container", 578, 400);  
  4.            var curveLayer = new Kinetic.Layer();  
  5.            var ballLayer = new Kinetic.Layer();  
  6.            var radius = 20;  
  7.            var curve = new Kinetic.Shape({  
  8.            drawFunc: function ()  
  9.            {  
  10.            var canvas = this.getCanvas();  
  11.            var context = this.getContext();  
  12.            context.beginPath();  
  13.            context.moveTo(40, canvas.height);  
  14.            context.bezierCurveTo(canvas.width * 0.2, -1 * canvas.height * 0.5, canvas.width * 0.7, canvas.height * 1.3, canvas.width, canvas.height * 0.5);  
  15.            context.lineTo(canvas.width, canvas.height);  
  16.            context.lineTo(40, canvas.height);  
  17.            context.closePath();  
  18.            context.fillStyle = "#8dbdff";  
  19.            context.fill();  
  20.            }  
  21.           }); 
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 curvedetectionpage.aspx page. Here we pass a Canvas in the canvas tag.
  1. <body style="background-color: #FFFAF0">  
  2.     <center>  
  3.         <h1>  
  4.             Curve detection  
  5.         </h1>  
  6.         <hr />  
  7.         <div id="container">  
  8.         </div>  
  9.     </center>  
  10. </body> 
Step 5 : The complete code for the CurveDetection application is:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="curvedetectionpage.aspx.cs" Inherits="CurveDetection._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 src="jscript.js">  
  8.     </script>  
  9.     <script>  
  10.     </script>  
  11. </head>  
  12. <body style="background-color: #FFFAF0">  
  13.     <center>  
  14.         <h1>  
  15.             Curve detection  
  16.         </h1>  
  17.         <hr />  
  18.         <div id="container">  
  19.         </div>  
  20.     </center>  
  21. </body>  
  22. </html> 
Step 6: Output Press F5 Note: For the accurate output of HTML5 applications, you must have the Google Chrome browser in your PC. You will see the ball thrown around with your cursor and the ball is revolving around a given path while being displayed in the browser.
 
curve.gif
 
Here are some useful resources