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 the 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 are used for design purposes.

JavaScript for Handling curve collision

function handleCurveCollision(ball, curve)
       {
           var curveLayer = curve.getLayer();
           var curveContext = curveLayer.getContext();
           var curveDamper = 0.05; // 5% energy loss
            if (curveContext.isPointInPath(ball.x, ball.y))
              {
                var normal = getNormal(curve, ball);
                 if (normal !== null)
                 {
                   var angleToNormal = angleBetween(normal, invert(ball.velocity));
                   var crossProduct = cross(normal, ball.velocity);
                   var polarity = crossProduct.z > 0 ? 1 : -1;
                   var collisonAngle = polarity * angleToNormal * 2;
                   var collisionVector = rotate(ball.velocity, collisonAngle);
                   ball.velocity.x = collisionVector.x;
                   ball.velocity.y = collisionVector.y;
                   ball.velocity.x *= (1 - curveDamper);
                   ball.velocity.y *= (1 - curveDamper);
                   while (curveContext.isPointInPath(ball.x, ball.y))
                     {
                        ball.x += normal.x;
                         if (ball.velocity.y > 0.1)
                         {
                           ball.y += normal.y;
                         }
                        else
                        {                            
                          ball.y += normal.y / 10;
                        }
                    }
                }
            }
        }

JavaScript for window onload

window.onload = function ()
         {
           var stage = new Kinetic.Stage("container", 578, 400);
           var curveLayer = new Kinetic.Layer();
           var ballLayer = new Kinetic.Layer();
           var radius = 20;
           var curve = new Kinetic.Shape({
           drawFunc: function ()
           {
           var canvas = this.getCanvas();
           var context = this.getContext();
           context.beginPath();
           context.moveTo(40, canvas.height);
           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);
           context.lineTo(canvas.width, canvas.height);
           context.lineTo(40, canvas.height);
           context.closePath();
           context.fillStyle = "#8dbdff";
           context.fill();
           }
          });

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.

<body style="background-color: #FFFAF0">
    <center>
        <h1>
            Curve detection
        </h1>
        <hr />
        <div id="container">
        </div>
    </center>
</body>

Step 5 : The complete code for the CurveDetection
application is:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="curvedetectionpage.aspx.cs" Inherits="CurveDetection._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <style>
    </style>
    <script src="jscript.js">
    </script>
    <script>
    </script>
</head>
<
body style="background-color: #FFFAF0">
    <center>
        <h1>
            Curve detection
        </h1>
        <hr />
        <div id="container">
        </div>
    </center>
</body>
</
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 the some useful resources

Canvas Clipping Region Using HTML 5
Use Canvas Tag in HTML 5
Change the Curve Position Using HTML 5
Explore generating visualisations using JavaScript in HTML5
Canvas in HTML 5

Up Next
    Ebook Download
    View all
    Learn
    View all